1. MANSI TYAGI
FUNCTION
De_nition
A’C’ program consists of one or more functions.
Every program must contain atleast one function named as main( ),
where the program always begins execution.
Functions are normally used to divide a large program into smaller
programs which are easier to handle.
Function after its execution returns a single value.
Need of Functions :of Function
Several advantages of modularizing a program into function includes:
Reduction in code redundancy
Enabling code reuse
Better readability
Information Hiding
Improved debugging and testing
Improved maintainability
Abhineet
A function is a self contained block of codes or sub programs with a set of
statements that perform some specific task or coherent task.
2. MANSI TYAGI
There are basically two types of function those are:
1. Library function
2. User defined function
1. Library /Built in /Standard function :
These functions that are inbuilt in the C language compiler are library
functions i.e: printf() and scanf() belongs to the category of library
functions.
These functions are present in C library and they are
predefined.
For ex: printf(),scanf(),getch(),clrscr(),main().
2. User defined function :
These functions that are defined by the user according to its requirement
at the time of writing the program.
User can create their own functions for performing any
specific task of the program.These types of functions are
called user-defined functions.
There are three aspect of working with user-defined
functions:
1. Function Declaration, also known as function prototype
2. Function Definition
3. Function Call or function invocation
3. MANSI TYAGI
Syntax:-
Return type function name (argument list )
Return type name of function (type 1 arg 1, type2 arg2, type3 arg3)
So when user gets his own function three thing he has to know, these
are.
Function declaration
Function definition
Function call
These three things are represented like :
int function(int, int, int); /*function declaration*/
main()
{
function(arg1,arg2,arg3); /* calling function*/
}
int function(type 1 arg 1,type2 arg2,type3, arg3) /*function definition/*
{
Local variable declaration;
Statement;
Return value;
}
Function declaration:-
Function declaration is also known as function prototype. It inform the compiler
about three thing, those are name of the function, and type of argument
received by the function and the type of value returned by the function.
Function prototype always terminated by the semicolon.
Syntax:
Type function_name(parameter list);
int function(int, int, int); /*function declaration*
Ex- int add(int x,int y);
4. MANSI TYAGI
Function definition:-
Function definition consists of the whole description and code of the function.
It tells about what function is doing what are its inputs and what are its out put
It consists of two parts function header and function body.
Syntax:-
return type function(type 1 arg1, type2 arg2, type3 arg3) /*function header*/
{
Local variable declaration;
Statement 1;
Statement 2; /*function body*/
Return value;
}
The local variable declared inside a function is local to that function only. It
can’t be used anywhere in the program and its existence is only within this
function.
The arguments of the function definition are known as formal arguments.
Function Call :
When the function get called by the calling function then that is called, function
call. The compiler execute these functions when the semicolon is followed by the
function name.
Example:-
function(arg1,arg2,arg3);
The argument that are used inside the function call are called actual argument
Ex:-
int S=sum(a, b); //actual arguments
5. MANSI TYAGI
PARAMETER PASSING:
Parameters are nothing but input information given to a function. By passing
parameters the caller can ask the function to process a set of values. Parameter passing
allows you to run generalized and reusable functions.
What ever the parameters the caller passes are called the actual parameters and
what ever parameters the function is return to receive are called the formal
parameters.
The actual parameters are copied to the formal parameters.
The arguments which are mentioned or used inside the function call is knows as
actual argument and these are the original values and copy of these are actually
sent to the called function.
The actual parameters are copied to the formal parameters.
It can be written as constant, expression or any function call like
Function (x);
Function (20, 30);
Example
Main()
{
Int ans;
Ans=add(a,b);
Actual argument
1. Actual Argument
1. Actual Argument
6. MANSI TYAGI
The arguments which are mentioned in function definition are called formal
arguments or dummy arguments.These arguments are used to just hold the copied
of the values that are sent by the calling function through the function call.
Example:
Int add (int x, int y) //*function definition*//
Formal Parameter/Argument
SCOPE OF VARIABLES
Scope of a variable means the portion of the program within which it can be
referenced and lifetime means the time of its existence in the memory.
The scope of local variables is limited to the functions in which they are declared,
or in other words these variables are inaccessible outside of the function. Like
wise the scope of the block variables is limited to the block in which they are
declared. Global have a scope that spans the entire source program, which is why
they can be used in any function.
1>.Local Variable 2>.Global Variable
1. LOCAL VARIABLES: Local variables that are defined with in a body of
function or block. The local variables can be used only in that function or block in
which they are declared.
Example : function()
{
int a,b;
2. Formal Argument
7. MANSI TYAGI
function 1();
}
2.GLOBAL VARIABLES :
These variables that are defined outside of the function is called global
variable/External variables. All functions in the program can access and modify
global variables.
A global variable can be used any where in the program.
Global variables are automatically initialized at the time of initialization.
Example:
#include<stdio.h>
void function(void);
void function1(void);
void function2(void);
int a, b=20; // * global variables *//
void main()
{
printf(“inside main a=%d,b=%d n”,a,b);
function();
function1();
function2();
}
function()
{
Prinf(“inside function a=%d,b=%dn”,a,b);
}
function 1()
{
GLOBAL vs LOCAL VARIABLES:
1. Local variables can be used only inside the function of the block in which they are
declared. On the other hand global variables are used through out the program.
8. MANSI TYAGI
2. All global variables, in the absence of explicit initialization, are automatically
initialized to zero. On the other hand All local variables, in the absence of explicit
initialization, are automatically initialized to garbage value.
3. The initial that you supplied for a global variable must be a constant, where as a
local variable can contain variable in its initializer.
4. A local variables loses its value the movement the function/block containing it is
exited. So you cannot expect a local variable to retain the value deposited in it the
previous time the function/block was entered. Global variables retain there values
through the program’s execution.
i) Function with no argument & no return value
Function that have no argument and no return value is written as:-
SYNTAX :
void function(void);
main()
{
void function()
{
Statement;
}
Category of Function based on argument and return type
i) Function with no argument & no return value
ii) Function with no argument but return value
iii ) function with argument but no return value
iv) function with argument and return value
9. MANSI TYAGI
Example :
#include<stdio.h>
#include<conio.h>
Void add (void);
{ Main()
Add();
}
Void add (void)
{
Int a,b,c;
Printf(“enter two valu”);
Scanf(“%d%d”,&a,&b);
C=a+b;
Printf(“%d”,c);
}
ii) Function with no argument but return value
Syntax:-
int fun(void);
main()
10. MANSI TYAGI
{
int r;
r=fun();
}
int fun()
{
reurn(exp);
}
Example:-
int sum();
main()
{
int b=sum();
printf(“entered %dn, b”);
}
int sum()
{
int a,b,s;
s=a+b;
return s;
}
Here called function is independent and are initialized. The values aren’t
passed by the calling function .Here the calling function and called function are
communicated partly with each other.
iii ) function with argument but no return value
Here the function have argument so the calling function send data to the called
function but called function does not return value.
11. MANSI TYAGI
Syntax:-
void fun (int,int);
main()
{
int (a,b);
}
void fun(int x, int y);
{
Statement;
}
Here the result obtained by the called function.
iv) function with argument and return value
Here the calling function has the argument to pass to the called function and the
called function returned value to the calling function.
Syntax:-
fun(int,int);
main()
{
int r=fun(a,b);
}
int fun(intx,inty)
{
return(exp);
}
Example:
main()
{
int fun(int);
int a,num;
printf(“enter value:n”);
scanf(“%d”,&a)
int num=fun(a);
}
int fun(int x)
{
++x;
12. MANSI TYAGI
return x;
}
Call by value and call by reference
There are two way through which we can pass the arguments to the
function such as call by value and call by reference.
1. Call by value
In the call by value copy of the actual argument is passed to the
formal argument and the operation is done on formal argument.When
the function is called by ‘call by value’ method.
It does not affect content of the actual argument. Changes made
to formal argument are local to block of called function so when
the control back to calling function the changes made is vanish.
Example:-
main()
{
int x,y;
change(int,int);
printf(“enter two values:n”);
scanf(“%d%d”,&x,&y);
13. MANSI TYAGI
change(x ,y);
printf(“value of x=%d and y=%dn”,x ,y);
}
change(int a,int b);
{
int k;
k=a;
a=b;
b=k;
}
Output: enter two values: 12 ,23
Value of x=12 and y=23
2. Call by reference
Instead of passing the value of variable, address or reference is
passed and the function operate on address of the variable rather than
value.
Here formal argument is alter to the actual argument, it
means formal arguments calls the actual arguments.
Example:-
void main()
{
int a,b;
14. MANSI TYAGI
change(int *,int*);
printf(“enter two values:n”);
scanf(“%d%d”,&a,&b);
change(&a,&b);
printf(“after changing two value of a=%d and b=%dn:”a,b);
}
change(int *a, int *b)
{
int k;
k=*a;
*a=*b;
*b= k;
printf(“value in this function a=%d and b=%dn”,*a,*b);
}
Output: enter two values: 12, 32
Value in this function a=32 and b=12
After changing two value of a=32 and b=12
So here instead of passing value of the variable, directly
passing address of the variables. Formal argument directly
access the value and swapping is possible even after calling a
function.
15. MANSI TYAGI
STORAGE CLASSES:
The storage class in C provides the complete information about the location and
visibility of variables.
To define a variable in C one needs to mention not only its but also its
storage class. In other words , not only do all variables have a data type,
they also have a storage class.
If we do not specify the storage class of a variable in its declaration , the
compiler will resume a storage class dependent on the context in which the
variable is used. Thus C has got certain default storage classes.
Moreover, a variables storage class tells us:
Where the variable would be stored.
What will be the initial value of the variable , if the initial value is not
specifically assigned( i.e. the default initial value)
What is the scope of the variable i.e in which function the value of
the variable would be available.
What is the life of the variable, i.e. how long would the variable exist.
TYPES OF STORAGE CLASSES:
a) Automatic storage class.
b) Register storage class.
c) Static storage class.
d) External storage class.
16. MANSI TYAGI
1. AUTOMATIC VARIABLES: Automatic variables are declared
inside a function in which they are used they are to be utilized.
They are created when the function is called and destroyed
automatically when they are declared. Because of this property,
automatic variables are also referred to as local or internal
variables.
Syntax :
We may also use the key word auto to declare automatic variables.
main()
{
auto int n;
Statements;
}
FEATURES OF AUTOMATIC
1.Storage : Memory
2.Default initial value : A garbage value
3.Scope: Local to the block in which it is declared.
4.Life: As long as the program control remains
within the block in which it is declared.
17. MANSI TYAGI
2.REGISTERS STORAGE CLASSES :
We can tell the compiler that a variable should be kept in one of the machine’s
register, instead of keeping in the memory. Since a register access is much faster
than a memory access. Keeping the frequently accessed variables in the register
will lead to faster execution of programs.
Syntax :
register int i;
3.STATIC STORAGE CLASS:
As the name suggests, the value of static variables persists until the end of the
program. A variable can be declared static using the keyword static.
A static variables may be either an internal type or an external type, depending on
the place of declaration.
main()
{
int i;
FEATURES OF REGISTERS
1.Storage : Memory
2.Default initial value : A garbage value
3.Scope: Local to the block in which it is declared.
4.Life: As long as the program control remains
within the block in which it is declared.
18. MANSI TYAGI
for(i=1;i<=3;i++)
fun();
}
fun()
{
static int x=5;
x=x+3;
printf(“x=%dn”, x);
}
4.EXTERNAL STORAGE CLASS:
Variables that are both alive and active throughout the entire program are known as
external variables. They are also known as global variables. External variables are
declared outside a function.
FEATURES OF STATIC
1.Storage : Memory
2.Default initial value : ZERO
3.Scope: Local to the block in which it is declared.
4.Life:Variables retains its value betwwn different
functions calls.
FEATURES OF STATIC
1.Storage : Memory
2.Default initial value : ZERO
3.Scope: In all functions of a program i.e: global
4.Life: Throughout the program execution.
19. MANSI TYAGI
Recursion
The function called by itself (inside function body) is called
recursive function and this process often referred as
recursion.
In recursion calling function and called function are same.
It is powerful technique of writing complicated algorithm in easiest way.
According to recursion problem is defined in term of itself. Here statement
with in body of the function calls the same function and same times it is
called as circular definition.
In other words recursion is the process of defining something in form of
itself.
Important conditions: There are two important conditions that must be satisfied
by any recursive procedure.
1.Each time a procedure calls itself, it must be nearer to a solution.
2.There must be a decision criterion for stopping the computation.
Types of recursion:
There are two types of recursions.
1.The first type concerns recursively defined functions. Example of this
kind is the Factorial function.
2.The second type of recursion is the recursive use of a procedure.
Syntax:
main ()
{
rec(); /*function call*/
rec();
rec();
20. MANSI TYAGI
Ex:- /*calculate factorial of a no.using recursion*/
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int num;
printf(“enter a number”);
scanf(“%d”,&num);
f=fact(num);
printf(“factorial is =%dn”f);
}
fact (int num)
{
If (num==0||num==1)
return 1;
else
return(num*fact(num-1));
}
21. MANSI TYAGI
IMPORTANT QUESTIONS
Q1. What is user defined functions?what is recursion?what are the
conditions for recursion to be convergent?write a recursive function to
compute factorial of a given number?Make use of this function to
compute the value of NCr.
Q2.Distinguish between Call by value and Call by reference methods of
parameter passings by giving suitable example?
Q3.Explain the concept of Storage classes.Explain eaxh with an
example.
Q4.Explain the concept of variables?
Q5.What is Recursion? Explain with its suitable example.
Q6. What do you understand by functions?Explain with its types.
Q7. What is user defined functions?How can you return a value from a
function?Explain with an example .Also discuss different methods to
pass parameters to a function by giving suitable examples.