declaring a function

Why do sometime we declare the functions and sometime do not ? how do we know when to declare the function ? like

// function example
#include <iostream>
using namespace std;

int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}

int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}



in this program function is not declared

and the following program it is declared.


// declaring functions prototypes
#include <iostream>
using namespace std;

void odd (int a); // declared functions
void even (int a);

int main ()
{
int i;
do {
cout << "Type a number (0 to exit): ";
cin >> i;
odd (i);
} while (i!=0);
return 0;
}

void odd (int a)
{
if ((a%2)!=0) cout << "Number is odd.\n";
else even (a);
}

void even (int a)
{
if ((a%2)==0) cout << "Number is even.\n";
else odd (a);
}

Pretend yourself being a compiler: you compile the second case and how can you know what the "odd" is, if it is not told previously?

With classes it is usually called a forward declaration. No need to confuse, compiler doesn't like to solve mysteries!
Yes,but why we didn't declare the function in this example as we did in the odd and even example ?


#include <iostream>
using namespace std;

int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}

int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
Some people prefer having forward declarations before the entry point and implementation after it.
In some cases the forward declaration is needed for circular calls:
1
2
3
4
5
6
7
8
9
10
11
12
int f1();

int f2()
{
    //calls f1
}

int f1()
{
   //calls f2
}
Do you mean that when we define the function after int main() { ..}
we have to declare it before int main() .
If we define the function before int main() , we dont have to declare the function ?
More generally, a function either has to be declared or defined before it is called. The compiler just processes the code as it reads it in from the file. If it gets to the function call within int main() or any other block of code and it hasn't come across either the declaration or definition of that function, it's going to throw an error. It won't go off and try to find the function in the rest of the code. It's the programmers responsibility to make sure everything is properly declared or defined before it's called.
No, declaring a function after main requires a function prototype which tells the compiler the return type, name, and passed in values.

A function prototype is this:
<return type> <function name>(<passed in values>);

When the actual function is called the compiler already knows what it needs to know to call the function.
Defining the whole function body before main doesn't require a function prototype because the compiler already knows what will happen.
Last edited on
closed account (z05DSL3A)
eker676, your 'function protoype' is the (forward) declaration, the definition will be elsewhere in the file.

Declarations and definitions

A declaration introduces names into a translation unit or redeclares names introduced by previous declarations. A declaration specifies the interpretation and attributes of these names.

A declaration is a definition unless it declares a function without specifying the function’s body, it contains the extern specifier or a linkage-specification and neither an initializer nor a function-body, it declares a static data member in a class declaration, it is a class name declaration, or it is a typedef declaration, a using-declaration, or a using-directive.

[Example: all but one of the following are definitions:
int a; // defines a
extern const int c = 1; // defines c
int f(int x) { return x+a; } // defines f and defines x
struct S { int a; int b; }; // defines S, S::a, and S::b
struct X { // defines X
int x; // defines nonstatic data member x
static int y; // declares static data member y
X(): x(0) { } // defines a constructor of X
};
int X::y = 1; // defines X::y
enum { up, down }; // defines up and down
namespace N { int d; } // definesN and N::d
namespace N1 = N; // defines N1
X anX; // defines anX

whereas these are just declarations:
extern int a; // declares a
extern const int c; // declares c
int f(int); // declares f
struct S; // declares S
typedef int Int; // declares Int
extern X anotherX; // declares anotherX
using N::d; // declares N::d
—end example]
ISO/IEC 14882 Programming languages — C++ (cross references removed)



PS: masiht, Please use code tags.
How to: Put code into your postings
http://www.cplusplus.com/forum/articles/1624/
Topic archived. No new replies allowed.