fb popup

/* remove if it's exist in your template */

Types of Functions

These are of 4 types:-

I.Functions with no arguments(parameters) and no return type:

#include<stdio.h>
void sum();
void main()
{
sum(); //calling function
}

void sum() //called function
{
int a,b,result;
printf("Enter 2 values:");
scanf("%d%d",&a,&b);
result=a+b;
printf("Sum of %d and %d is %d",a,b,result);
}
Output:-  

Functions with no arguments(parameters) and no return type

Explanation:-

1.As you see here we do not pass any parameters to the called function.

 2.The return type is "void" which means there is no returned value from called function to the calling ones as the result is printed in the called function itself. 

  3.When you call sum both a and b are added and the result is printed.

II.Functions with no arguments(parameters) and has return type:

#include<stdio.h>
float sum();
void main()
{
float result;
result=sum(); <--------------------------- span="">
printf("%f",result);<---------------------<--------------------------- span="">|
}                                         |
                                          |
float sum() //called function             |
{                                         |
float a,b;                                |a+b sent to calling function which 
printf("Enter 2 values:");                |is then stored in result variable.
scanf("%f%f",&a,&b);                      |
printf("Sum of %f and %f is ",a,b);       |
return(a+b);------------------------------|
}
Output:-  

Functions with no arguments(parameters) and has return type

Explanation:-

1.Now we called a function which has return type as float.So, called function must send a float value to the calling function.

2.both a and b are added and then sent for printing the result in the main().

III.Functions with arguments(parameters) and has no return type:

Program:-
#include<stdio.h>
void sum(int,int);  
void main()
{
int a,b;
printf("Enter 2 values:");
scanf("%d%d",&a,&b);
sum(a,b);     //calling function
}

void sum(int a,int b) //called function
{
int result=a+b;
printf("Sum of %d and %d is %d",a,b,result);
}
Output:-

Functions with arguments(parameters) and has no return type
 Explanation:-

1.Here we sent two values a and b to called function.

2.As there is no return type there is no way of getting the result to main function.So,the values are printed in called function itself.

IV.Functions with arguments(parameters) and return type:

Program:-
#include<stdio.h>
int sum(int,int);
void main()
{
int a,b,result;
printf("Enter 2 values:");
scanf("%d%d",&a,&b);
result=sum(a,b);
printf("Sum of %d and %d is %d",a,b,result);
}

int sum(int a,int b)
{
int c;
c=a+b;
return(c);
}
Output:-  


Functions with arguments(parameters) and return type

Explanation:-

1.Here we sent values of a and b to called function.

 2.The called function returns the sum value(c=a+b) to calling function.

No comments:

Post a Comment