Wu Yuxiong - born natural C ++ language study notes: C ++ function

Is a set of functions to perform a task with the statement. Each C ++ programs have at least one function, i.e. the main function main (), all simple program can define other additional functions.
Code can be divided into different function. How to divide the code into separate functions it is up to you to decide, but logically divided usually carried out each function to perform a specific task based on.
Function declaration tells the compiler function name, return type and parameter. Function definition provides the actual body of the function.
C ++ standard library provides a large number of built-in functions of the program can call. For example, the function strcat () is used to connect two strings, functions memcpy () to copy the memory to another location.
There are many function is called, such as methods, procedures or subroutines, and so on.
C ++ general form of a function defined as follows:
return_type function_name( parameter list )
{
   body of the function
}
In C ++ , the function of a first function and a function of body composition. Listed below are a function of all components:
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 required operations without the return value, in this case, the keyword return_type void .
Function name: This is the actual name of the function. Function name and parameter list together constitute the function signature.
Parameters: Parameter is like a placeholder. When the function is called, you pass a value to the parameter, this value is called the actual parameters. Parameter list of function parameters include the type, order, quantity. Parameter is optional, that is, the function may not contain parameters.
Main function: function body contains a set of functions defined mission statement.
The following is the max () function in the source code. This function takes two parameters num1 and num2, which will return the larger of the two numbers count:
 // function returns the larger of the two numbers in the number of
 
int max(int num1, int num2) 
{
   // local variable declaration 
   int the Result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}
Function declaration includes the following sections:
return_type function_name( parameter list );
For the above-defined function max (), is a function of the following statement:
int max(int num1, int num2);
In the function declaration, the name of the parameter is not important, only the type of the parameter is required, so the following is a valid statement:
int max(int, int);
When a function is defined in the source file, and call a function in another file, the function declaration is necessary. In this case, you should call the function declaration function at the top of the file.
Create a C ++ function, the function will define what to do, and then to complete the task it has been defined by calling the function.
When the program calls a function, program control will be transferred to the called function. When the function is defined tasks is called when the function return statement is executed, or reach the end brackets function, the program will return control to the main program.
The function is called, passing the required parameters, if the function returns a value, the return value may be stored. E.g:
#include <iostream>
using namespace std;
 
// function declaration 
int max ( int num1, int num2);
 
int main ()
{
   // local variable declaration 
   int A = 100 ;
    int B = 200 is ;
    int RET;
 
   // call the function to get the maximum 
   RET = max (A, B);
 
   cout << "Max value is : " << ret << endl;
 
   return 0;
}
 
// function returns the number of the larger of the two numbers 
int max ( int num1, int num2)
{
   // local variable declaration 
   int the Result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}
The max () function and the main () function to put a, compile the source code. When you run the final executable file will produce the following results:
Max value is : 200
If you want to use the function parameter, you must declare a variable to accept the parameter value. These variables are called the formal parameters of the function.
Like other forms of local variables within the parameter of the function is created when entering the function, it is destroyed when the function exits.
When you call the function, there are three ways to pass parameters to the function:
Call-by-the method copies the actual values ​​of the parameters to the formal parameters of the function. In this case, the parameter modifications have no effect on the function of the actual parameter.
This method calls the copy pointer address parameter to the formal parameters. Inside the function, the address for the actual parameters to use to access the call. This means that modification of parameters affect the actual parameters.
This method is called the reference copy reference to the formal parameters parameters. Inside the function, the reference to the actual parameters to use to access the call. This means that modification of parameters affect the actual parameters.
By default, C ++ using the call-by to pass parameters. In general, this means that the code in the function can not change the parameters used to call the function. Examples mentioned before, call max () function, the method of using the same.
The default value of the parameter
When you define a function, you can specify default values ​​for each parameter in the parameter list behind. When you call the function, if the value of the actual parameter is left blank, then the default value.
This is done using the assignment operator to assign the function definition as a parameter. When you call the function, if the value of the parameter is not passed, then the default value if the value is specified, the default value is ignored, using the passed value. Consider the following examples:

int main ()
{
   // local variable declaration 
   int A = 100 ;
    int B = 200 is ;
    int Result;
 
   // call the function to add values 
   Result = SUM (A, B);
   cout << "Total value is :" << result << endl;
 
   // call the function again with 
   the Result = SUM (A);
   cout << "Total value is :" << result << endl;
 
   return 0;
}
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 function (also known as Lambda expressions).
Lambda expressions to be seen as a function of the object. Lambda expressions can be used like an object, such as they can be assigned to variables and passed as parameters, like a function thereof may also be evaluated.
Lambda expressions on the nature and function declarations are very similar. Lambda expressions specific forms as follows:
[capture](parameters)->return-type{body}
[](int x, int y){ return x < y ; }
If no return value can be expressed as:
[capture](parameters){body}
[] { ++ global_x; }
In a more complex example, the return type may be explicitly specified as follows:
[](int x, int y) -> int { int z = x + y; return z + x; }
C ++ variable has passed by value and by-reference difference. Can be specified by the previous []:
[]       // no variables are defined. The use of undefined variables will lead to error. 
[X, Y &] // X In passed by value (the default), y passed by reference. 
[&]      @ Any external variables to be used are implicitly be referenced by reference. 
[=]      @ Any external variables to be used are to be referenced implicitly by value. 
[&, X]   // X explicitly to be referenced by value. The remaining variables to be cited by reference. 
[=, & Z] // Z explicitly be referenced by reference. The remaining variables to be referenced by value. 
Another point to note. For the [=] or [&] form, lambda expressions can directly use this pointer. However, [] in the form of, if you want to use this pointer must be explicitly pass:

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

 

Guess you like

Origin www.cnblogs.com/tszr/p/12148358.html