Coding Beyond Technology..

Monday, 19 September 2016

Functions

Functions are small block of codes or segments or subroutine that are used to perform some operation.

For example :

printf() - to print message or values onto the console.
scanf() - to take input from the console.
getch() - to take a character from the console.

and  many more..

Functions always have opening and closing brackets  " ( ) ".

Syntax :

  return_type function_name ( paraments )
{
       //body
}

In C and C++, there is a term called Function Prototype . A Function Prototype , can also be called Function Signature that contain return_type , function_name and the types & number of parameter it has, omitting the function's body. While defining function prototype it must be end with a semi-colon ';' .

Function prototyping also called function defining.

Syntax :
return_type function_name ( parameter_1 , parameter_2, ... ) ;

For Eg:
int add( int x , int y ); or int add( int , int ) ;

The above example tells the compiler that there exits a function( add() ) with integer( int ) return type and has two parameters both of integer(int) type , whose body has been declared some where in the program.

The main purpose of the Function Prototype is to make sure the compiler or other functions that function is exits in the program.

Suppose , there is function add() and you call it in the main() ,

//code in turbo C

case I :

#include<iostream.h>
#include<conio.h> 

int add( int x , int y ){
      int z = x + y ;
      return z ;
}

void main(){
     clrscr();
     int a = add(10,20) ;
     cout<<a;
     getch();
}

output : 30

case II :

#include<iostream.h>
#include<conio.h>

void main(){
     clrscr();
     int a = add(10,20) ;
     cout<<a;
     getch();
}

int add( int x , int y ){
      int z = x + y ;
      return z ;
}


output : error

case III :

#include<iostream.h>
#include<conio.h> 

int add(int , int);

void main(){
     clrscr();
     int a = add(10,20) ;
     cout<<a;
     getch();
}

int add( int x , int y ){
      int z = x + y ;
      return z ;
}

output : 30


Now, looking at case I , case II and case III, you came to know what is the importance of Function Prototype and where it is necessary.


Calling a function 

In the above example , you saw that add() is called inside main(). Calling a function means the flow of the program when encounters with the function call , the flow is transferred to the function where it is declared. 
Until and unless the called function's body is not completely executed the  function will not terminated.

#include<iostream.h>
#include<conio.h> 

int add( int x , int y ){
      int z = x + y ;
      cout<<"This is add() body";
      return z ; // it will return the value of z where this function is called
}

void main(){
     clrscr();
     int a = add(10,20) ; // function call
     cout<<"\n"<<a; // this line will not be executed until and unless the add() body is completely executed
     getch();
}

output : 
This is add() body
30

Return keyword

Return is used to either return the processed value by the function to where it has been called or to terminate the function forcefully or both .

case I :

#include<iostream.h>
#include<conio.h> 

int add( int x , int y ){
      int z = x + y ;
      cout<<"This is add() body";
      return z ; // it will return the value of z where this function is called
}

void main(){
     clrscr();
     int a = add(10,20) ; // function call
     cout<<"\n"<<a; // this line will not be executed until and unless the add() body is completely executed
     getch();
}

output : 
This is add() body
30

case II : 
 
#include<iostream.h>
#include<conio.h> 

int add( int x , int y ){
      int z = x + y ;
      cout<<"This is add() body I";
      return z ; // it will return the value of z where this function is called
      cout<<"\nThis is add() body II"; // this line will not be executed because in the above line there  is return keyword
}

void main(){
     clrscr();
     int a = add(10,20) ; // function call
     cout<<"\n"<<a; // this line will not be executed until and unless the add() body is completely executed
     getch();
}

output : 
This is add() body
30



Any question and query, please do comment..

till then, Happy Coding

 

Whats the Output ??

#include <iostream>

using namespace std;

class Test{

    int i; // data member

    public :

        Test(){
            cout<<"\nYou Entered Test Class.";

            i = 10;

            showValue(); // Member function
        }

        void showValue(){
            cout<<"\n i = "<<i;
        }

}obj;

main(){}


Q. Will it be an error  ? If yes why ? If no why ?

Popular Posts

Recent Posts

Unordered List

Text Widget

Powered by Blogger.