Functions

Functions in C -: Functions are one of the most important facilities provided by a programming language. There are several advantages of functions but some of these are listed below.




A sample program with functions.


#include< stdio.h>
#include< conio.h>
void function()
{
printf("abc\n");
printf("def\n");
}
void main()
{
function1();
printf("**********\n");
function1();
getch();
}

Although functions in C are of two types these are predefined functions and user defined functions ( udf) predefined functions are already defined in different header files hence we do not need to define th em we only call them but user defined functions are always to be defined by the user before their calling.

Program to add two number using functions.


#include< stdio.h>
#include< conio.h>
void abc()
{
int a,b,c;
printf("enter two numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("the sum of number is %d",c);
}
void main()
{
clrscr();
getch();
}


Calling versus Called functions-:


In functional programming those functions that are capable to call some other functions are called calling functions .
In contrast those functions that are called by calling function are known as called functions.
Example-: In the previous program (ie.addition of two number via functions) the main() function is working as a calling function while the abc () is working like a called function.

Function Definition versus Function Calling-:


Every function that is being called must be defined somewhere in the program definition means to bundle the logic and provide a proper name while calling means to be called by any calling functions.

Function Definition-:
void function1()
{
printf("hello");
}

Function Calling-:
void main()
{
clrscr();
function1();
getch();
}

Note point-:In a program although we can define a function anywhere but it is better to define its calling when we define our functions after calling we must use a prototype for that function.

Program to print table using functions.


#include< stdio.h>
#include< conio.h>
void function1()
{
int i,num,num1;
printf("enter the number");
scanf("%d",&num);
for(i=1;i<=10;i++)
{
num1=num*i;
printf("%d",num1);
}
}
void main()
{
clrscr();
function1();
getch();
}

To find prime or not using functions.


#include< stdio.h>
#include< conio.h>
void function1()
{
int i,num,c=0;
printf("enter a number");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
{
c=c+1;
}
}
if(c==2)
{
printf("prime");
}
else
{
printf("not prime");
}
}
void main()
{
clrscr();
function1();
getch();
}

Fibonnaci series using functions.


#include< stdio.h>
#include< conio.h>
void function1()
{
int i,j=1,k=0,l=0;
for(i=1;i<=10;i++)
{
l=j+k;
printf("\n%d",l);
k=j;
j=l;
}
}
void main()
{
clrscr();
function1();
getch();
}


Program that inputs a number and prints its reverse.


#include< stdio.h>
#include< conio.h>
int reverse(int);
void main()
{
int num,reverse;
printf("enter a number");
scanf("%d",&num);
reverse=reverse(num);
printf("%d",reverse);
getch();
}
int reverse(int n)
{
int rev=0,i; for(i=num;i>=1;i=i/10)
{
rev=rev*10+(i%10);
}
return rev;
}

Find difference of two numbers via functions.


#include< stdio.h>
#include< conio.h>
int substraction(int,int);
void main()
{
int a,b,diff;
int substraction(int,int)
void main()
{
diff=substraction(a,b);
printf("the difference is %d",diff);
getch();
}
int substraction(intc,intd)
{
int s;
s=c+d;
return s;
}

Find the factorial of a number using functions.


#include< stdio.h>
#include< conio.h>
int factorial(int,int);
void main()
{
int num,fct;
printf("enter a number");
scanf("%d",&num);
fct=factorial(num);
printf("the factorial is %d",fct);
getch();
}
int factorial(int n)
{
int i,fact=1;
for(i=n;i<=1;i--)
{
fact=fact*i;
}
return fact;
}

Program to find the number is even or odd.


#include< stdio.h>
#include< conio,h>
void even(int);
void main()
{
printf("enter a number");
scanf("%d",&num);
void even(num);
getch();
}
void even(int n)
{
for(i=1;i<=n;i++)
{
if(n%2==0)
{
printf("even");
}
else
{
printf("odd");
}
}

Program to find cube of a number.


#include< stdio.h>
#include< conio.h>
int cube(int);
void main()
{
int num,c;
printf("enter a number");
scanf("%d",&num);
c=cube(num);
printf("%d",c);
getch();
}
int cube(int n)
{
int l;
l=n*n*n;
return l;
}

Program to calculate simple intrest.


#include< stdio.h>
#include< conio.h>
float intrest(float,float,float);
void main()
{
float p,r,t,simple;
printf("enter the principal amount");
scanf("%f",&p);
printf("enter the rate percentage");
scanf("%f",&r);
printf("enter the time duration");
scanf("%f",&t);
simple=intrest(p,r,t);
printf("%f",simple);
getch();
}
float intrest(float p,float r,float t);
{
float s;
s=(p*r*t)/100;
return s;
}

Program that inputs a character and displays its opposite case.


#include< stdio.h>
#include< conio.h>
void opposite(char);
void main()
{
char ch;
printf("enter the character");
scanf("%d",&ch);
opposite(ch);
getch();
}
void opposite(char c)
{
if(c>=65&&c<=90)
{
c=c+32;
printf("the opposite case is%c",c);
}
else if(c>=97&&c<=122)
{
c=c-32;
printf("the opposite case is%c",c);
}
}

Program that inputs a number and checks whether it is even or odd.


#include< stdio.h>
#include< conio.h>
void check(int)
void main()
{
int num;
printf("enter the number");
scanf("%d",&num);
check(num);
getch();
}
void check(int n)
{
int i,c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c=c+1;
}
}
if(c==2)
{
printf("the number you entered is prime ");
}
else
{
printf("the number you entered is not prime ");
}
}

Program that inputs colour of traffic light and di splays whether to stop ,or to go or to to get ready.


#include< stdio.h>
#include< conio.h>
void display(char)
void main()
{
printf("enter the first letter red ,yellow or green ");
scanf("%c",&ch);
display(ch);
getch();
}
void display(char c)
{
switch(c)
{
case r:
case R:
printf("stop");
break;
case g:
case G:
printf("go");
break;
case y:
case Y:
printf("get ready");
break;
default:
printf("invalid choice");
break;
}
}

Function prototype


A function prototype is a statement that looks very similar to function calling. It is written right after the header file statements although it is better to declare/define before calling but when we call before definition, we must use function prototype .

With function prototype #include< stdio.h>
#include< conio.h>
void function ();
void main()
{
clrscr();
function();
________
________
getch();
}
void function()
{
__________
__________
__________
}

Without function prototype
#include< stdio.h>
#include< conio.h>
void function()
{
__________
__________
__________
}
void main()
{
clrscr();
function();
________
________
getch();
}


Syntax of function prototype-:

Syntax-:
returntype function name ( );
Arguments to a Function -: Arguments are nothing but the inputs that are given from calling to called functions these arguments are given a a group within the paranthesis of called functions.

Syntax-:
main()
______
______
______
function1(a,b);
{
______
______
_______
}


Argument must be given in a predefined number if we give more or less,we get errors.
Errors can be either-:
  1. Too much arguments.
  2. Too few arguments.


The output generated by called functions when comes back to the calling function it is called a return value.
Syntax-:
function1();
{
int a;
______
______
_______
return a;
}


Program to find area of circle.


#include< stdio.h>
#include< conio.h>
float areaofcircle(int);
void main()
{
int radius;
float area;
printf("enter radius");
scanf("%d",&radius);
area=areaofcircle(radius);
getch();
}
float areaofcircle(int r)
{
float a;
a=3.14*r*r;
return a;
}

Find sum of two numbers via functions.


#include< stdio.h>
#include< conio.h>
int addition(int,int);
void main()
{
int a,b,sum;
int addition(int,int)
void main()
{
sum=addition(a,b);
printf("the sum is %d",sum);
getch();
}
int addition(intc,intd)
{
int s;
s=c+d;
return s;
}

Program that inputs a number and prints its reverse .


#include< stdio.h>
#include< conio.h>
int reverse(int);
void main()
{
int num,reverse;
printf("enter a number");
scanf("%d",&num);
reverse=reverse(num);
printf("%d",reverse);
getch();
}
int reverse(int n)
{
int rev=0,i; for(i=num;i>=1;i=i/10)
{
rev=rev*10+(i%10);
}
return rev;
}

Find difference of two numbers via functions.


#include< stdio.h>
#include< conio.h>
int substraction(int,int);
void main()
{
int a,b,diff;
int substraction(int,int)
void main()
{
diff=substraction(a,b);
printf("the difference is %d",diff);
getch();
}
int substraction(intc,intd)
{
int s;
s=c+d;
return s;
}

Find the factorial of a number using functions.


#include< stdio.h>
#include< conio.h>
int factorial(int,int);
void main()
{
int num,fct;
printf("enter a number");
scanf("%d",&num);
fct=factorial(num);
printf("the factorial is %d",fct);
getch();
}
int factorial(int n)
{
int i,fact=1;
for(i=n;i<=1;i--)
{
fact=fact*i;
}
return fact;
}



Program to find the number is even or odd.


#include< stdio.h>
#include< conio,h>
void even(int);
void main()
{
printf("enter a number");
scanf("%d",&num);
void even(num);
getch();
}
void even(int n)
{
for(i=1;i<=n;i++)
{
if(n%2==0)
{
printf("even");
}
else
{
printf("odd");
}
}

Program to find cube of a number.


#include< stdio.h>
#include< conio.h>
int cube(int);
void main()
{
int num,c;
printf("enter a number");
scanf("%d",&num);
c=cube(num);
printf("%d",c);
getch();
}
int cube(int n)
{
int l;
l=n*n*n;
return l;
}

Program to calculate simple intrest.


#include< stdio.h>
#include< conio.h>
float intrest(float,float,float);
void main()
{
float p,r,t,simple;
printf("enter the principal amount");
scanf("%f",&p);
printf("enter the rate percentage");
scanf("%f",&r);
printf("enter the time duration");
scanf("%f",&t);
simple=intrest(p,r,t);
printf("%f",simple);
getch();
}
float intrest(float p,float r,float t);
{
float s;
s=(p*r*t)/100;
return s;
}

Program that inputs a character and displays its o pposite case.


#include< stdio.h>
#include< conio.h>
void opposite(char);
void main()
{
char ch;
printf("enter the character");
scanf("%d",&ch);
opposite(ch);
getch();
}
void opposite(char c)
{
if(c>=65&&c<=90)
{
c=c+32;
printf("the opposite case is%c",c);
}
else if(c>=97&&c<=122)
{
c=c-32;
printf("the opposite case is%c",c);
}
}

Program that inputs a number and checks whether it is even or odd.


#include< stdio.h>
#include< conio.h>
void check(int)
void main()
{
int num;
printf("enter the number");
scanf("%d",&num);
check(num);
getch();
}
void check(int n)
{
int i,c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c=c+1;
}
}
if(c==2)
{
printf("the number you entered is prime ");
}
else
{
printf("the number you entered is not prime ");
}
}

Program that inputs colour of traffic light and displays whether to stop ,or to go or to to get ready.


#include< stdio.h>
#include< conio.h>
void display(char)
void main()
{
printf("enter the first letter red ,yellow or green ");
scanf("%c",&ch);
display(ch);
getch();
}
void display(char c)
{
switch(c)
{
case r:
case R:
printf("stop");
break;
case g:
case G:
printf("go");
break;
case y:
case Y:
printf("get ready");
break;
default:
printf("invalid choice");
break;
}
}


Different types of functions with their Examples-:


  1. Without argument and without return value.
  2. With argument and without return value.
  3. With argument and with return value.
  4. Without argument and with return value.



Which type of function is to be implemented in the program totally depends on the type of program, here are some clues for it.
  • Those program that do not contain any mathematical calculations are best programs to be wriiten via without argument and without return value.
  • Those program that require mathematical inputs but do not produce any mathematical output can be written via with argument and without return value. For Example-: Any kind of checking and any kind of series.
  • Those programs that require mathematical inputs a nd produce mathematical outputs can be written via with argument and with return value functions.
  • Those programs that do not require mathematical inputs and produce mathematical outputs can be wri tten via without argument and with return value functions.



  • Examples-:

    Without argument and without a return value-:
    #include< stdio.h>
    #include< conio.h>
    void check(void)
    void main()
    {
    check();
    getch();
    }
    void check()
    {
    int i,n,c=0;
    printf("enter the number");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    if(n%i==0)
    {
    c=c+1;
    }
    }
    if(c==2)
    {
    printf("the number you entered is prime ");
    }
    else
    {
    printf("the number you entered is not prime ");
    }
    }

    With argument and without return value-:
    #include< stdio.h>
    #include< conio.h>
    void arm(int)
    void main()
    {
    int num;
    printf("enter a number");
    scanf("%d",&num);
    arm(num);
    getch();
    }
    void arm(int n)
    {
    int d=0,k,i;
    for(i=n;i>=1;i=i/10)
    {
    k=i%10;
    d=d+(k*k*k);
    }
    if(d==n)
    {
    printf("armstrong number");
    }
    else
    {
    printf("not a armstrong number");
    }
    }

    With argument and with return value-:
    #include< stdio.h>
    #include< conio.h>
    int pallindrome(int)
    void main()
    {
    int num,pallin;
    printf("enter a number");
    scanf("%d",&num);
    pallin=pallindrome(num);
    if(num==pallin)
    {
    printf("pallindrome");
    }
    else
    {
    printf("not pallindrome");
    }
    getch();
    }
    int pallindrome(int n)
    {
    int rev,i;
    for(i=n;i>=1;i=i/10)
    {
    rev=rev*10+(i%10);
    }
    return rev;
    }




    With argument and with return value-:
    #include< stdio.h>
    #include< conio.h>
    int areaofsquare(void)
    void main()
    {
    int area;
    area=areaofsquare(void);
    printf("the area of square is %d",area);
    getch();
    }
    int areaofsquare(void)
    {
    int a,side;
    printf("enter the side of square");
    scanf("%d",&side);
    a=side*side;
    return a;
    }

    Nesting of functions.

    C allows several functions to be nested in each ot her the one that is being called is known as calle d function while the one calling some other functi on is known as calling function.
    Syntax-:
    function1();
    {
    ______
    ______
    ______
    function2();
    {
    ______
    ______
    _______
    }
    function3();
    {
    ______
    ______
    ______
    }
    }
    void main()
    {
    _________
    _________
    _________
    function1();
    _________
    _________
    }
    getch();
    }


    Program to find roots of quadritic equation via functions.


    #include< stdio.h>
    #include< conio.h>
    #include< math.h>
    void function1(int i,int j,int k)
    {
    float alp,bit,e;
    e=function2(i,j,k);
    alp=((-j)+e)/(2*i);
    bit=((-j)-e)/(2*i);
    printf("%f",alp);
    printf("%f",bit);
    }
    function2(int x,int y,int z)
    {
    float d;
    d=(y*y)-(4*x*z);
    d=sqrt(d);
    return d;
    }
    void main()
    ; {
    int a,b,c;
    clrscr();
    printf("enter the value of a ,b and c");
    scanf("%d%d%d",&a&b&c);
    function1(a,b,c);
    getch();
    }