Function/Function Definition/Function Call/Function Value Transfer/Function Common Form/Function Declaration/Function File Writing
Table of contents
Second, the definition of the function
1. Brief introduction
Organize some knowledge developed in C++, so that you can consult and use it in time when you encounter similar problems later.
This section introduces that function encapsulates a piece of frequently used code to reduce repeated code. A large program is generally divided into several program blocks, and each module implements a specific function, including function/function definition/function call/ Function value passing/common form of function/function declaration/function file writing and precautions. If there are deficiencies, welcome to point out, or if you have a better method, please leave a message.
Second, the definition of the function
The definition of a function generally has 5 main steps:
- 1. Return value type
- 2. Function name
- 3. List of parameters
- 4. Function body statement
- 5. return expression
form:
返回值类型 函数名 (参数列表)
{
函数体语句
return表达式
}
illustrate:
- Return type: A function can return a value. in the function definition
- Function name: give the function a name
- Parameter list: When using this function, the data passed in
- Function body statement: the code inside curly braces, the statement that needs to be executed in the function
- return expression: linked to the return value type, after the function is executed, the corresponding data is returned
Code example:
Description: Define an addition function to add two numbers
#include <iostream>
using namespace std;
// add 函数定义
int add(int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
int main() {
cout << "3 + 5 = " << add(3, 5) << endl;
cout << "122 + 678 = " << add(122, 678) << endl;
system("pause");
return 0;
}
3. Function call
The function call is to use the defined function
Call form: `function name (parameter)`
illustrate:
- The parentheses in the function definition are called formal parameters, and the parameters passed in when the function is called are called actual parameters.
code:
#include <iostream>
using namespace std;
//函数定义
int add(int num1, int num2) //定义中的num1,num2称为形式参数,简称形参
{
int sum = num1 + num2;
return sum;
}
int main() {
int a = 10;
int b = 10;
//调用add函数
int sum = add(a, b);//调用时的a,b称为实际参数,简称实参
cout << "sum = " << sum << endl;
a = 100;
b = 100;
sum = add(a, b);
cout << "sum = " << sum << endl;
system("pause");
return 0;
}
4. Function value transfer
The so-called passing by value means that when the function is called, the actual parameter passes the value to the formal parameter.
illustrate:
- When passing by value, if the formal parameter occurs, it will not affect the actual parameter
code:
#include <iostream>
using namespace std;
void swap(int num1, int num2)
{
cout << "交换前:" << endl;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
int temp = num1;
num1 = num2;
num2 = temp;
cout << "交换后:" << endl;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
//return ; 当函数声明时候,不需要返回值,可以不写return
}
int main() {
int a = 10;
int b = 20;
swap(a, b);
cout << "mian中的 a = " << a << endl;
cout << "mian中的 b = " << b << endl;
system("pause");
return 0;
}
5. Common styles of functions
There are 4 common function styles
- 1. No participation, no return
- 2. Participation but no return
- 3. Return without participation
- 4. Participation and return
code:
//函数常见样式
//1、 无参无返
void test01()
{
//void a = 10; //无类型不可以创建变量,原因无法分配内存
cout << "this is test01" << endl;
//test01(); 函数调用
}
//2、 有参无返
void test02(int a)
{
cout << "this is test02" << endl;
cout << "a = " << a << endl;
}
//3、无参有返
int test03()
{
cout << "this is test03 " << endl;
return 10;
}
//4、有参有返
int test04(int a, int b)
{
cout << "this is test04 " << endl;
int sum = a + b;
return sum;
}
6. Function declaration
Function declaration: Tell the compiler the name of the function and how to call the function. The actual body of the function can be defined separately.
illustrate:
- A function can be declared multiple times
- But a function can only be defined once
#include <iostream>
using namespace std;
//声明可以多次,定义只能一次
//声明
int max(int a, int b);
int max(int a, int b);
//定义
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;
}
Seven, function file writing
Writing functions in separate files: making the code structure clearer
There are generally 4 steps to write function files
- 1. Create a header file with a suffix of .h
- 2. Create a source file with the suffix .cpp
- 3. Write the declaration of the function in the header file
- 4. Write the definition of the function in the source file
code:
1)swap.h
//swap.h文件
#include<iostream>
using namespace std;
//实现两个数字交换的函数声明
void swap(int a, int b);
2、swap.cpp
// 引入swap.h头文件
#include "swap.h"
// 实现定义函数
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
3)main.cpp
//main函数文件
// 引入函数头文件
#include "swap.h"
int main() {
int a = 100;
int b = 200;
// 调用函数
swap(a, b);
system("pause");
return 0;
}
4) Code file structure
5) Code running results