C++ Function declaration and definition and their differences

In the previous post we have seen what is function and how to call a function here we will see what does function declaration and function definition means and their differences.

Link What is function

Function declaration and and definition are very much related,whenever we declare a function we must also define it or we can just define the function without declaring it.The difference between a function declaration and definition is given below.

Function declaration

A function is said to be declared only when a function is given a name and has the variables name which it will receives and also the return type.Note in function declaration there will be no body of the function meaning there will be no braces { } with codes inside it.Some examples of function declaration is given below.

int func(); //A function declaration

string NewSTring( string st); //function declaration

Here you can see in a function declaration a semi-colon is used just after the closing bracket.Also note we cannot call a function which is just declared in out program.Look at the example below.

#include <iostream>

using namespace std ;

string name() ;   //function declaration

int add(int,int) ;   /* A declaration of function accepting two int type values */

int main( )
{
string str ;

/*str=name(); error!! you cannot call this function */

cin.get() ;
return 0 ;
}

Since declaring a function does not have a body or codes to execute we cannot call it in our program so,calling the name() function above will give you an error.

The use of declaring a function is only to tell the compiler that such function if with it’s body exists somewhere then you can call it but if it’s body does not exist then give an error.

In the second function declaration “int add(int,int) ;” the variables name of the two int type accepted by the function are not mentioned but it is acceptable.You can check this link for more information why is declaring or defining a function without the parameters names acceptable?


Function definition

A function is said to be defined only when it has a body:meaning a function will have codes and statements inside braces next to the function name.So,in function definition braces will replace the semi-colon.

int func()
{
//your code here
}

string NewSTring( string st)
{
//your code here
}

The program below shows how to define a function.

#include <iostream>

using namespace std;

string name() ;   //function declaration

int main()
{
string str ;

str=name();

cout<< str ;

cin.get();
return 0;
}

string name( )  ///Function definition
{
return “Returning a string” ;
} 

The function name() is declared before main() function but the definition is given after the main(),this is allowed,more about this is discuss below.

**NOTE:a function definition has a codes that is executed whenever it is called so the compiler must allocate a memory for the code.In other words we can say a memory is allocated for a function only when it is defined.

A variable declaration has a different concept with function declaration.If we give a variable a name and a type without initializing it then the variable is defined not declared.It is because just by seeing the name and the type the compiler has enough information on how much space to allocate for that variable.If you want to declare a variable without defining it then you must use the keyword extern.This keyword tells the compiler that this variable is defined somewhere else.Declaring a variable using the keyword “extern” in a particular file becomes necessary when you want to use that variable in that file but the variable is defined in some other files.


Where to declare and define a function?

One thing you should know is a function can be either declared before the main() function and then defined after the main() or you can just define the function before the main function without declaring it.Consider the code below

#include <iostream>

using namespace std;

void func1( ); //Function declaration works fine

int func2( ) //Function define wihtout declaration
{
return 2 ;
}

int main( )
{
func1() ;
cout<< func2() ;

/*func3() ; error function not declared*/

cin.get() ;
return 0;
}

void func1()   //func1() define after main
{
cout<< “func1” ;
}

In the program above func1() is declared before the main() but defined after it and the func2() is defined before main() without requiring any declaration.Both the functions work fine this is because by defining the function func2() before the main() we have also declared it.Defining a function before main() is preferable because it removes the need for extra declaration.

 

If we define a function after the main() without declaring it before the main() and if we call the function in the main() then the compiler will complain.For instance,

int main()
{
func() ;

cin.get() ;
return0 ;
}

void func( ){ } //define after main()

Here,you will get an error “Error:func( ) was not declared in this scope“ why? When the compiler compiles the file it comes across the function name func() in the main() for the first time so, the compiler does not know what this function return type is or the type of value it will accept.Simply put the compiler has no information about this function at all.So,calling it is an error.

However, if we had declared the function before main() the compiler comes across it first and take into account that such function exists somewhere and so when we call it later in main() the compiler searches for the function and calls it accordingly.

void func( );

int main()
{
func() ; //ok fine

cin.get() ;
return0 ;
}

void func( ){ } 

Now the program compiles.

So,there are couple of rules which we must follow when declaring or defining a function.

i)Define the function before the main() without declaring it or

ii)Declare the function before main() and define it before or after main().
 




Leave a Reply

Your email address will not be published. Required fields are marked *