Function Prototypes in C++

Introduction:
  • The functions are classified into the following types depending on whether the arguments (parameters) are present or not and whether the value is returned or not
  • These are also called function prototypes.
  • The following are the function prototypes.
                  i).   Function with no arguments and no return value.
                  ii).  Function with arguments and no return value.
                  iii). Function with arguments and with the return value.
                  iv). Function with no arguments and with the return value.
1. Function with no arguments and no return value:
  • The called program does not receive any data from the calling program and does not send back value to the calling program.
  • This function is used to perform the only operation they act independently (They read data values and print results in the same block.
Syntax:
Example: Addition of two numbers

#include<iostream.h>

#include<conio.h>

int add();//fun declaration

int a,b;

void main()

{

clrscr();

cout<<"Enter two values: ";

cin>>a>>b;

add();//func calling

getch();

}

int add() //fun definition

{

int c;

c=a+b;

cout<<"Sum is "<<c;

}

Output:
2. Function with arguments and no return value:
  • The called program receives some data from the calling program and does not send back any values to the calling program. (One-way communication).

Syntax:
Example: Addition of two numbers

#include<iostream.h>

#include<conio.h>

int add(int,int);//fun declaration

void main()

{

int a,b,c;

clrscr();

cout<<"Enter two values: ";

cin>>a>>b;

add(a,b);//func calling

getch();

}

int add(int x,int y) //fun definition

{

int z;

z=x+y;

cout<<"\nSum is "<<z;

}

Output:
3. Function with arguments and with return statement:
  • The called program receives some data from the calling program and sends back any values to the calling program. (Two-way communication)

Syntax:
Example: Addition of two numbers

#include<iostream.h>

#include<conio.h>

int add(int,int);//fun declaration

void main()

{

int a,b,c;

clrscr();

cout<<"Enter two values: ";

cin>>a>>b;

c=add(a,b);//func calling

cout<<"\nResult is ....."<<c;

getch();

}

int add(int x,int y) //fun definition

{

int z;

z=x+y;

return z;

}

Output:
4. Function with no arguments and with return value:
  • The calling program cannot pass any arguments to the called program but, the called program may send some return value to the calling program.
Syntax:
Example: Addition of two numbers

#include<iostream.h>

#include<conio.h>

int add();//fun declaration

int a,b;

void main()

{

clrscr();

cout<<"Enter two values: ";

cin>>a>>b;

cout<<"\n Result is...."<<add();//func calling

getch();

}

int add() //fun definition

{

int c;

c=a+b;

return c;

}

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me