Module 4-Modular Programming in C
Module 4-Modular Programming in C
De_nition
MANSI TYAGI
1. Library function
2. User defined function
MANSI TYAGI
So when user gets his own function three thing he has to know, these
are.
Function declaration
Function definition
Function call
}
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.
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.
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
MANSI TYAGI
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.
1. Actual Argument
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.
1. Actual Argument
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
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.
MANSI TYAGI
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.
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=%d\n”,a,b);
}
function 1()
{
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.
MANSI TYAGI
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.
SYNTAX :
void function(void);
main()
{
void function()
{
Statement;
}
MANSI TYAGI
#include<conio.h>
{ Main()
Add();
Int a,b,c;
Scanf(“%d%d”,&a,&b);
C=a+b;
Printf(“%d”,c);
MANSI TYAGI
Example:-
int sum();
main()
int b=sum();
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.
MANSI TYAGI
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;
MANSI TYAGI
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);
MANSI TYAGI
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;
MANSI TYAGI
MANSI TYAGI
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 .
MANSI TYAGI
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.
MANSI TYAGI
Syntax :
register 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.
main()
int i;
MANSI TYAGI
fun();
fun()
x=x+3;
printf(“x=%d\n”, x);
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:
MANSI TYAGIIn all functions of a program i.e: global
itself.
Important conditions: There are two important conditions that must be satisfied
by any recursive procedure.
Types of recursion:
Syntax:
main ()
{
rec(); /*function call*/
rec();
rec();
MANSI TYAGI
#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 =%d\n”f);
If (num==0||num==1)
return 1;
else
return(num*fact(num-1));
MANSI TYAGI
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.
MANSI TYAGI