从c ++中的main()调用函数 [英] Calling functions from main() in c++

查看:122
本文介绍了从c ++中的main()调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个有10个标题和10个源文件的程序。我在我的教科书中读到,函数从main调用。但是如何从main()传递数据到这么多的函数?

I came across a program with 10 header and 10 source files. I read in my text book that the functions are called from main. But how can I pass data to so many functions from main()?

推荐答案

函数不一定需要从main 。它们可以被其他函数调用。例如:

Functions don't necessarily need to called from main. They can be called by other functions. For example:

int foo(int x)
{
    return x*x;
}

int bar(int x)
{
   return foo(x) + 1;
}

int main()
{
    int a = bar(42);
    std::cout << a << std::endl;
    return 0;
}

注意 foo()从不直接从 main()调用。

这篇关于从c ++中的main()调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆