C++ Function:Declaration and definition


A function is a sub-program.It can be made a part of the larger program by calling it in the program.Every function has a name,a return type,a type with an identifier(known as parameter) which the function will make use of and a body with codes and statements inside a braces(‘{ }‘).The syntax of a function is shown below.

void function1( )
{   ///function body starts here
  //Codes is written here
}   ///function body ends here

The above function has a name function1 and returns void which means it returns back nothing.

The syntax of a function accepting an int value and returning an int type is shown below.

Link: Integral type:Built-in Data types.

int function2(int i)
{
return i++ ;
}

The value when received by the function1 is given a name ‘i’ and also to return a value we include the statement return x ;: x is the value return by the function.In the function above the return value is an incremented value of ‘i’ .

Calling a function

If you want to use a function in your program then you have to call it first.Calling a function in a program is pretty straightforward.To call a function,simply write it’s name and if the function accepts a value(known as argument) then write the appropriate value of the argument type and if it returns a value then assign the returned value to the variable of it’s type in the calling side.The function1() below accepts nothing and returns nothing,so to call it we simply write it’s name.The function2() accepts an integer so a value 9 is pass to the function by writing it inside the bracket and the return value is assigned to ii.

Link:Function arguments and parameters.

#include &lt:iostream>

using namespace std;

void function1( )
{
  cout<< “A function1 is called” ;
}

int function2( int i)
{
  cout<< “A function2 is called ” ;
  return (i++) ;   //An incremented value of i is return here
}

int main( )
{
function1( ) ; //function1 is called and

int ii ;
ii=function2( 9 ) ; /* function2 is called and the returned value is assigned to ii */

cout<< ii ;

cin.get( ) ;
return 0 ;
}

Note:whenever we pass a value to a function,it’s type must match with the type mentioned in the function else it may give rise to an error or you may get some unexpected value.For instance,while calling the function function2 if we pass a string you will get an error but if you pass a float the decimal point value gets truncated or if a char is passed then it gets converted to it’s corresponding int value.In this way different type value can produce different result which can be very hard to rectify sometimes.





Function declaration and definition

We must declare and define a function or we can just define a 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,with it’s return type and the arguments type without including the body(the braces { } with codes inside it) .

function declaration

The program below shows some function declaration.

Link:C++ String type and it’s differences with char type

#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 the name 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 that it tells the compiler that such function if with it’s body exists somewhere you can call it but if it doesn’t then give an error.

In the second function declaration “int add(int,int) ;” the parameters(the two int type accepted by the function) names are not mentioned but it is acceptable, you can read here why? why is declaring or defining a function without the parameters names acceptable?.


 


Stop wasting time,earn money($$$) from your website-Join Now!

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.

function declaration

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” ;
}

 
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.
 

Note: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.

int i ;   ///i defined
int ii=90 ;   // ii is defined

extern int size ;   ///declared





Where to declare and defined a function?

A function can be either declared before the main function and then defined after it or just defined before the main function without declaring it.In the program below 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.So defining a function before main() is preferable because it removes the need for extra declaration.

#include <iostream>

using namespace std;

void func1( ); //Function declaration works fine

int func2( ) //Function definition works fine
{
return 2 ;
}

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

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

cin.get() ;
return 0;
}

void func1()   ///func1() definition
{
cout<< “func1” ;
}

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( ){ }

Here,you will get an error “Error:func( ) was not declared in this scope“.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 arguments it will accept.Simply put the compiler has no information about this function at all.So,calling it is an error.But 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.


 


Stop wasting time,earn money($$$) from your website-Join Now!