我的 C++ 类(class)在秋季开始,在那之前我尝试学习一些知识,以便在我上学时有一个优势。
无论如何,我正处于学习用户定义变量的阶段,但我并不真正理解这与仅声明变量之间的区别。我知道我在这里缺少一些东西,我希望有人能为我解决这个问题。更具体地说,这之间有什么区别:
#include<iostream>
#include <cmath>
void myfun(int);
int main()
{
using namespace std;
myfun(45);
system("pause");
return 0;
}
void myfun(int x)
{
using namespace std;
cout << "My favorite number is " << x << endl;
}
还有这个:
#include<iostream>
#include <cmath>
int main()
{
using namespace std;
int x = 45;
cout << "My favorite number is " << x << endl;
system("pause");
return 0;
}
我看不出有什么不同,前者看起来更像是一个过程。
最佳答案
想想如何输出:
My favorite number is 13
My favorite number is 23
My favorite number is 11
My favorite number is 25
My favorite number is 77
有了函数,就容易多了,但如果只使用变量就很笨拙了。
关于c++ - 用户定义的函数和声明变量有什么区别? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30990284/