Functions in c
A function is a set of statements that takes input, do some specific computation and produces output.Function Declaration
Function declaration tells compiler about number of parameters function takes, data-types of parameters and return type of function.
Parameter Passing to functions
The parameters passed to function are called actual parameters ,and the parameters received by function are called formal parameters.
The parameters passed to function are called actual parameters ,and the parameters received by function are called formal parameters.
There are two most popular ways to pass parameters.
Pass by Value: In this parameter passing
method, values of actual parameters are copied to function’s formal
parameters and the two types of parameters are stored in different
memory locations. Pass by Reference Both actual and formal parameters refer to same locations, so any changes made inside the function are actually reflected in actual parameters of caller.
Types of Functions in C
- Function with no argument and no Return value.
- Function with argument and No Return value.
- Function with no argument and Return value.
- Function with argument and Return value
Type 1 Program:
Function with no argument and no return value
// program to illustrate Function with no argument and no Return value.
#include<stdio.h>#define pf printf
#define sf scanf
void sum (void); // prototype or function declaration
int main()
{
sum(); //calling funtion used parameters are called actual arguments
return 0;
}
void sum(void) // called function used parameters are called formal arguments
{
int a, b ,r;
pf("\n Enter a nd b values : ");
sf("%d %d",&a,&b);
r=a+b;
pf("\n sum is %d",r);
}
output:
Type 2 Program:
Function with argument and no return value
#include<stdio.h>
#define pf printf
#define sf scanf
void sum (int , int); // prototype or function declaration
int main()
{
int a, b;
pf("\n Enter a nd b values : ");
sf("%d %d",&a,&b);
sum(a,b); //calling function ,used parameters are called actual arguments
return 0;
}
void sum(int p , int q) // called function ,used parameters are called formal arguments
{
int r;
r=p+q;
pf("\n sum is %d",r);
}
output:
Type 3 Program:
Function with no argument and return value
// program to illustrate Function with no argument and Return value.
#include<stdio.h>#define pf printf
#define sf scanf
int sum (void); // prototype or function declaration
int main()
{
int c;
c=sum(); //calling funtion ,used parameters are called actual arguments
pf("\n sum is %d",c);
return 0;
}
int sum (void) // called function ,used parameters are called formal arguments
{
int a,b,r;
pf("\n Enter a nd b values : ");
sf("%d %d",&a,&b);
r=a+b;
return r;
}
output:
Type 4 Program:
Function with argument and return value
// program to illustrate Function with argument and Return value.
#include<stdio.h>#define pf printf
#define sf scanf
int sum (int , int); // prototype or function declaration
int main()
{
int a,b,c;
pf("\n Enter a nd b values : ");
sf("%d %d",&a,&b);
c=sum(a,b); //calling function ,used parameters are called actual arguments
pf("\n sum is %d",c);
return 0;
}
int sum (int p,int q) // called function ,used parameters are called formal arguments
{
int r;
r=p+q;
return r;
}
output:
No comments:
Post a Comment