Chapter 6 Functions
6.1 Function basis
1. In the function body, once a return statement is encountered, the control flow is returned to the caller
This means that in a for loop or while loop, when it encounters a return, it exits directly and does not continue to execute the loop.
2. The function return type cannot be an array
3. Initialization of local variables
If no initial value is given when the variable is defined, the variable will be initialized by default. The default initialization has two cases:
if the variable is a built-in type (int, char, float), the value of the variable is undefined. Using these undefined variables will cause an error.
If the variable is a user-defined class, the default constructor of the class will be executed.
It is recommended that when defining variables of built-in types, try to give them an initial value.
4. Use of local static variables
Local static variables are initialized when the execution path of the program passes the object definition statement for the first time, and are not destroyed until the program terminates.
5. Use void to explicitly declare an empty parameter list
6. Separate compilation
The difference between angle brackets and double brackets in header files
Angle brackets and double brackets search for header files in a different order:
- <> means to start the search from the system directory, and then search the directories listed in the PATH environment variable, instead of searching the current directory .
- "" means to search from the current directory first , and then the directories listed in the system directory and the PATH environment variable.
If we know that the header file is in the system directory or environment variable directory, we can use <> to speed up the search
The flow from source code to executable file
Pre-compilation (compilation pre-processing)
General processing work
such as macro definition ①, replace the macro definition instructions: such as # define Name TokenString. Replace Name with TokenString
②, conditional compilation instructions: # ifdef, # ifndef, # else, # elif, # endif according to conditions See whether to execute
③, the header file contains instructions: add the header file described in the cpp file to the cpp file
④, special symbol processing
The precompiled program basically completes the "replacement" of the source program. After this substitution, an output file with no macro definitions, no conditional compilation instructions, and no special symbols is generated. The meaning of this file is the same as the source file without preprocessing, but the content is different.
Compile
The work that the compiler needs to do is to pass lexical analysis and grammatical analysis, after confirming that all instructions comply with the grammatical rules, translate them into equivalent intermediate code representations or assembly codes.
compilation
The assembly process actually refers to the process of translating assembly language code into target machine instructions to
generate an independent single target file
link
Link all target files.
Compilation part of the reference article
6.2 Parameter transfer
1. Use references to avoid copying
Copying large type objects or container objects is relatively inefficient, and even some classes (IO) do not support copy operations at all. At this time, you can pass parameters by reference .
For example: when the function parameter is a string, because the string object may be very long, you should try to avoid copying them directly. At this time, it is a wise choice to use reference parameters.
If you don't want to change the value of the actual parameter because of the reference, you can use the constant reference as the function parameter.
2. Use reference parameters to return additional information
When you want a function to return multiple values, you can pass some of the values in by reference, and assign the corresponding return value to the reference parameter in the function.
In this example, ret can be obtained through the return value, and the actual parameter corresponding to occurs can be modified through the nature of the reference.
3. Array reference arguments
4. The parameters of the main function
main(int argc, char *argv[])
argc: record the number of strings in the array
argv: a two-dimensional array of characters, that is, an array of strings
5. Ellipsis parameter
6.3 Return type and return statement
1. The return statement in the loop must be followed by a return statement after the end of the loop.
Many compilers can't find this error, so you need to pay more attention.
2. Do not return references or pointers to local objects
After the function is completed, the storage space occupied by the local object is also released. Therefore, function termination means that references to local variables will point to areas of memory that are no longer valid.
3. The return value of the list initialization
4. The return value of the main function
If the control reaches the end of the main function and there is no return statement, the compiler will implicitly insert a return statement that returns 0
6.4 Function overloading
1. The relationship between overloading and const
- Overloading cannot distinguish top-level const
- Overloading can distinguish other non-top-level const
For variables containing const and pointer, or const and reference:
- The original value can be modified by pointer or reference is the top-level const
- It is the underlying const that cannot modify the original value by pointer or reference
For variables that only contain const, they are all top-level const
6.5 Special Purpose Language Features
1. Default actual parameters
Let the actual parameters that do not use the default value appear in the front, and the actual parameters that use the default value appear in the back.
2, constexpr function, inline function
The constexpr function will be implicitly designated as an inline function, which can improve efficiency
3. Assert
- If the Boolean value in the assertion is 0, the execution of the program will be terminated.
- Need to include the header file cassert