C language learning -- 9 functions

Keywords: C

function

A function is a set of statements that perform a task together. Every C program has at least one function, the main function (). All simple programs can define other additional functions.

You can divide the code into different functions. It's up to you to divide the code into different functions, but logically, the division is usually based on each function performing a specific task.

Function declarations tell the compiler the name, return type, and parameters of a function. The function definition provides the actual body of the function.

C standard library provides a large number of built-in functions that can be called by programs. For example, the function strcat() is used to connect two strings, and the function memcpy() is used to copy memory to another location.

There are many other names for functions, such as methods, subroutines or programs, and so on.

Define function

Format:
return_type function_name( parameter list )
{
   body of the function
}
Ingredients:
  • Return type: a function must contain a return type return_type, the keyword of the return type is defined according to the type of value to be returned. If the return value is not required, return_type is void
  • Function name: the name of the function to be defined
  • Parameter: Yes or no, parameter is the type that defines the parameter, and list is the name of the parameter. Parameters defined here are formal parameters and cannot be used outside the function body
  • Function body: the function body contains a set of statements that define the execution task of the function.
example:
The following is the source code of the max() function. This function has two parameters num1 and num2, which will return the larger of the two numbers:
/* Function returns the larger of the two numbers */
int max(int num1, int num2) 
{
   /* Local variable declaration */
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}

Function declaration

The function declaration tells the compiler the name of the function and how to call it. The actual body of a function can be defined separately.
The function declaration includes the following parts:
return_type function_name( parameter list );

For the function max() defined above, the following is the function declaration:

int max(int num1, int num2);

In the function declaration, the name of the parameter is not important. Only the type of the parameter is required. Therefore, the following is also a valid declaration:

int max(int, int);

When you define functions in a source file and call functions in another file, function declarations are required. In this case, you should declare the function at the top of the file that calls the function.

Call function

When you create a C function, you define what the function does, and then complete the defined tasks by calling the function.

When a program calls a function, program control is transferred to the called function. The called function executes the defined task. When the return statement of the function is executed or the end bracket of the function is reached, the program control will be returned to the main program.

When calling a function, pass the required parameters. If the function returns a value, you can store the return value. For example:

#include <stdio.h>
 
/* Function declaration */
int max(int num1, int num2);
 
int main ()
{
   /* Local variable definition */
   int a = 100;
   int b = 200;
   int ret;
 
   /* Call the function to get the maximum value */
   ret = max(a, b);
 
   printf( "Max value is : %d\n", ret );
 
   return 0;
}
 
/* Function returns the larger of the two numbers */
int max(int num1, int num2) 
{
   /* Local variable declaration */
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}

Put the max() function and the main() function together and compile the source code. When the last executable is run, the following results are generated:

Max value is : 200

Function parameters

If a function wants to use parameters, it must declare variables that accept parameter values. These variables are called formal parameters of the function. Formal parameters, like other local variables in a function, are created when entering the function and destroyed when exiting the function. When calling a function, there are two ways to pass parameters to the function:
Call typedescribe
Value passing callThis method copies the actual value of the parameter to the formal parameter of the function. In this case, modifying the formal parameters in the function will not affect the actual parameters.
Reference callThrough the pointer transfer method, the formal parameter is a pointer to the address of the argument. When pointing to the formal parameter, it is equivalent to the operation on the argument itself.
By default, C uses value passing calls to pass parameters. In general, this means that the code in the function cannot change the actual parameters used to call the function.

Call function by value

The value passing method that passes the parameter to the function calls the method, and copies the actual value of the parameter to the formal parameter of the function. under these circumstances, Modifying formal parameters in a function does not affect the actual parameters.
By default, C language uses value passing call methods to pass parameters. In general, this means that the code in the function does not change the actual parameters used to call the function. The function swap() is defined as follows:
/* Function definition */
void swap(int x, int y)
{
   int temp;

   temp = x; /* Save the value of x */
   x = y;    /* Assign y to x */
   y = temp; /* Assign temp to y */
  
   return;
}

Now, let's call the function swap() by passing the actual parameters:

#include <stdio.h>
 
/* Function declaration */
void swap(int x, int y);
 
int main ()
{
   /* Local variable definition */
   int a = 100;
   int b = 200;
 
   printf("Before the exchange, a Value of: %d\n", a );
   printf("Before the exchange, b Value of: %d\n", b );
 
   /* Call a function to exchange values */
   swap(a, b);
 
   printf("After the exchange, a Value of: %d\n", a );
   printf("After the exchange, b Value of: %d\n", b );
 
   return 0;
}

When the above code is compiled and executed, it will produce the following results:

Before the exchange, a Value of: 100
 Before the exchange, b Value of: 200
 After the exchange, a Value of: 100
 After the exchange, b Value of: 200

The above example shows that although the values of a and b are changed in the function, in fact, the values of a and b are not changed.

Call function by reference

By reference transfer, the formal parameter is a pointer to the address of the argument. When pointing to the formal parameter, it is equivalent to the operation on the argument itself.

Passing a pointer allows multiple functions to access the object referenced by the pointer without declaring the object globally accessible.

/* Function definition */
void swap(int *x, int *y)
{
   int temp;
   temp = *x;    /* Save the value of address x */
   *x = *y;      /* Assign y to x */
   *y = temp;    /* Assign temp to y */
  
   return;
}

Now, let's call the function swap() by passing a value by reference:

#include <stdio.h>
 
/* Function declaration */
void swap(int *x, int *y);
 
int main ()
{
   /* Local variable definition */
   int a = 100;
   int b = 200;
 
   printf("Before the exchange, a Value of: %d\n", a );
   printf("Before the exchange, b Value of: %d\n", b );
 
   /* Call a function to exchange values
    * &a Indicates the pointer to a, that is, the address of variable a
    * &b Indicates the pointer to b, that is, the address of variable b
   */
   swap(&a, &b);
 
   printf("After the exchange, a Value of: %d\n", a );
   printf("After the exchange, b Value of: %d\n", b );
 
   return 0;
}

When the above code is compiled and executed, it will produce the following results:

Before the exchange, a Value of: 100
 Before the exchange, b Value of: 200
 After the exchange, a Value of: 200
 After the exchange, b Value of: 100

The example of face shows that, unlike the value passing call, the reference call changes the values of a and b inside the function, and actually changes the values of a and b outside the function.

Posted by curby on Sat, 18 Sep 2021 20:06:41 -0700