Reputation: 403
What's the difference between:
void function();
int main()
{......}
void function()
{......}
vs
void function()
{.......}
int main();
It seems odd to declare a function before main then define it after main when you could just declare and define it before main. Is it for aesthetic purposes? My teacher writes functions like the first example.
Upvotes: 23
Views: 35655
Reputation: 138
Note I mean to make this answer supplementary, others have already given good answers to this questions.
Note that knowing how to forward declare things in C++ becomes very important. Once you begin using header files it basically becomes mandatory. Header files will allow you to build a prototype of functions and classes/structs then define them in a corresponding .cpp file. This is a very important organizational feature of C++.
// MyClass.h
class MyClass
{
public:
MyClass(int x);
void printInt();
private:
int myInt;
};
// MyClass.cpp
MyClass::MyClass(int x)
{
MyClass::myInt = x;
}
void MyClass::printInt()
{
std::cout << MyClass::myInt;
}
Doing things this way makes it so you're not completely bound to make a huge hodgepodge of code. Especially if you're writing real programs that will have a considerably large amount of source code.
So while in the question you asked forward declaring is really just more of a preference, later on it really won't be a choice.
Upvotes: 8
Reputation: 6488
It's just for code organization purposes ("aesthetics", I guess). Without forward declarations you'd need to write every function before it's used, but you may want to write the bodies of a function in a different order for organizational purposes.
Using forward declarations also allows you to give a list of the functions defined in a file at the very top, without having to dig down through the implementations.
Forward declarations would also be necessary in the case of mutually recursive functions. Consider this (silly) example:
bool is_odd(int); // neccesary
bool is_even(int x) {
if (x == 0) {
return true;
} else {
return is_odd(x - 1);
}
}
bool is_odd(int x) {
return is_even(x - 1);
}
Upvotes: 20
Reputation: 2224
Look at this example:
int max(int num1, int num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
where, int max(int num1, int num2)
is behaving as a function.
Now, here we have a program
#include <iostream>
using namespace std;
// function declaration
int max(int num1, int num2);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}
// function returning the max between two numbers
int max(int num1, int num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
I kept max()
function along with main()
function and compiled the source code. While running final executable, it would produce the following result:
Max value is : 200
Calling a Function:
While creating a C++ function, you give a definition of what the function has to do. To use a function, you will have to call or invoke that function.
When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.
To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value.
Upvotes: 0
Reputation: 17329
Your first example is in the Top-Down style, the second in Bottom-Up style.
It's largely aesthetic, in that some people prefer one over the other.
For example, someone might prefer to get a high-level overview of the program before getting the details (top-down), while another might person might prefer to see the details first (bottom-up).
A tangible benefit of the declarations in the top-down approach is that you can more easily re-organize the function definitions without having to worry about ordering.
Upvotes: 0