Functions
Function is a collection of statements to perform a specific task.
Dividing a large program into the sub programs where sub program known as function.
Program
Sub Program Sub Program Sub Program
Advantage of functions in C
There are the following advantages of C functions.
o Code Reusability:
o By using functions, we can avoid rewriting same logic/code again and again in a
program.
o We can call C functions any number of times in a program and from any place in a
program.
o We can track a large C program easily when it is divided into multiple functions.
o Debugging is easy:
Whenever an error occurs in the program, you can easily investigate faulty functions and
correct only those errors.
o Easy to understand:
Dividing a large program into various functions, it becomes easy to manage each function
individually
Types of Functions
Functions divided into two types
1. Built-in Functions/library Functions
2. User-defined Functions
Library Functions
There functions are already defined in the C Library
These functions are defined in the header file. To use these functions we need to import the
specific header files.
Eg:
The library function <stdio.h> includes these common functions(there are many other functions
too):
printf() shows the output in the user’s format.
scanf() used to take the user’s input
User-defined function:
User-defined function is a type of function which is defined by the user.
It is a collection of statements to perform a specific task.
functions are divided into three activities such as,
1. Function declaration (Function Prototype)
2. Function call (Calling function)
3. Function definition (Called function)
Function prototype
A function prototype is simply the declaration of a function that specifies function's name, type of
parameters, Number of parameters and return type.
returnType functionName(type1, type2, ...);
Example: int sum(int a, int b); is the function prototype which provides the following
information to the compiler:
name of the function is addNumbers()
return type of the function is int
two arguments of type int are passed to the function
Calling a Function
This is used to call the function.
After creating a function, the next step is to call the created function.
When a program calls a function, the program control is transferred to the called function
functionName(argument1, argument2, ...);
Ex: sum(n1, n2);
A called function performs a defined task and when its return statement is executed or when
its function-ending closing brace is reached, it returns the program control back to the main
program.
During call of a function, we simply need to pass the required parameters along with the
function name, and if the function returns a value, then we can store the returned value.
Function definition :
It is a collection of statements to perform a specific task.
returnType functionName(type1 argument1, type2 argument2, ...)
{
//body of the function
return(val/Expression)
}
function may or may not return a value from the function. If you don't have to return any value
from the function, use void for the return type.
Ex:
int sum(int a, int b)
{
int s;
s=a+b;
return s;
}
WAP to find the sum of two numbers using functions?
#include<stdio.h>
int add(int , int ); // function declaration
void main()
{
int a, b, result;
printf("enter 2 numbers \n");
scanf("%d%d", &a, &b);
result = add(a, b); // function call
printf("The result of addition is: %d", result);
}
int add(int x, int y) // function defintion
{
int s;
s=x+y;
return (s);
}
Different aspects of function calling(Category of User –Defined Functions)
A function may or may not accept any argument. It may or may not return any value. Based on
these facts, There are four different aspects of function calls.
o function without arguments and without return value
o function without arguments and with return value
o function with arguments and without return value
o function with arguments and with return value
Functions with no arguments and no return values:
This type of function has no arguments, meaning that it does not receive any data from the
calling function and there is no data transfer from called function to the calling function.
Functions with arguments and no return values:
In this category there is data transfer from the calling function to the called function using
parameters.
But, there is no data transfer from called function to the calling function.
Functions with no arguments and return values:
In this category there is no data transfer from the calling function to the called function.
But, there is a data transfer from called function to the calling function using return
statement.
Functions with arguments and return values:
In this category there is data transfer from the calling function to the called function.
And there is a data transfer from called function to the calling function using return
statement.
function without arguments and without function with arguments and without
return value return value
#include<stdio.h> #include<stdio.h>
void add( ); // function declaration void add(int , int ); // function declaration
void main( ) void main()
{ {
add( ); // function call int a, b;
} printf("enter 2 numbers \n");
void add( ) // function defintion scanf("%d%d", &a, &b);
{ add(a, b); // function call
int a, b,s; }
printf("enter 2 numbers \n"); void add(int x, int y) // function defintion
scanf("%d%d", &a, &b); {
s=a+b; int s;
printf("Addition is: %d", s); s=x+y;
} printf("Addition is: %d", s);
}
function without arguments and with return function with arguments and with return
value value
#include<stdio.h> #include<stdio.h>
int add( ); // function declaration int add(int , int ); // function declaration
void main( ) void main()
{ {
int result; int a, b, result;
result = add( ); // function call printf("enter 2 numbers \n");
printf("Addition is: %d", result); scanf("%d%d", &a, &b);
} result = add(a, b); // function call
int add( ) // function defintion printf("Addition is: %d", result);
{ }
int a, b,s; int add(int x, int y) // function defintion
printf("enter 2 numbers \n"); {
scanf("%d%d", &a, &b); int s;
s=x+y; s=x+y;
return(s); return (s);
} }
Function Arguments: The arguments that are supplied to function are in two categories
Actual Arguments/Parameters
Formal Arguments/Parameters
Actual Arguments/Parameters:
Actual parameters are the expressions in the calling functions.
These are the parameters present in the calling statement (function call).
Formal Arguments/Parameters:
Formal parameters are the variables that are declared in the header of the function
definition.
Note: Actual and Formal parameters must match exactly in type, order, and number.
Passing Parameters to Functions: There are two ways of passing parameters to the
functions.
1. Call by value 2. Call by reference
Call by value:
Calling a function by passing the value is called as call by value.
In call by value actual parameters and formal parameters having its own memory locations
so any modification to the formal parameter does not affect the actual parameter.
Example:-
void swap(int,int);
void main()
{
int x,y;
printf(“enter two values:\n”);
scanf(“%d%d”,&x,&y);
printf(“value of x=%d and y=%d\n”,x ,y);
swap(x ,y);
printf(“value of x=%d and y=%d\n”,x ,y);
}
void swap(int a,int b)
{
int k;
k=a;
a=b;
b=k;
printf(“value of a=%d and b=%d\n”,a ,b);
}
Output: enter two values: 12 23
Value of x=12 and y=23
Value of a=23 and y=12
Value of x=12 and y=23
When a function is called with actual parameters, the values of actual parameters(x,y) are
copied into the formal parameters(a,b).
Note that the values of a and b are swapped in the function.
But, the values of actual parameters remain same before swap and after swap.
Note: In call by value any changes done on the formal parameter will not affect the actual
parameters.
Storage Classes:
Storage class refers to the performance of a variable and its scope. A storage class
determines
• The part of memory where storage is allocated for a variable (Memory area)
• What will be the initial value of the variable if it is not initialized (Default initial
value)
• How long the storage allocation continuous to exist (Life time)
• In which parts of the program the variable can be accessed by using its name (Scope
or Visibility)
There are four types of storage classes:-
1 ) Automatic (auto)
2 ) Register (register)
3 ) Static (static)
4 ) External (extern)
Syntax of declaring storage classes is:-
storageclass datatype variable name;
Ex: static int a;
Storage class Default value Storage Scope Lifetime
auto Garbage value Memory Within the block Till end of block
static Zero Memory Within the block Till end of
program
extern Zero Memory Entire program / Till end of
Other files program
register Garbage value Register Within the block Till end of block
Automatic storage class
The keyword used to declare automatic storage class is auto.
Its features:-
Storage-memory location
Default initial value:-unpredictable value or garbage value.
Scope:-local to the block or function in which variable is defined.
Life time:-Till the control remains within function or block in which it is defined.
It terminates when function is released.
The variable without any storage class specifier is called automatic variable.
Example:
#include<stdio.h>
void display();
void main()
{
display();
display();
}
void display()
{
auto int a=1;
printf("%d ",a);
a=a+1;
}
Output: 1 1
Static storage class
The keyword used to declare static storage class is static.
Its features:-
Storage-memory location
Default initial value:-Zero
Scope:-local to the block or function in which variable is defined.
Life time:- Till end of program. It always maintains the last updated value
between function calls.
Example:
#include<stdio.h>
void display();
void main()
{
display();
display();
}
void display()
{
static int a=1;
printf("%d ",a);
a=a+1;
}
Output: 1 2
Extern storage class
The keyword used to declare extern storage class is extern.These variables called as global
variable
Its features:-
Storage-memory location
Default initial value:-Zero
Scope:-Entire program (global)
Life time:- Till end of program
Example:
#include<stdio.h>
void display();
int a=1;
void main()
{
display();
display();
}
void display()
{
printf("%d ",a);
a=a+1;
}
Output: 1 2
Register storage class
The keyword used to declare register storage class is register.
Its features:-
Storage-Register
Default initial value:-unpredictable value or garbage value.
Scope:-local to the block or function in which variable is defined.
Life time:-Till the control remains within function or block in which it is defined.
It terminates when function is released.
Example:
#include<stdio.h>
void display();
void main()
{
display();
display();
}
void display()
{
register int a=1;
printf("%d ",a);
a=a+1;
}
Output: 1 1
Local Variable VS Global Variable:
Parameter Local Global
Declaration It is declared inside a function. It is declared outside of all functions.
If it is not initialized, a garbage
Value If it is not initialized zero is stored as default.
value is stored
Local variables can be accessed
Scope inside a function in which they Global variables can be accessed entire program
are declared.
Lifetime Till the function ends Till the program ends
Data sharing is not possible as
Data sharing is possible as multiple functions
Data sharing data of the local variable can be
can access the same global variable.
accessed by only one function.
Parameters passing is required
Parameters passing is not necessary for a global
Parameters for local variables to access the
variable as it is visible throughout the program
value in other function
When the value of the local
Modification When the value of the global variable is
variable is modified in one
of variable modified in one function changes are visible in
function, the changes are not
value the rest of the program.
visible in another function.
#include<stdio.h> #include<stdio.h>
void display(); void display();
void main() int a=1; //global var
{ void main()
display(); {
display(); display();
} display();
Program void display() }
{ void display()
int a=1; //local var {
printf("%d ",a); printf("%d ",a);
a=a+1; a=a+1;
} }
Output: 1 1 Output: 1 2
Recursion:
A function calling itself is called recursive function.
Recursion is a repetitive process, where the function calls itself.
It is used to perform a task repeatedly.
It uses a STACK data structure.
Factorial using Recursion:
#include<stdio.h>
void fact(int);
void main()
{
int n;
printf("enter the value\n");
scanf("%d",&n);
fact(n);
}
void fact(int n)
{
static int f=1;
if(n>=1)
{
f=f*n;
fact(n-1); //recursive call
}
else
{
printf("factorial = %d",f);
}
}
Output:
enter the value
5
factorial = 120
Explanation:
n=5 fact(5) f=1*5=5 fact(5-1)
n=4 fact(4) f=5*4=20 fact(4-1)
n=3 fact(3) f=20*3=60 fact(3-1)
n=2 fact(2) f=60*2=120 fact(2-1)
n=1 fact(1) f=120*1=120 fact(1-1)
n=0 fact(0) printf("factorial = %d",f); 120
Fibonacci series using Recursion:
#include<stdio.h>
void fibo(int);
void main()
{
int n;
printf("Enter a number \n");
scanf("%d",&n);
fibo(n);
}
void fibo(int n)
{
static int f1=-1,f2=1,f;
if(n>0)
{
f=f1+f2;
printf("%d ",f);
f1=f2;
f2=f;
fibo(n-1);
}
}
OUTPUT:
Enter a number
10
0 1 1 2 3 5 8 13 21 34