6 function
6.1 general
Function: encapsulate a piece of frequently used code to reduce repeated code
A large program is generally divided into several program blocks, and each module realizes specific functions.
6.2 definition of function
The definition of a function generally consists of five steps:
1. Return value type
2. Function name
3. Parameter table column
4. Function body statement
5. return expression
Syntax:
Return value type function name (parameter list) { Function body statement return expression }
- Return value type: a function can return a value. In function definition
- Function name: give the function a name
- Parameter list: the data passed in when using this function
- Function body statement: the code in curly braces and the statement to be executed in the function
- Return expression: linked to the return value type. After the function is executed, the corresponding data is returned
Example: define an addition function to add two numbers
//Function definition int add(int num1, int num2) { int sum = num1 + num2; return sum; }
6.3 function call
Function: use defined functions
Syntax: function name (parameter)
Example:
//Function definition int add(int num1, int num2) //Num1 and num2 in the definition are called formal parameters, or formal parameters for short { int sum = num1 + num2; return sum; } int main() { int a = 10; int b = 10; //Call the add function int sum = add(a, b);//a and b at the time of calling are called actual parameters, which are referred to as actual parameters for short cout << "sum = " << sum << endl; a = 100; b = 100; sum = add(a, b); cout << "sum = " << sum << endl; system("pause"); return 0; }
Summary: the parentheses in the function definition are called formal parameters, and the parameters passed in during function call are called arguments
6.4 value transfer
- The so-called value passing means that the real parameter passes the value to the formal parameter when the function is called
- When a value is passed, if a formal parameter occurs, the argument is not affected
Example:
void swap(int num1, int num2) { cout << "Before exchange:" << endl; cout << "num1 = " << num1 << endl; cout << "num2 = " << num2 << endl; int temp = num1; num1 = num2; num2 = temp; cout << "After exchange:" << endl; cout << "num1 = " << num1 << endl; cout << "num2 = " << num2 << endl; //return ; When a function is declared, there is no need to return a value. You can not write return } int main() { int a = 10; int b = 20; swap(a, b); cout << "mian Medium a = " << a << endl; cout << "mian Medium b = " << b << endl; system("pause"); return 0; }
Summary: formal parameters cannot modify arguments when passing values
6.5 common styles of functions
There are four common function styles
- No reference, no return
- There is no return
- Return without participation
- Participation and return
Example:
//Common styles for functions //1. No reference, no return void test01() { //void a = 10; // No type. Cannot create variable because memory cannot be allocated cout << "this is test01" << endl; //test01(); function call } //2. There is no return void test02(int a) { cout << "this is test02" << endl; cout << "a = " << a << endl; } //3. Return without participation int test03() { cout << "this is test03 " << endl; return 10; } //4. Participation and return int test04(int a, int b) { cout << "this is test04 " << endl; int sum = a + b; return sum; }
6.6 function declaration
Function: tell the compiler the function name and how to call the function. The actual body of a function can be defined separately.
- A function can be declared multiple times, but a function can only be defined once
Example:
//You can declare multiple times and define only once //statement int max(int a, int b); int max(int a, int b); //definition int max(int a, int b) { return a > b ? a : b; } int main() { int a = 100; int b = 200; cout << max(a, b) << endl; system("pause"); return 0; }
6.7 function sub file preparation
Function: make the code structure clearer
Function sub file writing generally has four steps
- Create a header file with the suffix. h
- Create a source file with the suffix. cpp
- Write the declaration of the function in the header file
- Write the definition of the function in the source file
Example:
//swap.h file #include<iostream> using namespace std; //A function declaration that implements the exchange of two numbers void swap(int a, int b);
//swap.cpp file #include "swap.h" void swap(int a, int b) { int temp = a; a = b; b = temp; cout << "a = " << a << endl; cout << "b = " << b << endl; }
//main function file #include "swap.h" int main() { int a = 100; int b = 200; swap(a, b); system("pause"); return 0; }
come on.
thank!
strive!