Functions are a set of statements that execute a specific task together. Every C program has at least one function, which is the main function main(), and all simple programs can define additional functions.You can divide your code into different functions. How to divide the code into different functions is up to you, which is what we commonly refer to as modular programming. Logically, the division is usually based on each function performing a specific task.A function declaration tells the compiler the function’s name, return type, and parameters. 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 printf() is used to output data, and the function scanf() is used to accept input from the keyboard.Functions also have many other names, such as methods, subroutines, or procedures, etc.Function DefinitionOf course, the function definitions in each programming language are quite similar, so the template is relatively universal. The general form of a function definition in C language is as follows:
return_type function_name(parameter1, parameter2, parameter3...) { function_body_code }
In C language, a function consists of a function header and a function body. Below are all the components of a function:Return Type: A function can return a value. The so-called return type is the data type of the value returned by the function. Some functions perform operations without returning a value; in this case, the return type is the keyword void.Function Name: This is the actual name of the function. The function name and the parameter list together form the function signature.Parameters: Parameters act as placeholders. When a function is called, you pass a value to the parameters, which is called the actual parameter or argument; however, the parameters you write when defining the function are called formal parameters. The parameter list includes the type, order, and number of function parameters. Parameters are optional, meaning a function may not contain parameters.Function Body: The function body contains a set of statements that define the task the function performs.ExampleBelow is the source code for the max() function. This function has two parameters num1 and num2, and it returns the larger of the two numbers:
/* 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; // Return the maximum value}
Function CallWhen creating a C function, you define what the function does and then call the function to complete the defined task.When the program calls a function, control is transferred to the called function. The called function executes the defined task, and when the function’s return statement is executed or the end brace of the function is reached, control is returned to the main program.When calling a function, pass the required parameters, and if the function returns a value, it can be stored. For example:
#include <stdio.h> // Include header file // Define a function to find the maximum value max() that compares two numbers using the ternary operator from the previous articleint max(int num1, int num2) { return num1 > num2 ? num1 : num2;}int main() { printf("%d", max(3, 6)); // Call max() function and output the return value return 0; }
Of course, you can also define a variable to receive the return value of the function and output it, which has the same effect:
int ans = max(1, 2);printf("%d", ans);
Compile the source code. When running the final executable file, the final result is as follows:
After learning about function definitions and calls, we often encounter a problem: why does it report an error when I clearly defined the function? For example:
#include <stdio.h> // Include header fileint main() { printf("%d", max(3, 6)); return 0; } /* Define a function to find the maximum value max() that compares two numbers using the ternary operator? If the expression num1 > num2 is true, return the maximum value num1; otherwise, return num2 */int max(int num1, int num2) { return num1 > num2 ? num1 : num2;}
When I learned C language, if I placed the function definition after the main() function and did not declare the function at the beginning, it would report an error. However, it seems that after the C90 standard, this error has turned into a warning, which does not affect code execution. The warning is as follows:
To solve this problem, we need to learn the following knowledge point — Function DeclarationFunction DeclarationA function declaration tells the compiler the function name and how to call the function. The actual body of the function can be defined separately.
return_type function_name(parameter_list...);
In a 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);
So for the above problem, we just need to add the function declaration at the beginning of the program, and the complete code is as follows:
#include <stdio.h> // Include header fileint max(int, int);int main() { printf("%d", max(3, 6)); return 0; } // Define a function to find the maximum value max() that compares two numbers using the ternary operatorint max(int num1, int num2) { return num1 > num2 ? num1 : num2;}
Finally, let’s briefly talk about function formal parameters.Formal ParametersIf a function needs to use parameters, it must declare variables that accept parameter values. These variables are called formal parameters of the function.Formal parameters are like other local variables within the function, 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 Type | Description |
|---|---|
| Pass by Value | This method copies the actual value of the parameter to the function’s formal parameters. In this case, modifying the formal parameters within the function does not affect the actual parameters. |
| Pass by Reference | By passing pointers, the formal parameters are pointers to the addresses of the actual parameters. When the formal parameters are manipulated, it is equivalent to manipulating the actual parameters themselves. |
By default, C uses pass by value to pass parameters. Generally, this means that the code within the function cannot change the values of the actual parameters used to call the function.Of course, there are many other functions and usages, which we will gradually understand later. Well, that concludes the function section for today.In the next lesson, we will learn about the concept of scope in C language, studying the scope of variables in detail. Interested friends can try to understand it first.See you in the next lesson (づ。◕‿‿◕。)づ!