C Language Tutorial Methods and Classes 1 .

Mondo Education Updated on 2024-01-31

1. Overview

The program modules in C are called methods and classes, and C programs are made up of a combination of methods and classes. A method is called by a method call statement that describes the name of the method and provides the information (parameters) needed to call the method to perform a specific task, and when the method is called, the method either returns a value to the method that called it, or simply returns control to the method that called it.

Methods allow programmers to modularize programs, and there are several advantages to modularizing programs with methods:

One is that this divide and conquer approach makes the development of programs manageable;

The second is that software reuse can be realized, using existing methods and classes as building blocks to create new programs, so that we can use the correct method names and definitions to create programs created by standard methods, rather than building custom ones

The third is to avoid repeated writing in the program, and the ** encapsulated in the form of a method can be in several positions in the program.

2. Statement of methodology

A method is a member of a class that is used to perform calculations or other actions, and here's the declaration format for the method:

Attribute Method Modifier Return Value Type Method Name (Parameter List), max(6,8));

The output of the program is:

the max of 6 and 8 is:8

3. Parameters in the method

There are four types of arguments to the method in c, and they are:

1 value parameter

The value parameter does not contain any modifiers.

2 Reference Parameters

Declare referential parameters with the ref modifier.

3 Output Parameters

Declare the output parameters with the out modifier.

4 Array parameters

Declare array-type parameters with params modifiers

3.1 value parameter

When a value is used to pass a parameter to a method, the compiler makes a copy of the value of the argument and passes this copy to the method. The method being called does not modify the value of the argument in memory, so when using the value parameter, you can ensure that the actual value is safe.

Here's an attempt to swap two values:

static void swap(int x, int y)

int temp = x;

x = y;

y = temp;

Let's call this function to complete the swapping of two numbers:

static void main()

int i = 1, j = 2;

swap(i, j);

console.writeline("i = , j = ", i, j);

The output of the call above is:

i = 1, j = 2

Analysis: The reason for this result is that the parameters of the swap method are used to pass the value, and we emphasized above that when the value parameter is passed, the compiler will generate a copy, so the swap is actually the value of the copy, not the value of the argument

3.2 Reference Parameters

From the above example, we know that the value parameter cannot complete the value exchange, and if the data exchange is to be done, the argument itself must be passed into the method call, not a copy of the argument. c provides us with another way to pass parameters, which is reference parameters, unlike value parameters, reference parameters do not open up new memory areas, when using reference parameters to pass form parameters to a method, the compiler will pass the address of the actual value in memory to the method.

Let's use the example of swapping two values to illustrate the use of reference parameters.

static void swap(ref int x, ref int y), j = ", i, j);

This time, the output of the function is:

i = 2, j = 1

I'm a tech creator

Related Pages