C Function

; ... ... and so on.main(), and all simple programs can define other additional functions.

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

FunctionDeclarationTells the compiler the function's name, return type, and parameters. A functiondefinitionProvides 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 functionstrcat()Used to concatenate two strings, the functionmemcpy()Used to copy memory to another location.

Functions have many other names, such as methods, subroutines, or procedures, and so on.

Define Function

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

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

In C, a function consists of a function header and a function body. Below is a list of all the parts of a function:

  • Return type:A function can return a value.return_typeIs the data type of the value returned by the function. Some functions perform the desired operations without returning a value; in this case, return_type is the keywordvoid。
  • Function name:This is the actual name of the function. The function name and the parameter list together constitute the function signature.
  • Parameters:Parameters act like placeholders. When a function is called, you pass a value to the parameter, and this value is called an actual parameter. The parameter list includes the type, order, and number of the function's parameters. Parameters are optional; that is, a function may contain no parameters.
  • Function body:The function body contains a set of statements that define what the function does.

Example

The following aremax()The source code of the function. This function has two parameters, num1 and num2, and returns the larger of the two numbers:

/*The function returns the larger of 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

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

A function declaration consists of 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 a function declaration, the parameter names are not important; only the parameter types are required. Therefore, the following is also a valid declaration:

int max(int, int);

When you define a function in one source file and call it in another file, a function declaration is 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 task by calling the function.

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

When calling a function, pass the required parameters, and if the function returns a value, you can store the returned value. For example:

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; } /*The function returns the larger of 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, compile the source code. When the final executable file is run, the following results are produced:

Max value is : 200

Function parameters

If the function is to use parameters, you must declare variables that accept the parameter values. These variables are called the function'sFormal parameters。

Formal parameters are like other local variables inside 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 values of the arguments to the function's formal parameters. In this case, modifying the formal parameters inside the function does not affect the actual arguments.
Call by ReferenceIn pointer passing, the formal parameter is a pointer to the address of the actual argument. When an operation is performed on the formal parameter's pointing, it is equivalent to operating on the actual argument itself.

By default, C usesCall by valueto pass parameters. Generally speaking, this means that the code inside the function cannot change the actual arguments used to call the function.

other extensions