声明一个函数? [英] Declaring a function?

查看:43
本文介绍了声明一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C 编写一些我的第一个练习.早些时候,我试图在 main 中声明一个简单的函数,但它出现了一个错误:此处不允许定义函数".但我认为一个函数可以在 main 内部或外部声明,唯一的区别是作用域??我也在这里读到其他人在 main 中编写函数,那为什么不让我这样做呢?谢谢

in C I am writing some of my very first exercises. Earlier on, I tried to declare a simple function inside of main and it comes with an error: "function definition is not allowed here". But I thought a function could be declared inside of main or outside, the only difference being the scope?? I have also read, in here, of other people writing functions inside of main, so why won't it let me do it? thanks

推荐答案

你可以在另一个函数中声明一个函数:

You can declare a function inside another function:

int main(void) {
    int foo(int); // declaration
    ...
}

但是你不能在另一个函数中定义一个函数:

But you can't define a function inside another function:

int main(void) {
    // Doesn't work.
    int foo(int x) {
        return x * 2;
    }
    ...
}

另外,在其他函数中声明函数是一件非常不寻常的事情,基本上从来没有必要.

Also, declaring functions inside other functions is a really unusual thing to do, and essentially never necessary.

这篇关于声明一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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