C function


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

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

Function Declaration Tells the compiler the name, return type, and parameters of the function. FunctionDefinition provides the actual body of the function.

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

Function also has many names, such as method, subroutine or program, etc.

Define function

The general form of function definition in C language is as follows:

return_type function_name( parameter list ){
   body of the function}

In C language, a function consists of a function header and a function body. All components of a function are listed below:

  • Return type: A function can return a value. return_type is the data type of the value returned by the function. Some functions perform the required operation without returning a value, in this case return_type is the keyword void.

  • Function Name: This is the actual name of the function. The function name and parameter list together form the function signature.

  • Parameters: Parameters are like placeholders. When a function is called, you pass a value to the argument, which is called the actual argument. The parameter list includes the type, order, and number of function parameters. Parameters are optional, that is, the function may not contain parameters.

  • Function body: The function body contains a set of statements that define the tasks performed by the function.

Example

The following is the source code of the max() function. This function has two parameters num1 and num2, and will return the larger of the two numbers:

/* 函数返回两个数中较大的那个数 */int max(int num1, int num2) {   /* 局部变量声明 */   int result; 
   if (num1 > num2)
      result = num1;   else
      result = num2; 
   return result; }

Function declaration

FunctionDeclaration will tell the compiler Function name and how to call the function. The actual body of the 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 It doesn't matter, only the types of the parameters are required, so the following is also a valid declaration:

int max(int, int);

A function declaration is required when you define a function in one source file and call it in another file . In this case, you should declare the function at the top of the file where it is called.

Call function

When you create a C function, you define what the function does, and then call the function to complete the defined task.

When a program calls a function, program control is transferred to the called function. The called function performs the defined task, and returns program control to the main program when the function's return statement is executed, or when the function's closing bracket is reached.

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> /* 函数声明 */int max(int num1, int num2); int main (){   /* 局部变量定义 */   int a = 100;   int b = 200;   int ret; 
   /* 调用函数来获取最大值 */
   ret = max(a, b);
 
   printf( "Max value is : %d\n", ret ); 
   return 0;} /* 函数返回两个数中较大的那个数 */int max(int num1, int num2) {   /* 局部变量声明 */   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 final executable is run, the following results are produced:

Max value is : 200

Function parameters

If a function is to use parameters, it must declare variables that accept the parameter values. These variables are called the formal parameters of the function.

Formal parameters are like other local variables within a function. They 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 typeDescription
Call by valueThis method copies the actual value of the parameter Formal parameters given to the function. In this case, modifying the formal parameters within the function does not affect the actual parameters.
Reference callThis method copies the address of the parameter to the formal parameter. Within the function, this address is used to access the actual parameters to be used in the call. This means that modifying the formal parameters affects the actual parameters.

By default, C uses call-by-value to pass parameters. Generally speaking, this means that code within a function cannot change the actual parameters used to call the function.