Sunday, November 16, 2014

3.2.2 TYPES OF FUNCTIONS


ü  Function with arguments and return type

ü  Functions with arguments but no return type

ü  Functions without arguments but return type

ü  Functions without arguments and no return type

 

§  Function with arguments and return type

o   In this type of functions, the called function receives the data from the calling function.

o   After the processing, the called function returns the value to the calling function.

 

Example:

            /* With arguments and return type */

#include<stdio.h>

#include<conio.h>

int addsum(int x,int y);                       //Declaration

void main()

{

  int a, b, result;

  clrscr();

              printf("\nEnter the value of a and b:\n");

              scanf("%d%d",&a,&b);

              result=addsum(a,b);                          //Call

              printf("\nSum=%d”,result);

              getch();

            }

           

int addsum(int a,int b)                        //Definition

            {

              int sum=0;

              sum=x+y;

              return sum;

            }

 

§  Functions with arguments but no return type

o   In this type of functions, the called function receives the data from the calling function for computation.

o   After the computation, the called function does not return any value back to the calling function; it prints that data in its scope only.

 

Example:

            /* With arguments and no return type */

#include<stdio.h>

#include<conio.h>

void addsum(int x,int y);                    //Declaration

void main()

{

  int a, b, result;

  clrscr();

              printf("\nEnter the value of a and b:\n");

              scanf("%d%d",&a,&b);

              addsum(a,b);                                     //Call

              getch();

            }

           

void addsum(int a,int b)                     //Definition

            {

              int sum=0;

              sum=x+y;

              printf(“\nSum=%d”, sum);

            }

 

§  Functions without arguments but return type

o   In this type of function, the called function does not receive any data from the calling function for processing.

o   After the processing, the called function returns the computed value to the calling function.

Example:

/* Without arguments but return type */

#include<stdio.h>

#include<conio.h>

int addsum();                                       //Declaration

void main()

{

  int result;

  clrscr();

  result=addsum();                               //Call

       printf("\nSum=%d”,result);

  getch();

            }

           

int addsum()                                        //Definition

{

              int x,y,sum=0;

              printf(“\nEnter the value of x and y:\n”);

  scanf("%d%d",&x,&y);

  sum=x+y;

        return sum;

            }

 

§  Functions without arguments and no return type

o   In this type of function, the called function does not receive any data from the calling function for processing.

o   After processing, the called function does not return any data to the calling function.

Hence, there is no data transfer between the calling function and the called function.

Example:

            /* With no arguments and no return type */

#include<stdio.h>

#include<conio.h>

void addsum();                                    //Declaration

void main()

{

  clrscr();

              addsum(a,b);                                     //Call

              getch();

            }

           

void addsum()                                     //Definition

            {

              int x,y,sum=0;

              printf(“\nEnter the value of x and y:\n”);

  scanf("%d%d",&x,&y);

  sum=x+y;

        printf("\nSum=%d”,sum);

            }

 

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.