*************
C++
topic:call functions
*************
1、为什么要用函数
In every codes, functions are the crucial parts. There are many advantages of the functions. But I introduce two of them.
The first usage of the functions is reuse. And the second usage of the functions is separating the whole progect to some modules.
![]() |
2、写一个简单的函数
Functions are easy. The first thing to write a function is naming the function. A good habbit is that each function have to realize one function.
before writing a function, follow the c++ rules of the functions.
返回类型 函数名(参数类型 参数名, 参数类型 参数名,...)
{
// 函数体
// 包含具体的实现逻辑
// 可以有变量声明、表达式、语句等
// 可能包含 return 语句用于返回值
}
int add(int a, int b)
{ // 返回类型为 int,函数名为 add,参数列表包含两个 int 类型的参数
int result = a + b; // 函数体
return result; // 返回结果
}
double calculateAverage(int a, double b)
{ // 参数类型分别为 int 和 double,返回类型为 double
return (a + b) / 2.0; // 返回计算后的平均值
}
void display(int value, bool newline = true)
{ // 参数 newline 有默认值 true
std::cout << value;
if (newline)
{ // 如果 newline 为 true
std::cout << std::endl; // 换行
}
}
Here take add function as an example. The name is add and the return type is void.
void add(int a, int b)
{
int result = a + b;
}
3、调用add函数
Introduse a new concept, which is Namespace and Class. Namespace is to organize the function names. Class is to package the methods and data.
We have two beauties who share the same name. If you call Zhang Ruonan to be your girlfriend, two of them are puzzled. Which one you are calling?
![]() |
Namespace is to distinguish two Zhang Ruonan.
namespace ElseWhere's girlfriend
{
beauty ZhangRuonan
}
namespace Your girlfriend
{
beauty ZhangRuonan
}
if you call ElseWhere:: ZhangRuonan, that is my girlfriend.
if you call Your girlfriend:: ZhangRuonan, she is your girlfriend.
class is the method.
namespace ElseWhere's girlfriend
{
class Kiss
{
kiss myLips;
};
class Hug
{
hug myArms;
};
}
how to let ZhangRuonan kiss you?
Call the function.
namespace ElseWhere's girlfriend
{
class Kiss
{
kiss myLips;
void myLips();
};
class Hug
{
hug myArms;
};
// call the function hug() from the class Hug
void hug()
{
Hug HugElseWhere;
HugElseWhere.myArms();
}
}
kiss mylips are called in the Kiss class. Hug are called out of the class Hug. Some attention pleasec. If you call the function out of the calss, make an object first. And then the object call the function. Like Zhang Ruonan and I walks in the street, I said hey, kiss me. Maybe Liu Yifei will come up and kiss me, Zhang Ruonan will annoy at me. So you will call hey, Zhang RUonan kiss me. If in the same class, it likes I and Zhang Ruonan are in home and only two of us are in the room. When I say hey kiss me, Zhang will come up and give me a sweety kiss kiss.
namespace ElseWhere // namespace 名字可以自定义
{
class kiss // class 名字可以自定义
{
kiss myLips(beauty Zhang, handsome ElseWhere) // 函数的名字是myLips
{
kiss myLips// 在这里写函数的具体内容
}
// 在kiss内部调用myLips函数
void home(void) // 在家里让章若楠亲我
{
myLips(); // 调用myLips函数
}
};
// 在kiss外部调用myLips函数
class street // 在街道上让章若楠亲我
{
public: // 公有成员变量和函数
kiss myKiss; // 创建一个kiss类的对象myKiss
myKiss.myLips(); // 调用myKiss对象的myLips函数
};
}
4、总结
That is main usage of the skills in c++, the rest of the skills are mathematic tricks.