*************
C++
topic:functions
*************
functions are crucial in the programs. Functions are the basic units which realize the function. A basic structure of the code has some modules.
![]() |
Program starts from main (). mian - function has rights calling other functions. BTW, return 0 can be hidden in the main function. return hides only in the main function; But for a readable code, keep return 0; in the main code. Good habbits improve skills indefinitely.
int main() {
// 主程序代码写在这里
return 0; // 成功结束程序
}
diy function:
diy functions are the perfect example of a man's brilliance. A good function seems so sexy in the program.
return sth. |
|
return nothong |
|
inline function:
The inline functions are used to improve a program performs by expanding the body of the function directly to the call. Iron Man has the ability to summon his armour at any time. Inline functions are comparable to armour.
![]() |
|
overload function:
C++ lets you write the same function name in different ways, depending on the type and number of arguments that are passed in.
void print(int num) {
std::cout << "Integer: " << num << std::endl;
}
void print(std::string str) {
std::cout << "String: " << str << std::endl;
}
Write a program to calculate the summation of two integers.
#include <iostream>
using namespace std;
int main() {
int num1, num2, sum;
cout << "Enter the first integer: ";
cin >> num1;
cout << "Enter the second integer: ";
cin >> num2;
sum = num1 + num2;
cout << "The sum of " << num1 << " and " << num2 << " is " << sum << endl;
return 0;
}
remenber the basic function form is as follow.
返回类型 函数名(参数类型 参数名, 参数类型 参数名, ...)
{
// 函数体
return 返回值; // 如果返回类型不是 void,则需要返回一个值
}
Then the summation function is written.
int add(int a, int b)
{
int sum = a + b;
return sum;
}
then add mian function and head code.
#include <iostream>
using namespace std;
// 求和函数
int add(int num1, int num2) {
return num1 + num2;
}
int main() {
int num1, num2;
// 提示用户输入
cout << "Enter first integer: ";
cin >> num1;
cout << "Enter second integer: ";
cin >> num2;
// 调用求和函数
int sum = add(num1, num2);
// 输出结果
cout << "Sum: " << sum << endl;
return 0;
}
to improve the performs of the code, add inline function.
#include <iostream>
using namespace std;
// 内联求和函数
inline int add(int num1, int num2) {
return num1 + num2;
}
int main() {
int num1, num2;
cout << "Enter first integer: ";
cin >> num1;
cout << "Enter second integer: ";
cin >> num2;
int sum = add(num1, num2);
cout << "Sum: " << sum << endl;
return 0;
}
put overload function in the program.
#include <iostream>
using namespace std;
// 内联求和函数
inline int add(int a, int b) {
return a + b;
}
// 重载函数:带字符串描述
void add(int a, int b, const string& description) {
int sum = a + b;
cout << "Adding " << a << " and " << b << " (" << description << "): " << sum << endl;
}
int main() {
int num1, num2;
cout << "Enter first integer: ";
cin >> num1;
cout << "Enter second integer: ";
cin >> num2;
// 使用内联求和函数
int sum = add(num1, num2);
cout << "Sum: " << sum << endl;
// 使用重载函数
add(num1, num2, "Example of overloaded function");
return 0;
}
here is the return.
Have a good weekend.