Definition: A self –contained block of code that performs a
particular task is called
Function.
Advantages of functions:
1. It facilitates top down modular programming.
2. The length of a source program can be reduced by using
functions at appropriate
places.
3. It is easy to locate and isolate a faulty function for
further investigations.
4. A function may be used by many other programs.
A function has 3 elements.
1. Function Declaration (Function Prototype)
2. Function Call
3. Function Definition
1. Function Declaration (Function Prototype)
A function declaration also known as Function Prototype
consists of four parts:
Function type(return type)
Function name
Parameter list
Terminating Semicolon
Syntax:
Function_type Function_name(Formal Parameter list);
Example:
int sum(int a, int b);
or
int sum(int, int);
Points to note:
1. The parameter list must be separated by commas.
2. The parameter names do not need to be the same in the
prototype declaration
and the function definition.
3. The types must match the types of parameters in the
function definition, in
number and order.
4. Use of parameter names in the declaration is optional.
Function Parameter:
The parameters used in function declaration and function
definition are called formal
parameter and those used in function call is called actual
parameter.
The formal and actual parameters must match exactly in type,
order and number. Their
names, however, do not need to match.
Examples: sum=add (a, b, c); // here a, b, c are called
actual parameter.
int add(int x , int y, int z); // here x, y, z are formal
parameter.
Function Return: Function can be organized into 2 types.
a) Function that has not a return value i.e. void function.
b) Function that has a returned value i.e. int function.
2. Function Call
A function can be called by simply using the function name
followed by a list of actual
parameters, if any, enclosed in parenthesis.
Static variables are created and initialized once, on the
first call to the function.
Subsequent calls to the function do not recreate or
re-initialize the static variable.
Example:
c=sum (a,b);
3. Function Definition
The function definition is an independent program module
that is specially written to
implement the requirement of the function.
A function definition, also known as function implementation
shall include the following
elements.
a) Function type
b) Function Name
c) List of parameters
d) Local variable declarations
e) Function statements
f) A return statement
All the six elements are grouped into two parts, namely,
Function Header(First three elements)
Function Body ( Second three elements)
Syntax:
Function_type Function_Name(Formal Parameter list)
{
Local variable declaration;
Executable statement 1;
Executable statement 2;
......................
......................
return statement;
}
Note that a semicolon is not used at the end of the Function
Header of Function
definition.
Difference between Local Variable and Global variable
[HSEB2068]
Local Variable Global Variable
1. Any variable declare within the specific
function is called Local Variable.
1. Any variable which is declared outside all
the function is called Global Variable.
2. Visibility: Visible inside the
corresponding function only.
2. Visibility: Visible throughout program.
3. Accessible: Accessible to instruction of
corresponding function.
3. Accessible: Accessible to all the
instruction to specified program.
4. Life span: Created when control is
transferred from calling to called function
and destroyed when control returned to
calling function.
4. Life span: Life of program or until the
program run.
5. Duplicate is allowed. 5. Global variable name should be
unique.
Difference between Library and User-defined Function [HSEB
2067]
Library Function User –defined Function
1. It is already defined functions. 1. It is defined by the
programmers as their need.
2. It needs header file to use the functions. 2. It requires
the function declaration to use the functions.
3. We can’t modify the library functions. 3. We can modify
the user –defined functions.
5. Example:
printf( );
scanf ( ); etc.
5. Example:
sum( );
max( ); etc.
50.WAP to find the sum of two numbers using function?[HSEB
2072-E,2073-D]
#include<stdio.h>
#include<conio.h>
int sum (int x, int y);
void main( )
{
int a,b,c;
printf("Enter any two numbers=");
scanf("%d%d",&a,&b);
c=sum (a,b);
printf("The sum of two numbers=%d",c);
getch( );
}
int sum (int x, int y)
{
int p;
p=x+y;
return p;
}
51. WAP to find the area of circle using function?
#include<stdio.h>
#include<conio.h>
void area(int);
main( )
{
printf("Enter the radius=");
scanf("%d",&r);
area(r);
getch( );
}
void area(int r)
{
float a;
a=3.14*r*r;
printf("The area of circle=%0.2f\n",a);
}
Output:
Enter the radius=10
The area of circle=314.00
52.WAP to calculate simple interest using function?
#include<stdio.h>
#include<conio.h>
int si(int,int,int);
void main( )
{
int a,p,t,r;
printf("Enter the value of p,t,r =");
scanf("%d%d%d",&p,&t,&r);
a=si(p,t,r);
printf("Simple Interest=Rs.%d",a);
getch( );
}
int si(int x,int y, int z)
{
int s;
s=(x*y*z)/100;
return s;
}
53.WAP to find factorial of a given number using function?
[2063]
#include<stdio.h>
#include<conio.h>
int fact(int);
void main( )
{
int n,f;
printf("\nEnter any number ");
scanf("%d",&n);
f=fact(n);
printf("\n The factorial of %d = %d",n,f);
getch( );
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return(f);
}
54. WAP to display largest number among three numbers using
a function ?
#include<stdio.h>
#include<conio.h>
int max(int a, int b,int c);
main( )
{
int w,x,y,z;
printf("Enter any three numbers=");
scanf("%d%d%d",&x,&y,&z);
w=max(x,y,z);
printf("Largest number = %d",w);
getch( );
}
int max(int a, int b,int c)
{
if(a>b&&a>c)
return a;
else if(b>a&&b>c)
return b;
else
return c;
}
55.WAP to find the sum of “n” numbers using function? [HSEB
2073-C]
#include<stdio.h>
#include<conio.h>
int sum(int n);
main( )
{
int n,a;
printf("Enter a number=");
scanf("%d",&n);
a=sum(n);
printf("The sum of number=%d",a);
getch( );
}
int sum(int n)
{
int i,p=0;
for(i=1;i<=n;i++)
{
p=p+i;
}
return p;
}
56.WAP to calculate x raise to power y using a user defined
function? [P = xy ]
#include<stdio.h>
#include<conio.h>
int power(int x, int y);
main( )
{
int x,y,c;
printf("Enter the value of x and y=");
scanf("%d%d",&x,&y);
c = power(x,y);
printf("\n %d to the power %d=%d",x,y,c);
getch( );
}
int power(int x,int y)
{
int i,p=1;
for(i=1;i<=y;i++)
p=p*x;
return p;
}
Output:
Enter the value of x and y=5
2
5 to the power 2=25
57.WAP to sort “n” numbers of array using functions?
#include<stdio.h>
#include<conio.h>
void sort(int a[100],int n);
main( )
{
int i,n,a[100];
printf("Enter the size of array not more than
100=");
scanf("%d",&n);
sort(a,n);
getch( );
}
void sort(int a[100],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
printf("Enter the %d number =",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp; }} }
printf("Series in Ascending order\n");
for(i=0;i<n;i++)
{
printf("%d \t",a[i]);
}
}
What is a recursion?
Ans: Recursion is a process of calling itself again and
again until some condition has been
satisfied is called Recursion.
For the problem to solve recursively two conditions must be
satisfied:
The function should call itself.
The problem statement must include a stopping condition.
58.WAP to calculate the factorial of a number using
recursive function? [HSEB
– 2064, 2067, 2068, 2070-D,2071-C]
#include<stdio.h>
#include<conio.h>
int factorial(int);
main( )
{
int n;
printf("Enter a number=");
scanf("%d",&n);
printf("The factorial of %d is
%d",n,factorial(n));
getch( );
}
int factorial(int n)
{
if(n<=1)
return(1);
else
return(n*factorial(n-1));
}
59.WAP to display the Fibonacci series using recursive
function?
#include<stdio.h>
#include<conio.h>
int fibonacci(int,int,int);
main( )
{
int n;
printf("Enter number of terms=");
scanf("%d",&n);
fibonacci(0,1,n);
getch( );
}
int fibonacci(int a,int b,int c)
{
if(c!=0)
{
printf("%d\t",a);
return (fibonacci(b,a+b,c-1));
}
}
Output:
Enter number of terms=10
0 1 1 2 3 5 8 13 21 34
Accessing a Function
The function can be access by its name as declared in
definition and prototype part. The
passed out parameters on the user defined functions must be
accepted by the called
function. The function can be accessed by within the same
program. This process of performed by calling the function name with some parameter.
There are two types of
accessing a function.
a) Call by Value
b) Call by Reference
a) Call by Value
There is link between the formal and actual parameters. A
temporary storage is created
where the value of actual parameter is stored. The formal
parameter picks up its value
from storage area; the mechanism of data transfer between
actual and formal
parameters is known as call by value. The corresponding
formal parameter represents a
local variable in the called function. The current values of
corresponding actual
parameter become initial value of formal parameter. The
value of formal parameter may
be changed in the body of the actual parameter. The value of
formal parameter may be
changed in the body of the subprogram by assignment or input
statements. This will not
change the value of actual parameter.
Example program of Call by Value
#include<stdio.h>
#include<conio.h>
void fxn(int,int );
main( )
{
int a=20,b=30;
printf("Value of a=%d,b=%d\n",a,b);
fxn(a,b);
printf("Value of a=%d,b=%d",a,b);
getch( );
}
void fxn(int c,int d)
{
int temp;
temp=c;
c=d;
d=temp;
}
Output:
Value of a=20,b=30
Value of a=20,b=30
b) Call by Reference
When we pass address to a function the parameters receiving
the address should be
pointers. The process of calling a function by using
pointers to pass the address of the
variable is known as Call by Reference. This function which
is called by reference can
change the values of the variable used in the call.
Example program of Call by Reference
#include<stdio.h>
#include<conio.h>
void swap(int *x, int *y);
main( )
{
int a =50 ,b= 100;
printf("Before swapping a=%d\t b=%d\n",a,b);
swap(&a,&b);
printf("After swapping a=%d\t b=%d",a,b);
getch( );
}
void swap(int *x, int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
Output:
Before swapping a=50 b=100
After swapping a=100 b=50
Homework Questions
C-4 (Function)
1. What is function? Write down the advantages of
functions?[HSEB 2072-C]
2. Explain local, global and static local variables with
suitable example.
3. What is function? Differentiate between library and user
defined
function?
4. Write a recursive function to calculate factorial of a
given integer
number.[HSEB2068.70,71]
5. Write a program to find the sum of “n” integer number
using function in
C.[HSEB 2073-C]
Your suggestions / query encourages more to share things like this. Feel free to comment in below box.
What ever you think about it...