C++ function prototypes?

前端 未结 3 1218
粉色の甜心
粉色の甜心 2021-01-21 00:23

Here goes newbie question number 5, but I don\'t have a teacher.. so.. anyhow here we go:

I\'m wondering if is necessary to have function prototypes at the top of the fi

相关标签:
3条回答
  • 2021-01-21 01:10

    This works when you have tiny functions. It works less well when they get long. Or when you have more than one file of code. As a matter of style, many teachers demand that even tiny applications be written with the structure that serves large applications well.

    0 讨论(0)
  • 2021-01-21 01:14

    This declares but does not define the function say_hi:

    void say_hi();
    

    This both declares and defines the function say_hi:

    void say_hi(){
        std::cout << "hi" << std::endl;
    }
    

    You can declare a function many times; you can only define it once.

    A function must be declared in the file before you can call it. A function must be defined somewhere--in the same file before or after you call it or maybe even in a different file.

    So, yes, this is perfectly fine.

    0 讨论(0)
  • 2021-01-21 01:15

    You are correct; if you define all your functions above where they are called, you don't need function prototypes. The actual function definition serves the same purpose as a separate declaration.

    0 讨论(0)
提交回复
热议问题