用户定义的函数和声明变量之间的区别是什么?C++

0 c++ variables

我的C++课程在秋季开始,之前我正在尝试做一些学习,这样我上学的时候就能站稳脚跟.

无论如何,我正处于我正在学习用户定义变量的地步,我真的不明白它与仅仅声明一个变量之间的区别.我知道我在这里缺少一些东西,我希望有人可以为我清除这一点.更具体地说,这是什么区别:

 #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;

}
Run Code Online (Sandbox Code Playgroud)

还有这个:

#include<iostream>
#include <cmath>

int main()

{
    using namespace std;

    int x = 45;

    cout << "My favorite number is " << x << endl;

    system("pause");
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

我没有看到差异,前者看起来更像是一个过程.

dig*_*ber 5

想想你如何输出这个:

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
Run Code Online (Sandbox Code Playgroud)

使用函数,它更容易,但如果您只使用变量则非常笨拙.