Loading....
Coupon Accepted Successfully!

 

Types of Functions

There are three types of functions. They are
  • Function with no arguments and no return values.
  • Functions with arguments and no return values.
  • Functions with arguments and return values.
Functions with no arguments and no return values:
  • This is the simplest function.
  • This does not receive any arguments from the calling function and does not return to the calling functions.
Example

void main()

{

int a,b;

--------

--------

message();

______

______

}

void message()

{

_______

_______

}

#include<iostream.h>

void main()

{

sum();

}

sum()

{

int i,j,k;

cin>>i>>j;

x= i + j;

cout<< "sum="<<x;

return;

}

Function with arguments and no return values

This function receives arguments from the calling function and does not return any value to the calling function.
Example

void main()

{

int a,b;

-------

-------

message (a,b);

---------

---------

}

void message(int x, int y)

{

-----------

-----------

}

 

Program

#include<iostream.h>

void main()

{

int I,j;

cin >> I >>j;

sum(i,j);

}

void sum(int x, int y)

{

int z;

z = x + y;

cout <<"sum="<<z;

}

Function with arguments and return values

  • This function receives arguments from the calling function and returns the computed value back to the calling function.
Example

void main()

{

int a,b,c;

----------

----------

c=message(a,b);

}

int message(int x,int y)

{

int value;

----------

----------

value = x +y;

return(value);

}

 

Program

#include<iostream.h>

void main()

{

int i,j,value;

cin >> i >> j;

value=sum(i,j);

cout<<"sum="<<value;

}

int sum(int x,int y)

{

int z;

z=x+y;

return(z);

}

Call by Reference

  • A function is called by call by reference means the value of the actual arguments are passed to the function through the reference variables

  • Using this type of calling we can access the actual parameters through its alias names.

  • So any change made to the formal arguments is seen in the actual arguments.

  • That is when function is working with its own arguments, it is actually working on the original data

  • The general form is

Datatype function_name (datatype1&, datatype2&.......);

Where
Datatype - valid data type such as int, float etc.
function_name - name of the defined function.
& - reference operator




Test Your Skills Now!
Take a Quiz now
Reviewer Name