English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about C ++All knowledge of functions in the middle. What types of functions there are, and how to use them in examples.
In programming, a function is a code block that groups code to perform a specific task.
Depend on whether the function is predefined or created by the programmer; there are two types of functions:
Standard library functions
User-defined Function
Library functions are C ++Built-in functions in programming.
Programmers can use library functions by directly calling the function; they do not need to write them themselves.
#include <iostream> #include <cmath> using namespace std; int main() { double number, squareRoot; cout << "Input a number: "; cin >> number; // sqrt() is a library function used to calculate the square root squareRoot = sqrt(number); cout << "Number " << number << " Square Root = " << squareRoot; return 0; }
Output Result
Input a number: 26 Number 26 Square Root = 5.09902
In the above example, the sqrt() library function is called to calculate the square root of the number26Square Root.
The notice code #include <cmath> in the above program. Here cmath is the header file. The sqrt() function is defined in the cmath header file.
When you include the contents of the file <cmath> into this program using #include <cmath>, you can use all the functions defined in cmath.
Each valid C ++A program must have at least one function, i.e., the main() function.
C ++Allows programmers to define their own functions.
User-defined functions group the code that performs specific tasks and assign a name (identifier) to that group of code.
When the function is called from any part of the program, they will execute the code defined in the function body.
According to the above figure.
When the program starts running, the system calls the main() function, i.e., the program starts executing code from the main() function.
When the control of the program reaches function_name() inside main(), it moves to void function_name() and executes all the code inside void function_name().
Then, control of the program returns to the main function, where, as shown in the figure above, the code after the call to function_name() is executed.
C ++The program adds two integers. Create a function add() to add integers and display the sum in the main() function.
#include <iostream> using namespace std; // Function Prototype (Declaration) int add(int, int); int main() { int num1, num2, sum cout << "Input two numbers to be added: "; cin >> num1 >> num2; // Function Call sum = add(num1, num2); cout << "Sum = " << sum; return 0; } // Function definition int add(int a, int b) { int add; add = a + b; // Return Statement return add; }
Output Result
Input two numbers to be added: 8 -4 Sum = 4
If the user-defined function is defined after the main() function, the compiler will display an error. This is because the compiler does not know the user-defined function, the types of the parameters passed to the function, and the return type.
In C ++In C, the function prototype is a declaration of the function without its body, providing compiler information about the user-defined function. The function prototype in the above example is:
int add(int, int);
You can see that there is no function body in the prototype. There are only return type parameters, without parameters. You can also declare the function prototype as follows, but it is not necessary to write the parameters.
int add(int a, int b);
Note:If the user-defined function exists before the main() function, there is no need to define a prototype.
To execute the code in the function body, it is necessary to call (invoke) the user-defined function.
In the above program, add(num1,num2); The internal main() function calls the user-defined function.
The function returns an integer, which is stored in the variable add.
The function itself is called a function definition. The function definition in the above program is:
// Function definition int add(int a, int b) { int add; add = a + b; return add; }
When the function is called, control is transferred to the first statement of the function body.
Then, the other statements in the function body are executed in order.
When all the code in the function definition is executed, the program control moves to the calling program.
In programming, a parameter (parameter) refers to the data passed to the function (function definition) when calling the function.
In the above example, two variables num1and num2are passed to the function during the function call. These parameters are called actual parameters.
num1and num2are initialized to variables a and b. These parameters a and b are called formal parameters.
As shown in the following figure:
Points to note about passing parameters
The number of actual parameters and formal parameters should be the same. (Exception:)Function overloading)
The type of the first actual parameter should match the type of the first formal parameter. Similarly, the type of the second actual parameter should match the type of the second formal parameter, and so on.
You can call the function a without passing any parameters. The number of parameters passed to the function depends on how the programmer solves the problem.
You can specify default values for parameters. These parameters are calledDefault parameters.
In the above program, both parameters are of int type. However, there is no need to use two parameters of the same type simultaneously.
A function can return a single value to the calling program using the return statement.
In the above program, the following statements are used to return the value of add from the user-defined function to the calling program:
return add;
The following diagram demonstrates how the return statement works.
In the above program, the value of the user-defined function add is returned to the calling function. Then this value is stored in a variable sum.
Note the returned variable, i.e., add is of type int, and sum is also of type int.
Additionally, please note that the return type of the function is defined before the function declaration int add(int a, int b) as int, which indicates that the function should return a value of type int.
If no value is returned to the calling function, void should be used.