FUNCTIONS

What are Functions?

Functions divide the larg part of the program into small parts which makes the program code organised and easy to read.

Defining a function involve three steps in the program code. these are:-

  1. FUNCTION DECLARATION:-   The function is declared at the start with data type which defines the type and name of function.

  2. FUNCTION CALLING:-   The function is called in the program code whenever required. One have to call for the function for its block of code to be executed.

  3. FUNCTION BODY:-   The function body includes everything that has to done on calling of function.


Function without Arguement

When a function has no arguements, it does not recieve any data from the calling function. Similarly function with no return does not recieve any data from the called function.

Syntax Input Output
#include<stdio.h>
void main()  
{
void abc();   (Function decalaration)  
abc();        (Function calling)
}
void abc()    (Function
{               B
________;       O
________;       D
}               Y)

#include<stdio.h>
void main()
{
void add();
void sub();
clrscr();
add();
sub();
getch();
}
void add()
{
int a,b,c;
printf("enter a=");
scanf("\n%d",&a);
printf("\nenter b=");
scanf("\n%d",&b);
c=a+b;
printf("\na+b=%d",c);
}
void sub()
{
int a,b,c;
printf("\nenter a=");
scanf("%d",&a);
printf("\nenter b=");
scanf("%d",&b);
c=a-b;
printf("\na-b=%d",c);
}


Function with Arguement

When a function has arguements, the variables are declared with function declaration, and it recieves data from the calling function.

Syntax Input Output
#include<stdio.h>
void main()  
{
void abc(int a,int b);   (Function decalaration)  
abc(10,20);              (Function calling)
}
void abc(int a,int b)    (Function
{                         B
________;                 O
________;                 D
}                         Y)

#include<stdio.h>
void main()
{
void add (int a, int b);
clrscr();
add(19,20);
add(45,66);
}
void add(int a, int b)
{
int c;
c=a+b;
printf("%d\n",c);
}


Function with Return

In a function with return, it recieves data from the called function.

Syntax Input Output
#include<stdio.h>
void main()  
{
int abc();                (Function decalaration)  
printf("%d",abc());       (Function calling)
}
int abc()                 (Function
{   
int c;                        B
________;                     O
________;
return(c);                    D
}                             Y)

#include<stdio.h>
void main()
{
int add();
int percentage;
clrscr();
add();
percentage=add()/3;
printf("%d",percentage);
getch();
}
int add()
{
int phys,chem,bio,total;
phys=89;
chem=90;
bio=95;
total=phys+chem+bio;
return(total);
}


Function with Return and Arguement

In a function with return and with arguement, it recieves data from the called function and also from the calling of function.

Syntax Input Output
#include<stdio.h>
void main()  
{
int abc(int a,int b);          (Function decalaration)  
printf("%d",abc(34,45));       (Function calling)
}
int abc(int a,int b)           (Function
{ 
int c;                          B
________;                       O
________;
return(c);                      D
}                               Y)

#include<stdio.h>
void main()
{
int add(int a, int b);
int d;
clrscr();
d=add(40,50)+12;
printf("%d",d);
getch();
}
int add(int a, int b)
{
int c;
c=a+b;
return(c);
}