Skip to main content
gamedev.net
🔒 Locked

Function defining vs. prototyping

Started by csscplayer Jul 3, 2007 at 2:32 PM 6 replies 2.8k views
Original Post
csscplayer
csscplayer
I have just learned about functions from Sam's Teach Yourself C++ in 21 days book. In their code examples they give the protoype of the function and then define it later in the code. Why would you not define the function at the beginning of the program before main begins? You could still call to it right? Thank you for the help. Also, I know this will spark debate, but if I eventually want to program games for the PC/Xbox/PS2 would I want to learn C++ or C#? I've already started C++ however, so it might not matter. Thank you.
Sevans
Sevans
If you can learn c++, you can learn c#, but going from c# to c++ would be a lot harder. So start with c++.

You prototype functions to avoid having to worry about function call order. For example, if you do not use prototyping, the following is invalid.
// in file main.cppint main(){   someFunc();};void someFunc(){};


But with a little prototyping, it becomes valid.
// in file main.cppvoid someFunc();int main(){   someFunc();};void someFunc(){};


Now for the example above you could argue, "why not just put everything above the main method then." And I would say something like, "It applies to all method calls, not just main. For instance:"
// in file main.cppvoid someFuncA(){   someFuncB();};void someFuncB(){   // do nothing};int main(){   someFuncA();};


Would also be invalid, because someFuncA is declared and defined before the definition of someFuncB. So when executing someFuncA's call to someFuncB, someFuncB is not even declared yet, therefore the computer doesn't know where to go! So prototyping all functions before defining them will solve this problem. :)

Let me know if you are still confused.

-Sevans
-Sevans
csscplayer
csscplayer
Yes, that makes perfect sense, thank you very much. I will stick with C++ then.
Oluseyi
Oluseyi
Quote:
Original post by Sevans
If you can learn c++, you can learn c#, but going from c# to c++ would be a lot harder. So start with c++.

False. If you can learn a programming language, you can learn all programming languages. And going from C# to C++ might be harder than going from C++ to C#, but going from "nothing" to C# is probably easier than going from nothing to C++.

Quote:
Original post by csscplayer
I've already started C++ however...

Stick with it.


Prototypes are useful in C++ because of the forward declaration requirement of that language (and C, too). An entity must be declared before it can be referenced, and a prototype is a function declaration. Because a function declaration contains no statements (and thus no references to other functions), a prototype resolves the order of function definition by making it irrelevant. The same applies to variables within scope (and will lead you to learn about the extern keyword).
csscplayer
csscplayer
Oluseyi, thank you also for exlaining prototyping, I see the advantages it has. Now if I could only get codeBlocks to work for me....haha. Thanks guys.
Sevans
Sevans
Thanks for the touch up on my hasty comment Oluseyi. :P
-Sevans
Oluseyi
Oluseyi
You're both welcome.

Sevans, I understand what you mean. These days, I try to take the extra couple of minutes to clarify the caveats about issues like that. The most important thing is consistency and steady progress; if a person has already started with one language, they should stick with it until they can write a good program easily, and then they can start learning other languages.

You never learn just one.
Moe
Moe
Another nice thing about prototyping your functions is that you can put your function prototypes in a .h file along with your class definitions, and then put all your definitions in a .cpp file. It makes things a lot cleaner looking when you get large classes that have many members and functions, and makes getting around your code easier. At least that is what my experience has been.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.