Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between declaring functions before or after main()?

Tags:

c++

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.

like image 849
Jesse Martinez Avatar asked Feb 12 '14 04:02

Jesse Martinez


People also ask

Can function be declared before Main?

Declaring and defining a function at the sametime has a sort of limitation that all definition must be written before main or in a header. If you write a prototype then it's definition can be written after main, in the same file or translation unit or in the different translation unit.

Can we declare function after Main?

You have to declare the function before.

What is the correct way of declaring a function?

1. Function declaration. A function declaration is made of function keyword, followed by an obligatory function name, a list of parameters in a pair of parenthesis (para1, ..., paramN) and a pair of curly braces {...} that delimits the body code.

Why does a function need to be declared before it is used?

The main reason will be to make the compilation process as efficient as possible.


2 Answers

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);
}
like image 124
alecbz Avatar answered Sep 21 '22 20:09

alecbz


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.

like image 24
Austin J. Avatar answered Sep 19 '22 20:09

Austin J.