Function prototypes in c


Introduction:
  • The function prototype is nothing but, it is number of way to calling function and return the value in c.
  • 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
Number of ways to give user-defined functions:
                  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<stdio.h>
#include<conio.h>
void main()
{
void add_func(void);//function definition
add_func();// function call  with no arguments
getch();
}
//function definition with no return value
void add_func()
{
int a,b,c;
clrscr();
printf("\n Enter two numbers :");
scanf("%d %d",&a,&b);
c=a+b;
printf("\n Sum is %d",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<stdio.h>
#include<conio.h>
void main()
{
void add_func(int,int);//function definition
int a,b;
clrscr();
printf("Enter two values: \n");
scanf("%d %d",&a,&b);
add_func(a,b);// function call  with arguments
getch();
}
//function definition with no return value
void add_func(int x,int y)
{
int z;
z=x+y;
printf("Sum is....%d",z);
}

Output:
3. Function with arguments and with return value:
  • 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<stdio.h>
#include<conio.h>
void main()
{
int add_func(int,int);//function definition
int a,b,c;
clrscr();
printf("Enter two values: \n");
scanf("%d %d",&a,&b);
c=add_func(a,b);// function call  with arguments
printf("Result is .....%d",c);
getch();
}
//function definition with return value
int add_func(int x,int y)
{
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<stdio.h>
#include<conio.h>
void main()
{
int add_func();//function definition
int a,b,c;
clrscr();
c=add_func();// function call  with no arguments
printf("Result is .....%d",c);
getch();
}
//function definition with return value
int add_func()
{
int a,b,c;
printf("Enter two values: \n");
scanf("%d %d",&a,&b);
c=a+b;
return(c);
}

Output:


Share:

No comments:

Post a Comment

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me