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 (), and all simple programs can define additional functions.

You can divide the code into different functions. It’s up to you to decide how to divide your code into different functions, but logically, partitioning is usually based on each function performing a specific task.

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

Functions are called methods, subroutines, programs, and so on.

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

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

In C++, a function consists of a function header and a function body. The following lists all the components of a function:

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 desired 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 argument list together form the function signature. Parameters: Parameters are like placeholders. When a function is called, you pass a value to the parameter, which is called the actual parameter. The parameter list includes the type, order, and number of function arguments. Arguments are optional, that is, functions may contain no arguments. Function body: The function body contains a set of statements that define the tasks the function performs. Below is the source code for the Max () function. This function takes two arguments, num1 and num2, and returns the larger of the two numbers:

// The function returns the larger of the two numbers

Int Max (int num1, int num2) {// local variable declaration int result; if (num1 > num2) result = num1; else result = num2; return result; }Copy the code

Function declarations Function declarations tell the compiler the name of the function and how to call it. The actual body of a function can be defined separately.

The function declaration consists of the following parts:

return_type function_name( parameter list );

For the function Max () defined above, here is the function declaration:

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 functions in one source file and call them in another. In this case, you should declare the function at the top of the file calling the function.

Call a function

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

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

When you call a function, you pass in the required arguments, and if the function returns a value, you store the return value. Such as:

#include <iostream> using namespace std; Int Max (int num1, int num2); Int main () {// local variable declaration int a = 100; int b = 200; int ret; Ret = Max (a, b); cout << "Max value is : " << ret << endl; return 0; Int Max (int num1, int num2) {// local variable declaration int result; if (num1 > num2) result = num1; else result = num2; return result; }Copy the code

Compile the source code by putting the Max () and main() functions together. When the final executable is run, the following results are produced:

Max value is : 200

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

Formal parameters, like other local variables within a function, are created upon entry and destroyed upon exit.

When a function is called, there are three ways to pass arguments to the function:

Default Values for Parameters When you define a function, you can specify default values for each parameter later in the argument list. This default value is used if the value of the actual argument is left blank when a function is called.

This is done by using the assignment operator in the function definition to assign values to parameters. When a function is called, the default value is used if the value of the argument is not passed; if a value is specified, the default value is ignored and the passed value is used. See the following example:

#include <iostream> using namespace std; int sum(int a, int b=20) { int result; result = a + b; return (result); } int main () {// int a = 100; int b = 200; int result; // call the function to add the value result = sum(a, b); cout << "Total value is :" << result << endl; Result = sum(a); cout << "Total value is :" << result << endl; return 0; }Copy the code

When the above code is compiled and executed, it produces the following results:

Total value is :300 Total value is :120

Lambda functions and expressions C++11 provides support for anonymous functions called Lambda functions (also called Lambda expressions).

Lambda expressions treat functions as objects. Lambda expressions can be used like objects, for example by assigning them to variables and passing them as arguments, and by evaluating them like functions

Lambda expressions are very similar in nature to function declarations. Lambda expressions take the following form:

The capture – > return -type {body} such as:

[](int x, int y){ return x < y ; }

If there is no return value, it can be expressed as:

capture{body}

Such as:

[]{ ++global_x; }

In a more complex example, the return type can be explicitly specified as follows:

[](int x, int y) -> int { int z = x + y; return z + x; }

In this case, a temporary parameter Z is created to store the intermediate results. As with normal functions, the value of z is not retained until the next time the unnamed function is called.

If a lambda function does not return a value (for example, void), its return type can be completely ignored.

Variables of the current scope can be accessed within a Lambda expression, which is the Closure behavior of a Lambda expression. Unlike JavaScript closures, C++ variable passing has a distinction between passing a value and passing a reference. This can be specified by the previous [] :

[] // No variables are defined. Using undefined variables raises an error. [x, &y] // x is passed as a value (default) and y as a reference. [&] // Any external variables that are used are implicitly referenced. [=] // Any external variables that are used are implicitly referred to as pass-throughs. [&, x] // x is explicitly referenced as a pass-value. The remaining variables are referenced by reference. [=, &z] // z is explicitly referenced by reference. The rest of the variables are referenced by value.Copy the code

There is another point to note. Lambda expressions can use the this pointer directly for the form [=] or [&]. However, for the form [], if we want to use the this pointer, we must pass it explicitly:

this { this->someFunc(); } ();

Recently sorted out a number of suitable for small white people entry learning materials, including e-books, tools, installation packages and so on click to join my official group [free] access to a full set of C language proficient learning materials. (“▔□▔)/ If you are new to C, you can share a full set of C language mastery learning materials to you. If you are learning C++, you can help you to learn the DIRECTION of C++ game development, so that you can think clearly and learn more effectively