C++ 11. Function - (function, definition, call, value transfer, common function style, function declaration, function sub file writing)

catalogue

Overview of functions:

Functions:

The function definition is divided into five steps:

Function call:

Value transfer:

Common function styles:

Declaration of function

Function sub file writing

Overview of functions:

A large program is generally divided into several program blocks, and each module realizes specific functions

Functions:

Encapsulate a piece of frequently used code to reduce repeated code

The function definition is divided into five steps:

1. Return value type: a function can return a value in the function definition
2. Function name: name the function
3. Parameter list: the data passed in when using this function
4. Function body statement: the code in curly braces and the statement executed in the function
5.return expression: linked to the return value type. After the function is executed, the corresponding data is returned
Syntax:
Return value type function name (parameter list)
{
Function body statement

return expression
}

#include <stdio.h>
#include <iostream>
using namespace std;
int add(int num1,int num2)//The addition function realizes the addition of two integers and returns the added result
{
	int sum = num1 + num2;
	return sum;
}

Function call:

Use the called function
Syntax: function name (parameter)

//Define addition function: add two integers and return the added result
//When defining a function, num1 and num2 have no real data, but are just formal parameters. Abbreviation formal parameter
int add(int num1,int num2)
{
	int sum = num1 + num2;
	return sum;
}
int main()
{
	//main function call add
	int a = 10;
	int b = 20;
	//Function call syntax: function name (parameter)
	//a. B is called the actual parameter for short
	//When a function is called, the value of the argument is passed to the formal parameter
	int c = add(a, b);
	cout << "c=" << c << endl;
	int d = add(10, 10);
	cout << "d=" << d << endl;
	system("pause");
	return 0;
}

Value transfer:

It is the real parameter that passes the value to the formal parameter when the function is called
When a value is passed, any change in the formal parameter does not affect the argument

//Define the function to realize the exchange of two numbers
//If the function does not need to return a value, you can write void when declaring
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 the return value is not needed, you can not write return 
}
int main()
{
	int a = 10;
	int b = 20;//When we pass values, the formal parameters of the function change without affecting the arguments
	swap(a, b);
	system("pause");
	return 0;
}

Common function styles:

1. No reference and no return
2. No return with reference
3. Return without participation
4. Participation and return

//1. No reference, no return No return value void
void test01()
{
	cout << "this is test01" << endl;	
}
//2. No return with reference
void test02(int a)
{
	cout << "this is test02  a =" << a << endl;
}
//3. Return without participation
int test03()
{
	cout << "this is test03"  << " ";
	return 1000;
}
//4. Participation and return
int test04(int a)
{
	cout << "this is test04 a=" << a << " ";
	return a;
}
int main()
{
	//1. Function call without parameters and return
	test01();
	//2. Function call with parameters and without return
	test02(100);
	//3. No parameter return function call. int variable name to receive the return value
	int num = test03();
	cout << "num=" << num << endl;
	//4. Function call with parameters and return
	int num1 = test04(20);
	cout << "num1=" << num1 << endl;
	system("pause");
	return 0;
}

Declaration of function

Function: tell the editor the function noun and how to call the function. The actual body of a function can be defined separately
Note: a function can be declared multiple times, but a function can only be defined once

//Comparison function to compare two integer numbers and return a larger value
//Declaration of function
int max(int a, int b);//To tell the compiler in advance of the existence, you can use function declarations. In this case, you can put int max() after int main()
int max(int a, int b); 
int max(int a, int b);//A function can be declared multiple times, but a function can only be defined once

//Definition of function
int max(int a,int b)
{
	return a > b ? a: b;//ternary operator 	
}
int main()
{
	int a = 12;
	int b = 23;
	cout << max(a, b) << endl;
	system("pause");
	return 0;
}

Function sub file writing

Function: make the code structure clearer
Four steps of function sub file writing
1. Create a suffix of h header file
2. Create a suffix of cpp source file
3. Write the function declaration in the header file
4. Write the definition of the function in the source file

swap.h file

#include <iostream>
using namespace std;
//Declaration of function
void swap(int num1, int num2);

swap.cpp file

#include "swap.h" / / Association h documents and cpp file

//Definition of function
void swap(int num1, int num2)
{
	int temp = num1;
	num1 = num2;
	num2 = temp;

	cout << "num1=" << num1 << endl;  //Also need to be in swap H add #include < iostream > using namespace STD to the file;
	cout << "num2=" << num2 << endl;
}

main.cpp

#include "swap.h" / / you can use swap(a, b);
// "swap.h" can use its own header file with double quotation marks
int main()
{
	int a = 10;
	int b = 20;//When we pass values, the formal parameters of the function change without affecting the arguments
	swap(a, b);
	system("pause");
	return 0;

Keywords: C++

Added by Andy-H on Mon, 03 Jan 2022 17:39:50 +0200