A function is a self-contained block or a sub-program of one or more statements that perform the special task when called.
Once a function is defined and called ,it takes some data from the calling function and returns a value to the called function.
The function operates on formal arguments and sends back the result to the calling function.the return () statements performs this task.
Declaration of Function And Function Prototypes :
Function_name (arguments / parameter list)
Argument declaration ;
{
Local variable declaration ;
Statements 1;
Statements 2;
Return (value);
}
Local and global variable :
Local variable : The local variables are defined within the body of the function or the function or the block.The variable defined is local to the function or block only.Other functions can not access these variables .The compiler shows errors in case other functions try to access the variables.
Global variables : global variables are defined outside the main() function .multiple functions can use them.
THE returns STATEMENTS:
The user defined function uses return statements to return the value to the calling function . exit from the called function to the calling function is done by the use of return statements.
when a return statement is executed it always returns 1.
Write a program to display alphabets ‘A’, ’B’, ‘C’ using function .
#include<stdio.h>
#include<conio.h>
void main(){
void a(),b(),c();
a();
b();
c();
d();
}
void a()
{
printf("\nA");
}
void b() {
printf("\nB");
}
void c() {
printf("\nC");
}
void d() {
printf("\nD");
}
Output:
A
B
C
D
Write a program to calculate square of a number using user-defined function.
#include<stdio.h>
void main (){
int j=0,k=0;
void sqr();
for(j=1;j<5;j++)
sqr(j);
}
void sqr(int k){
printf("%d\n",k*k);
}
Output:
1
4
9
16
Write a program to send values to user-defined function and receive and display the return value.
#include<stdio.h>
#include<conio.h>
int main (){
int sum(int,int,int),a,b,c,s;
printf("Enter three numbers: ");
scanf("%d%d%d",&a,&b,&c);
s = sum(a,b,c);
printf("sum = %d",s);
return 0;
}
sum(int x,int y,int z)
{
return(x+y+z);
}
Output :
Enter three numbers: 4 5 6
sum = 15
Write a program to return more than one value from a user-defined function.
#include<stdio.h>
#include<conio.h>
int main (){
int x,y,add,sub,change (int*,int*,int*,int*);
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
change(&x,&y,&add,&sub);
printf("\n Addition :%d ",add);
printf("\n Substraction :%d ",sub);
return 0;
}
change (int*a,int*b,int*c,int*d)
{
*c = *a+*b;
*d = *a-*b;
}
Output:
Enter the value of x and y
23
45
Addition :68
Subtraction :-22
Write a program to display array elements in reverse order
#include<stdio.h>
#include<conio.h>
void main(){
int show(int*);
int num[] = {12,34,56,78,99,233,123};
show(&num[6]);
}
show (int*u){
int m=6;
while(m!=-1){
printf("\nnum[%d] = %d ",m,*u);
u--;
m--;
}
return (NULL);
}
Output:
num[6] = 123
num[5] = 233
num[4] = 99
num[3] = 78
num[2] = 56
num[1] = 34
num[0] = 12