« Previous                                                                  Next Chapter »
Functions
A function is an independent subprogram which won’t be developed in main, which will be developed outside of the main and which can be used as many number of times as we required.

If we are not working with the concept of functions, we have the fallowing disadvantages

  • Length of the program will be increased enormously.
  • Repeated code will be available in the program, which can be called as redundant code.
  • Because of the redundant code the problem inconsistency will be raised.
  • We can’t reuse the pre-existed code instead we have to redevelop the code again.
  • There will not be the concept of modular programming.

If we work with the concept of functions, we have the fallowing advantages

  • Length of the program will be decreased enormously.
  • No read to produce the code repeatedly if we require it more than one time.
  • Re usability will be there.
  • No inconsistent will be there in the code.
  • There will be the concept of modular programming.

This is the strongest feature in C language. With this feature C became procedure oriented programming language. A function can also be called as independent module, as we can develop different modules with the concept of modular programming.

The functions can be categorized in two ways based on their development. They are

  1. Predefined function or Library function or Built in function.
  2. User defined function.

1. Predefined function
The function which the developers of the programming language already developed and which was given as a gift to the user and which is available in header file can be called as predefined or library function.

Ex: strlen() strupr() etc.


2. User defined function
The function, which will be developed by the user by writing his own code, can be called as user defined function.

May be Predefined or User defined functions were developed or can be developed in four different formats. They are

1. Function with parameter and with return value               
In this format The function will receive parameter and it performs some operation on it finally it sends some result as return value. 
Ex: int strlen (char*);

2. Function with parameter and without return value
In this format user has to send some parameter and the function will perform some operations on the data in that parameter and it wont send any result back.
Ex: textcolor(int);

3. Function without parameter and without return value
In this format user need not send any parameter to the function but it performs some operation and it won’t return any result.
Ex: clrscr();

4. Function without parameter and with return value
In this format user need not send any parameter to the function but the function will perform some operation and it sends some result.

            #include<stdio.h>
            #include<conio.h>
             int sum(int,int);
             main()                            // Calling function
            {
             int a,b,c;
             clrscr();
             printf(“Enter the values of a,b”);
             scanf(“%d%d”,&a,&b);
             c=sum(a,b);                  //  Function call
             printf(“The sum is %d\n”,c);
             }              
              int sum(int x,int y)     // Called function, Function head

             {
              return x+y;
              }

a, b are   actual parameters & x,y are formal or dummy arguments.

1. Prototype

Prototype is nothing but giving the complete information about the function that I am going to develop which includes the name of the function and the number of parameters and the type of the parameters if it returns any value what type of value it returns etc. In the above example int sum(int,int) is prototype.

2. Calling function

The function which activates the other function by making use of it in it can be called as calling function. In the above example main is the calling function as it is activating all the functions printf() scanf() clrscr() sum()

3. Called function

The function which will be activated by other function or which will be used by other function can be called as called function. In the above example all the four functions printf() scanf() sum() clrscr() are called functions.

4. Function call

The procedure we have to fallow to make activate any function by sending necessary parameters and by receiving the return value if it returns can be called as function call.
In the above example  c=sum(a,b) is function call.

5. Function head

The name of the called function along with its parameters and with its return value can be called as function head. In the above example int sum(int x,int y) can be called as function head.

6. Actual parameters

The variables whose values has to passed from calling function to called function through function call can be called as actual parameters. In the above example the variables a,b are actual parameters.
 
7. Formal or dummy parameters

The variables which receives the values in the called function  that  are coming from calling functions actual parameters can be called as formal or dummy parameters. In the above example the variables x,y are formal or dummy parameters.

We can activate the functions by using two different techniques. They are

1. Call by value

If we are activating the called function by sending the actual parameters values through function call such a function activating mechanism can be called as call by value. In the above example the function sum() was activated using call by value.

2. Call by reference or call by address

If we are activating the function by sending the address of the actual parameters address such a function activating mechanism can be called as call by reference or call by address. In the above example  scanf(“%d%d”,&a,&b) is the example for call by reference.

 

More Examples
/* program to find arithmetic calculations by using functions */
#include<stdio.h>
#include<conio.h>
int sum(int,int);
int sub(int,int);
int prod(int,int);
int rem(int,int);
float div(int,int);
main()
{
int a,b,c;
float x;
clrscr();
printf("enter the values of a,b");
scanf("%d %d",&a,&b);
c=sum(a,b);
printf("the sum is %d\n",c);
c=sub(a,b);
printf("the difference is %d\n",c);
c=prod(a,b);
printf("the product is %d\n",c);
x=div(a,b);
printf("the division is %f\n",x);
c=rem(a,b);
printf("the reminder is %d\n",c);
}
int sum(int x,int y)
{
return x+y;
}
int sub(int x,int y)
{
return x-y;
}
int prod(int k,int d)
{
return k*d;
}
float div(int ding,int dong)
{
return (float)ding/dong;
}
int rem(int k,int d)
{
return k%d;
}

/* program to find the simple interest by using functions */
#include<stdio.h>
#include<conio.h>
float findsi(int,int,int);
main()
{
int p,n,r;
float si;
clrscr();
printf("enter the values of p,n,r");
scanf("%d %d %d",&p,&n,&r);
si=findsi(p,n,r);
printf("the simple interest is %f\n",si);
}
float findsi(int x,int y,int z)
{
return (float)x*y*z/100;
}

/* program to find the factorial by using functions */
#include<stdio.h>
#include<conio.h>
int findfact(int);
main()
{
int n,res;
clrscr();
printf("enter the values of n");
scanf("%d",&n);
res=findfact(n);
printf("the factorial is %d\n",res);
}
int findfact(int x)
{
int fact=1;
while(x>0)
{
fact=fact*x;
x--;
}
return fact;
}

/* program to find the reverse of the number */
#include<stdio.h>
#include<conio.h>
int findreverse(int);
main()
{
int n,res;
clrscr();
printf("enter number n");
scanf("%d",&n);
res=findreverse(n);
printf("the reverse is %d\n",res);
}
int findreverse(int x)
{
int r,sum=0;
while(x>0)
{
r=x%10;
sum=(sum*10)+r;
x=x/10;
}
return sum;
}

/* program to find whether it is prime number or not */
#include<stdio.h>
#include<conio.h>
int prime(int);
main()
{
int n,c;
clrscr();
printf("enter the value of n");
scanf("%d",&n);
c=prime(n);
if(c==2)
printf("prime number");
else
printf("not a prime number");
}
int prime(int x)
{
int i=1,count=0;
while(i<=x)
{
if(x%i==0)
{
count++;
}
i++;
}
return count;
}

/* program to find given number is perfect or not */
#include<stdio.h>
#include<conio.h>
int perfect(int);
main()
{
int n,s;
clrscr();
printf("enter the value of n");
scanf("%d",&n);
s=perfect(n);
printf("the sum is %d\n",s);
if(s==n)
printf("perfect number");
else
printf("not a perfect number");
}
int perfect(int x)
{
int i=1,sum=0;
while(i<x)
{
if(x%i==0)
{
sum=sum+i;
}
i++;
}
return sum;
}

/* program to find the sum of the two numbers */
#include<stdio.h>
#include<conio.h>
void sum(int,int);
main()
{
int a,b;
clrscr();
printf("enter the value of a,b");
scanf("%d %d",&a,&b);
sum(a,b);
}
void sum(int x,int y)
{
printf("the sum is %d",x+y);
}

/* program to find the length of the string */
#include<stdio.h>
#include<conio.h>
int mystringlen(char[]);
main()
{
char x[40];
int l;
clrscr();
printf("enter the string");
gets(x);
l=mystringlen(x);
printf("the length of the string is %d\n",l);
}
int mystringlen(char x[40])
{
int i=0;
while(x[i]!='\0')
{
i++;
}
return i;
}

/* program to convert string into lowercase */
#include<stdio.h>
#include<conio.h>
void mystringlwr(char[]);
main()
{
char x[40];
int l;
clrscr();
printf("enter the string");
gets(x);
printf("my string is %s\n",x);
mystringlwr(x);
printf("my string in lower case %s\n",x);
}
void mystringlwr(char x[40])
{
int i=0;
while(x[i]!='\0')
{
if((x[i]>='A')&&(x[i]<='Z'))
{
x[i]=x[i]+32;
}
i++;
}
}

/* program to convert string into uppercase */
#include<stdio.h>
#include<conio.h>
void mystringupr(char[]);
main()
{
char x[40];
int l;
clrscr();
printf("enter the string");
gets(x);
printf("my string is %s\n",x);
mystringupr(x);
printf("my string in upper case %s\n",x);
}
void mystringupr(char x[40])
{
int i=0;
while(x[i]!='\0')
{
if((x[i]>='a')&&(x[i]<='z'))
{
x[i]=x[i]-32;
}
i++;
}
}

/* program to reverse the given string */
#include<stdio.h>
#include<conio.h>
void mystringrev(char[]);
main()
{
char x[40];
int l;
clrscr();
printf("enter the string");
gets(x);
printf("my string is %s\n",x);
mystringrev(x);
printf("reverse string is %s\n",x);
}
void mystringrev(char y[40])
{
int i=0,j=0;
char temp;
while(y[j]!='\0')
{
j++;
}
j--;
while(i<=j)
{
temp=y[i];
y[i]=y[j];
y[j]=temp;
i++;
j--;
}
}

/* program to copy the one string into another string */
#include<stdio.h>
#include<conio.h>
void mystringcopy(char[],char[]);
main()
{
char x[40],y[40];
int l;
clrscr();
printf("enter the string");
gets(x);
printf("my string is %s\n",x);
mystringcopy(y,x);
printf("final string is %s\n",y);
}
void mystringcopy(char y[40],char x[40])
{
int i=0;
while(x[i]!='\0')
{
y[i]=x[i];
i++;
}
y[i]=x[i];
}

/* program to concatenate the strings */
#include<stdio.h>
#include<conio.h>
void mystringcat(char[],char[]);
main()
{
char x[40],y[40];
int l;
clrscr();
printf("enter the string x");
gets(x);
printf("enter the string y");
gets(y);
mystringcat(x,y);
printf("final string is %s\n",x);
}
void mystringcat(char x[40],char y[40])
{
int i=0,j=0;
while(x[i]!='\0')
{
i++;
}
while(y[j]!='\0')
{
x[i+j]=y[j];
j++;
}
x[i+j]='\0';
}

/* program to read and print book details */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno;
char bname[40];
};
struct book read(struct book);
void print(struct book);
main()
{
struct book b;
clrscr();
b=read(b);
print(b);
}
struct book read(struct book x)
{
printf("enter the bno,bname");
scanf("%d %s",&x.bno,&x.bname);
return x;
}
void print(struct book k)
{
printf("%d %s",k.bno,k.bname);
}

/* program to find the price of the book */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno,nop;
char bname[40];
float price;
};
struct book read(struct book);
void print(struct book);
struct book price(struct book);
main()
{
struct book b;
clrscr();
b=read(b);
print(b);
}
struct book read(struct book x)
{
printf("enter the bno,bname,nop");
scanf("%d %s %d",&x.bno,&x.bname,&x.nop);
x=price(x);
return x;
}
struct book price(struct book y)
{
y.price=y.nop*0.25;
return y;
}
void print(struct book k)
{
printf("%d %s %f",k.bno,k.bname,k.price);
}

/* program to read and print the student details */
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
int sno,m1,m2,m3,tot;
char sname[40],status[40];
float avg;
};
struct student read(struct student);
void print(struct student);
struct student tot(struct student);
struct student avg(struct student);
struct student status(struct student);
main()
{
Struct student s;
clrscr();
s=read(s);
print(s);
}
struct student read(struct student s)
{
printf("enter the sno,sname,m1,m2,m3");
scanf("%d %s %d %d %d",&s.sno,&s.sname,&s.m1,&s.m2,&s.m3);
s=tot(s);
return s;
}
struct student tot(struct student s)
{
s.tot=s.m1+s.m2+s.m3;
s=avg(s);
return s;
}
struct student avg(struct student s)
{
s.avg=s.tot/3.0;
s=status(s);
return s;
}
struct student status(struct student s)
{
if((s.m1>=35)&&(s.m2>=35)&&(s.m3>=35))
{
if(s.avg>=60)
strcpy(s.status,"***FIRST CLASS***");
else if(s.avg>=50)
strcpy(s.status,"***SECOND CLASS***");
else if(s.avg>=35)
strcpy(s.status,"***THISRD CLASS***");
else
strcpy(s.status,"***FAIL***");
}
else
strcpy(s.status,"***FAIL***");
return s;
}
void print(struct student s)
{
printf(" student no=%d\n student name=%s\n total=%d\n avg=%f\n status=%s\n",s.sno,s.sname,s.tot,s.avg,s.status);
}

/* example for  functios on arrays */
#include<stdio.h>
#include<conio.h>
void read(int[],int);
void print(int[],int);
void sum(int[],int);
void findmin(int[],int);
void findmax(int[],int);
void search(int[],int);
void delete1(int[],int);
main()
{
int a[10],n,k;
clrscr();
printf("enter the no of variables to be used");
scanf("%d",&n);
read(a,n);
print(a,n);
sum(a,n);
findmin(a,n);
findmax(a,n);
search(a,n);
delete1(a,n);
n--;
print(a,n);
}
void read(int x[10],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("enter the values of x[%d]=",i);
scanf("%d",&x[i]);
}
}
void print(int x[10],int n)
{
int i;
printf("\n");
for(i=0;i<n;i++)
{
printf("x[%d]=%d\n",i,x[i]);
}
printf("\n");
}
void sum(int x[10],int n)
{
int i,sum=0;
for(i=0;i<n;i++)
{
           sum=sum+x[i];
}
printf("\nthe sum is %d\n",sum);
}
void findmin(int x[10],int n)
{
int i,min=x[0];
for(i=0;i<n;i++)
{
if(x[i]<min)
{
min=x[i];
}
}
printf("\nthe min value is %d\n",min);
}
void findmax(int x[10],int n)
{
int i,max=x[0];
for(i=0;i<n;i++)
{
if(x[i]>max)
{
max=x[i];
}
}
printf("\nthe max value is %d\n",max);
}
void search(int x[10],int n)
{
int i,se,found=0;
printf("\nenter the searching element");
scanf("%d",&se);
for(i=0;i<n;i++)
{
if(x[i]==se)
{
printf("\nthe element is found\n");
found=1;
break;
}
}
if(found==0)
printf("\nthe element is not found\n");
}
void delete1(int x[10],int n)
{
int i,pos;
printf("\nenter the position to be deleted");
scanf("%d",&pos);
if(pos<n)
{
for(i=pos;i<n;i++)
{
x[i-1]=x[i];
}
}
else
{
printf("please enter positon < %d",n);
}
}

/* exchanging the values(call by value) */
#include<stdio.h>
#include<conio.h>
void exchange(int,int);
main()
{
int a,b;
clrscr();
printf("enter the values of a,b");
scanf("%d %d",&a,&b);
printf("given values are %d %d\n",a,b);
exchange(a,b);
printf("the changed values are %d %d\n",a,b);
}
void exchange(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
}

/* exchanging values(call by reference) */
#include<stdio.h>
#include<conio.h>
void exchange(int *,int *);
main()
{
int a,b;
clrscr();
printf("enter the values of a,b");
scanf("%d %d",&a,&b);
printf("given values are %d %d\n",a,b);
exchange(&a,&b);
printf("the changed values are %d %d\n",a,b);
}
void exchange(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}

/* program to addition of two numbers(call by value) */
#include<stdio.h>
#include<conio.h>
int sum(int,int,int);
main()
{
int a,b,c;
clrscr();
printf("enter the values of a,b");
scanf("%d %d",&a,&b);
c=sum(a,b,c);
printf("the sum is %d\n",c);
}
int sum(int a,int b,int c)
{
c=a+b;
return c;
}

/* program to addition of two numbers(call by reference) */
#include<stdio.h>
#include<conio.h>
void sum(int *,int *,int *);
main()
{
int a,b,c;
clrscr();
printf("enter the values of a,b");
scanf("%d %d",&a,&b);
sum(&a,&b,&c);
printf("the sum is %d\n",c);
}
void sum(int *a,int *b,int *c)
{
*c=*a+*b;
}

/* program to factorial of given number(call by reference) */
#include<stdio.h>
#include<conio.h>
void findfact(int *,int *);
main()
{
int n,fact=1;
clrscr();
printf("enter the number");
scanf("%d",&n);
findfact(&n,&fact);
printf("the result is %d\n",fact);
}
void findfact(int *n,int *fact)
{
while(*n>0)
{
*fact=*fact*(*n);
(*n)--;
}
}




  « Previous                                                                  Next Chapter »