Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C function


May 11, 2021 C


Table of contents


The C function

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

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

The function declaration tells the compiler the name, return type, and argument of the compiler function. The function definition 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 connect two strings, and the function memcpy() is used to copy memory to another location.

Functions have many more calls, such as methods, sub-routines, or programs, and so on.

Define the function

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

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

In the 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. e is the data type of the value returned by the function. Some functions do what they want without returning a value, in which case return_type is the keyword void.
  • Function name: This is the actual name of the function. The function name, together with the list of parameters, forms the function signature.
  • Argument: The argument is like a placeholder. W hen a function is called, you pass a value to the argument, which is called the actual argument. T he list of parameters includes the type, order, and number of function parameters. Arguments are optional, that is, functions may not contain arguments.
  • Function body: A function body contains a set of statements that define a function to perform a task.

The following is the source code for the max() function. The function has two parameters, num1 and num2, which 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

The function declaration tells the compiler the name of the function and how to call it. The actual body of the function can be defined separately.

Function declarations consist of the following sections:

return_type function_name( parameter list );

For the function max() defined above, here are the function declarations:

int max(int num1, int num2);

In a function declaration, the name of the argument is not important, only the type of the argument is required, so the following is also a valid declaration:

int max(int, int);

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

Call the function

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

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

When a function is called, the desired argument is passed, and if the function returns a value, the return value can be stored. 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 you run the last executable, the following results occur:

Max value is : 200

Function arguments

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

Formal parameters, like other local variables within a function, are created when they enter a function and destroy when they exit.

When a function is called, there are two ways to pass arguments to it:

Call type describe
Pass value call This method copies the actual value of the parameters to the formal parameters of the function.In this case, the formal parameters in the modification function will not affect the actual parameters.
Reference call This method copies the address of the parameters to the form parameters.In the function, the address is used to access the actual parameters to be used in the call.This means that modification form parameters affect the actual parameters.

By default, C uses a pass-through call to pass arguments. In general, this means that the code within the function cannot change the actual parameters used to call the function.