SlideShare a Scribd company logo
Introduction to modular programming, writing functions, formal
parameters, actual parameters .Pass by Value, Recursion, Arrays
as Function Parameters ,structure, union, Storage Classes, Scope
and life time of variables
MODULE 4
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 1
Functions
• A function is a group of statements
that together perform a task.
• Every C program has at least one
function, which is main().
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 2
Why to use Functions?
• Development can be divided
• Readable programs
• Programming errors are easy to detect
• Allows re-use of codes
• Improves manageability
• Collaboration
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 3
Types of Functions
There are two types of functions in C programming:
• Standard library functions
• User-defined functions
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 4
Standard Library Functions
• The standard library functions are built-in functions
in C programming.
• These functions are defined in header files.
For example,
• The printf() is defined in the stdio.h header file.
• The sqrt() function is defined in the math.h header
file.
• The strlen() function is defined in the string.h
header file.6/30/2020 NIMMY RAJU,AP,VKCET,TVM 5
User-defined function
• User can also create functions as per their own
needs.
• Such functions created by users are known as
user-defined functions.
Advantages of user-defined function
• easier to understand, maintain and debug.
• Reusable codes
• A large program can be divided among many
programmers.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 6
• Three parts of a user defined functions are:
– Function Declaration or Prototype
– Function Definition
– Function Call
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 7
Function Declaration or function prototype
• A function prototype or function declaration is simply
the declaration of a function that specifies function's
name, parameters and return type.
• It doesn't contain function body.
• A function prototype gives information to the compiler
that the function may later be used in the program.
Syntax
• return_type function_name( parameter list );
• int add(int a, int b);6/30/2020 NIMMY RAJU,AP,VKCET,TVM 8
Function Definition
• A function definition in C programming consists
of a function header and a function body. Here
are all the parts of a function:
Return Type:
• A function may return a value.
• The return_type is the data type of the value the
function returns.
• Some functions perform the desired operations
without returning a value.
• In this case, the return_type is the keyword void.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 9
Function Definition
• Function Name: The actual name of the
function.
• Arguments: When a function is invoked, you
pass a value to the parameter. This value is
referred to as argument.
• Function Body: The function body contains a
collection of statements that define what the
function does.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 10
• Syntax:
return_type function_name( argument list )
{
body of the function
}
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 11
Function Call
• When a program calls a function from the
main function , the program control is
transferred to the called function.
• 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.
Syntax:
• functionName(parameter list);
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 12
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 13
• Return Statement
• The return statement terminates the
execution of a function and returns a value to
the calling function.
• The program control is transferred to the
calling function after the return statement.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 14
Types of user defined functions
Types INPUT OUTPUT
No arguments passed and
no return value
Sub function Sub function
Arguments passed but no
return value
Main function Sub function
No arguments passed but
return value
Sub function Main function
Arguments passed with
return value
Main function Main function
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 15
Without function Using function(No arguments passed and
no return value
)
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter the values of
a & b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
#include<stdio.h>
void add();
void main()
{
add();
}
// function to read two number and
print its sum
void add()
{
int a,b,sum;
printf("Enter the values of a & b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
Program to find the sum of two numbers
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 16
Without function Using function(arguments passed and no
return value
)
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter the values of a
& b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
#include<stdio.h>
void add(int ,int );
void main()
{
int a,b;
printf("Enter the values of a & b");
scanf("%d%d",&a,&b);
add(a,b);
}
// function that takes two arguments
and print their sum.
void add(int a,int b)
{
int sum= a + b;
printf("Sum=%d",sum);
}
Program to find the sum of two numbers
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 17
Without function Using function(No arguments passed and but
return value
)
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter the values of a
& b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
#include<stdio.h>
int add( );
void main()
{
int S;
S=add();
printf("Sum=%d",S);
}
// function to read two number and
return its sum
int add()
{
int a,b,sum;
printf("Enter the values of a & b");
scanf("%d%d",&a,&b);
sum = a+b;
return sum;
}6/30/2020 NIMMY RAJU,AP,VKCET,TVM 18
Without function Using function(arguments passed with return
value
)
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter the values of a
& b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("Sum=%d",sum);
}
#include<stdio.h>
int add(int ,int );
void main()
{
int a,b,S;
printf("Enter the values of a & b");
scanf("%d%d",&a,&b);
S=add(a,b);
printf("Sum=%d",S);
}
// function that takes two arguments
and print their sum.
int add(int a,int b)
{
int sum;
sum = a+b;
return sum;
}
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 19
No arguments
passed and no
return value
No arguments
passed and no return
value
No arguments passed
and no return value
No arguments passed
and no return value
#include<stdio.h>
void large();
void main()
{
large();
}
void large()
{
int a,b;
printf("Enter the
values of a & b");
scanf("%d%d",&a,&b)
;
If(a>b)
printf(“Largest=%d",a
);
else
printf(“Largest=%d“,a
);
}
#include<stdio.h>
void large(int,int);
void main()
{
int a,b;
printf("Enter the values
of a & b");
scanf("%d%d",&a,&b);
large(a,b);
}
void large(int x,int y)
{
If(x>y)
printf(“Largest=%d",x);
else
printf(“Largest=%d“,y);
}
#include<stdio.h>
int large();
void main()
{
int l;
l=large();
printf(“Large=%d”,l);
}
int large()
{
int a,b;
printf("Enter the values of
a & b");
scanf("%d%d",&a,&b);
If(a>b)
return a;
else
return b;
}
#include<stdio.h>
int large(int,int);
void main()
{
int a,b,l;
printf("Enter the values
of a & b");
scanf("%d%d",&a,&b);
l=large(a,b);
printf(“Large=%d”,l);
int large(int a,int b)
{
If(a>b)
return a;
else
return b;
}
Largest of two numbers
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 20
• Formal and Actual Parameters
• Let us assume that a function B() is called
from another function A().
• In this case A is called the “caller functionor
calling function” and B is called the “called
function or callee function”.
• Also, the arguments which A sends to B are
called actual arguments and the parameters
of B are called formal arguments.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 21
• Formal Parameter: A variable and its type as
they appear in the prototype of the function
or method.
• Actual Parameter: The variable or expression
corresponding to a formal parameter that
appears in the function or method call in the
calling environment.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 22
Formal and Actual parameters
 Let us assume that a function B() is called from another function
A(). In this case A is called the “caller function or calling
function” and B is called the “called function or callee
function”.
 The arguments which A sends to B are called actual arguments
(parameters)and the arguments of B are called formal
arguments(parameters).
 Formal Parameter: A variable that appears in the prototype of
the function.
 Actual Parameter: The variable corresponding to a formal
parameter that appears in the function call in the calling function.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 23
Example:
#include<stdio.h>
int swap(int a,int b); //function declaration
void main()
{
int x=10,y=20;
printf("Before swapping : x=%dty=%dn",x,y);
swap(x,y); //function call
printf("After swapping : x=%dty=%d",x,y);
}
int swap(int a,int b) //function definition
{
int temp=a;
a=b;
b=temp;
printf("a=%dt b=%d",a,b);
}
Output
Before swapping: x=10 y=20
a=20 b=10
After swapping : x=10 y=20
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 24
Pass by value(Call by value)
• In this parameter passing technique, a copy of
actual parameter is passed to formal parameter.
• As a result, any changes or modification
happened to formal parameter won’t reflect
back to actual parameter
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 25
Example:
#include<stdio.h>
int swap(int a,int b); //function declaration
void main()
{
int x=10,y=20;
printf("Before swapping : x=%dty=%dn",x,y);
swap(x,y); //function call –pass by value
printf("After swapping : x=%dty=%d",x,y);
}
int swap(int a,int b) //function definition
{
int temp=a;
a=b;
b=temp;
printf("a=%dt b=%d",a,b);
}
Output
Before swapping: x=10 y=20
a=20 b=10
After swapping : x=10 y=20
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 26
Pass by reference(Call by reference)
• Both the actual and formal parameters refer to
same locations, so any changes made inside
the function are actually reflected in actual
parameters of caller.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 27
Example:
#include<stdio.h>
int swap(int *a,int *b); //function declaration
void main()
{
int x=10,y=20;
printf("Before swapping : x=%dty=%dn",x,y);
swap(&x,&y); //function call –pass by reference
printf("After swapping : x=%dty=%d",x,y);
}
int swap(int *a,int *b) //function definition
{
int temp=*a;
*a=*b;
*b=temp;
printf("a=%dt b=%d",*a,*b);
}
Output
Before swapping: x=10 y=20
a=20 b=10
After swapping : x=20 y=10
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 28
Recursive Function
• A function that calls itself is known as a
recursive function. And, this technique is known as
recursion.
• While using recursion, programmers need to be
careful to define an exit condition from the
function, otherwise it will go into an infinite loop.
• Recursion makes program elegant.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 29
Write a program to find factorial of number using recursive function
#include<stdio.h>
int fact(int n)
{
if( n == 0)
return 1;
return n*fact(n-1);
}
void main()
{
int n,f;
printf("Enter the number:");
scanf("%d",&n);
f-=fact(n);
printf("factorial=%d",f);
}
Output
Enter the number:5
factorial=120
fact(5) fact(4) fact(3) fact(2) fact(1) fact(0)
fact(5)
{
return
5*fact(4);
}
fact(4)
{
return
4*fact(3);
}
fact(3)
{
return
3*fact(2);
}
fact(2)
{
return
2*fact(1);
}
fact(1)
{
return
1*fact(0);
}
fact(0)
{
return 1;
}
5*24=120 4*6=24 3*2=6 2*1=2 1*1=1 1
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 30
Write a program to find sum of first n natural numbers using recursive function
#include<stdio.h>
int sum(int n)
{
if( n == 0)
return 0;
return n+sum(n-1);
}
void main()
{
int n,f;
printf("Enter the limit:");
scanf("%d",&n);
printf(“n Sum=%d",sum(n));
}
Output
Enter the limit:5
factorial=15
sum(5) sum(4) sum(3) sum(2) sum(1) sum(0)
sum(5)
{
return
5+sum(4);
}
sum(4)
{
return
4+sum(3);
}
sum(3)
{
return
3+sum(2);
}
sum(2)
{
return
2+sum(1);
}
sum(1)
{
return
1+sum(0);
}
sum(0)
{
return 0;
}
5+10=15 4+6=10 3+3=6 2+1=3 1+0=1 0
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 31
Passing an array as Parameter
 Like the value of simple variables, it is also
possible to pass the values of an array to a
function.
 To pass a single dimensional array to a
function, it is sufficient to list the name of the
array without any subscripts and the size of
the array as arguments
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 32
Rules to pass an Array to Function
 The function must be called by passing only the
name of the array.
 In function definition, formal parameter must be
an array type; the size of the array does not need
to be specified.
 The function prototype must show that the
argument is an array.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 33
Write a function to sort an array.
#include<stdio.h>
int sort(int A[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(A[j]>A[j+1])
{
temp = A[j];
A[j] = A[j+1];
A[j+1]= temp;
}
}
}
void main()
{
int A[30],i,n;
printf("Enter the limit:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the
element:");
scanf("%d",&A[i]);
}
sort(A,n);
printf("Sorted Arrayn");
for(i=0;i<n;i++)
printf("%dt",A[i]);
}
Output
Enter the limit:5
Enter the element:17
Enter the element:23
Enter the element:5
Enter the element:2
Enter the element:9
Sorted Array
2 5 9
17 23
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 34
• Passing 2 D array as parameter
• While passing 2D as parameter we need to
mention the max size of element of each row
that is column
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 35
Write a program to pass a 2 D matrix as parameter and find its sum of all the elements
#include<stdio.h>
// function that takes a 2 D
and its order
int sum(int A[][30],int m,int
n)
{
int i,j,sum =0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum = sum + A[i][j];
}
}
printf("Sum=%d",sum);
}
} void main()
{
int A[30][30];
int i,n,m,j;
printf("Enter the order of
matrix:");
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the element:");
scanf("%d",&A[i][j]);
}
}
sum(A,m,n);
}
Output
Enter the order of
matrix:2 2
Enter the element:1
Enter the element:2
Enter the element:3
Enter the element:4
Sum=10
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 36
STRUCTURE
• A structure is a user defined data type in C.
• A structure creates a data type that can be
used to group items of possibly different types
into a single type.
• ‘struct’ keyword is used to create a structure.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 37
• Consider we want to create the structure of a
person with following variable name, age and
address.
• Then such a structure can be created as
struct Person
{
char name[30];
int age;
char addr[50];
}p;
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 38
The general format of a structure definition is as
follows
struct structure_name
{
data_type member1;
data_type member2;
---------------------
---------------------
}S;
Here S is called structure variable.6/30/2020 NIMMY RAJU,AP,VKCET,TVM 39
• A structure variable can either be declared
with structure declaration or as a separate
declaration like basic types.
struct structure_name
{
data_type member1;
data_type member2;
---------------------
---------------------
}:
void main()
{
struct structure_name S;
}
struct structure_name
{
data_type member1;
data_type member2;
---------------------
---------------------
}S;
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 40
Declare a structure namely Student to store the details (roll number,
name, mark_for_C) of two students and display the data.
#include<stdio.h>
struct Student
{
char name[30];
int rollnum;
int mark;
};
void main()
{
struct Student s1,s2;
printf("Enter the Student name:");
scanf("%s",s1.name);
printf("Enter the Student rollnum:");
scanf("%d",&s1.rollnum);
printf("Enter the Student Mark for C:");
scanf("%d",&s1.mark);
printf("Enter the Student name:");
scanf("%s",s2.name);
printf("Enter the Student rollnum:");
scanf("%d",&s2.rollnum);
printf("Enter the Student Mark for C:");
scanf("%d",&s2.mark);
printf("NametRoll NumbertMark for Cn");
printf("%st%dt%dn",s1.name,s1.rollnum,s1.m
ark);
printf("%st%dt%dn",s2.name,s2.rollnum,s2.m
ark);
}
Output
Enter the Student name:Sangeeth
Enter the Student rollnum:
Enter the Student Mark for C:35
Enter the Student name:Pratheesh
Enter the Student rollnum:24
Enter the Student Mark for C:40
Name Roll Number Mark for C
Sangeeth 27 35
Pratheesh 24 40
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 41
Array of Structures
 DeclareastructurenamelyStudenttostorethedetails(roll number, name, mark_for_C)
of a student. Then, write a program in C to find the average mark obtained by the
students in a class for the subject Programming in C (using the field mark_for_C). Use
array of structures to store the required data.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 42
Declare a structure namely Student to store the details (roll number,
name, mark_for_C) of two students and display the data.
#include<stdio.h>
struct Student
{
char name[30];
int rollnum;
int mark;
};
void main()
{
struct Student s1,s2;
printf("Enter the Student name:");
scanf("%s",s1.name);
printf("Enter the Student rollnum:");
scanf("%d",&s1.rollnum);
printf("Enter the Student Mark for C:");
scanf("%d",&s1.mark);
printf("Enter the Student name:");
scanf("%s",s2.name);
printf("Enter the Student rollnum:");
scanf("%d",&s2.rollnum);
printf("Enter the Student Mark for C:");
scanf("%d",&s2.mark);
printf("NametRoll NumbertMark for Cn");
printf("%st%dt%dn",s1.name,s1.rollnum,s1.m
ark);
printf("%st%dt%dn",s2.name,s2.rollnum,s2.m
ark);
}
Output
Enter the Student name:Sangeeth
Enter the Student rollnum:
Enter the Student Mark for C:35
Enter the Student name:Pratheesh
Enter the Student rollnum:24
Enter the Student Mark for C:40
Name Roll Number Mark for C
Sangeeth 27 35
Pratheesh 24 40
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 43
#include<stdio.h>
struct Student
{
char name[30];
int rollno;
int mark_for_C;
};
void main()
{
struct Student s[30];
int i,n,sum=0;
float avg;
printf("Enter the no
of Student:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Student
name:");
scanf("%s",s[i].name);
printf("Enter the Student
rollno:");
scanf("%d",&s[i].rollno);
printf("Enter the Mark for C:");
scanf("%d",&s[i].mark_for_C);
}
printf("NametRoll
NumbertMark for Cn");
for(i=0;i<n;i++)
{
printf("%st%dt%dn",s[i].name,
s[i].rollnum,s[i].mark_for_C);
sum = sum + s[i].mark_for_C;
}
avg = (float)sum /n;
printf("Average Mark=%fn",avg);
}
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 44
Output
Enter the no of Student:3
Enter the Student name:Sangeeth
Enter the Student rollnum:27
Enter the Student Mark for C:35
Enter the Student name:Pratheesh
Enter the Student rollnum:24
Enter the Student Mark for C:40
Enter the Student name:Poornima
Enter the Student rollnum:26
Enter the Student Mark for C:45
Name Roll Number Mark for C
Sangeeth 27 35
Pratheesh 24 40
Poornima 26 45
Average Mark=40.00
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 45
UNION
 A union is a user-defined type similar to struct in C
programming.
 We use the union keyword to define unions.
Example of Employee Union creation and declartion
union Employee
{
char name[30]; int age;
double salary;
};
union Employee e;
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 46
Difference between structure and union
Structure Union
struct keyword is used to define a structure union keyword is used to define a union
Members do not share memory in a structure. Members share the memory space in a union
Any member can be retrieved at any time in a
structure
Only one member can be accessed at a time
in a union.
Several members of a structure can be
initialized at once.
Only the first member can be initialized.
Sizeofthestructureisequalto thesumofsize
of the each member.
Size of the union is equal to the size of the
largest member.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 47
#include<stdio.h>
struct Person
{
char pincode[6]; // Size = 6 bytes
int age; // Size = 4 bytes
double salary;// Size = 8 bytes
};
union Employee
{
char pincode[6];
int age;
double salary;
};
void main()
{
struct Person p;
union Employee e;
printf("Size of Structure Person=%dn",sizeof(p));
printf("Size of Union Employee=%d",sizeof(e));
}
Output
Size of Structure Person=18
Size of Union Employee=86/30/2020 NIMMY RAJU,AP,VKCET,TVM 48
Storage Classes, Scope and life
time of variables
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 49
• In C, not only do all the variables have a data
type, they also have a storage class.
• The following variable storage classes are
most relevant to functions
– Automatic Variables
– External or Global Variables
– Static Variables
– Register Variables
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 50
Automatic Variables
• Automatic variables are declared inside a
function in which they are to be utilized.
• They are created when the function is called
and destroyed automatically when the
function is exited, hence the name automatic.
• Automatic variables are therefore private or
local to the function in which they are
declared.
• Because of this property, automatic variables
are also referred to as local or internal
variables.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 51
Automatic Variables
• A variable declared inside a function without
storage class specification is by default an
automatic variable.
void main()
{
int num;
}
is same as
void main()
{
auto int num;
}
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 52
• Example
#include<stdio.h>
void func1()
{
int max=10;
printf("Max in func1()=%dn",max);
}
void func2()
{
int max=20;
printf("Max in func2()=%dn",max);
}
void main()
{
int max=30;
func1();
func2();
printf("Max in main()=%dn",max);
}
Output
Max in func1()=10
Max in func2()=20
Max in main()=30
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 53
External Variables
• Variables that are both alive and active
throughout the entire program are known as
external variables.
• They are called global variables.
• Unlike local variables, global variables can be
accessed by any function in the program.
• External global variables are global variables
visible to all file other than in which it is
declared6/30/2020 NIMMY RAJU,AP,VKCET,TVM 54
#include<stdio.h>
int max;
void func1()
{
printf("Max in func1()=%dn",max);
}
void func2()
{
printf("Max in func2()=%dn",max);
}
void main()
{
max=30;
func1();
func2();
printf("Max in main()=%dn",max);
}
Max in func1()=30
Max in func1()=30
Max in main()=30
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 55
Static Variables
• 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 like
static int x;
• Static global variables are global variables
visible only to the file in which it is declared.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 56
Register Variables
• We can tell the compiler that a variable should be
kept in one of the machine’s registers instead of
keeping in the memory.
• Since a register access is fast than a memory
access, keep in the frequently accessed variables
in the register will lead to faster execution of
programs.
• This is done as follows
register int count;
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 57
• The scope of a variable is the part of the
program within which the variable can be
used. So, the scope describes the visibility of
an identifier within the program.
• The lifetime of a variable or function is the
time duration for which memory is allocated
to store it, and when that memory is released.
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 58
StorageClass Wheredeclared Visibility Lifetime
extern
Beforeallfunctionsinafile
Entire file plus other
files where variable is
declared.
Global
Static
Beforeallthefunctioninafile Onlyinthatfile
Global
Noneorauto
Inside afunction
Only in that function
orblock
Until end of
function
Register
Inside afunctionorblock
Onlyinthatfunction
orblock
Until end of
function
6/30/2020 NIMMY RAJU,AP,VKCET,TVM 59
Ad

Recommended

EST 102 Programming in C-MODULE 5
EST 102 Programming in C-MODULE 5
NIMMYRAJU
 
EST 102 Programming in C
EST 102 Programming in C
NIMMYRAJU
 
Code Optimization using Code Re-ordering
Code Optimization using Code Re-ordering
Arangs Manickam
 
ΠΛΗ20 ΤΥΠΟΛΟΓΙΟ ΕΝΟΤΗΤΑΣ 4
ΠΛΗ20 ΤΥΠΟΛΟΓΙΟ ΕΝΟΤΗΤΑΣ 4
Dimitris Psounis
 
Arteris network on chip: The growing cost of wires
Arteris network on chip: The growing cost of wires
Arteris
 
Hashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Binary tree
Binary tree
Rajendran
 
ADT STACK and Queues
ADT STACK and Queues
BHARATH KUMAR
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Make Mannan
 
Compiler: Syntax Analysis
Compiler: Syntax Analysis
Dr. Jaydeep Patil
 
Astar algorithm
Astar algorithm
Shuqing Zhang
 
8 number-system
8 number-system
Rohit Shrivastava
 
ΠΛΗ20 ΕΠΑΝΑΛΗΨΗ 1
ΠΛΗ20 ΕΠΑΝΑΛΗΨΗ 1
Dimitris Psounis
 
Data structure lecture 1
Data structure lecture 1
Kumar
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables
Nifras Ismail
 
Branch and bounding : Data structures
Branch and bounding : Data structures
Kàŕtheek Jåvvàjí
 
Selection sorting
Selection sorting
Himanshu Kesharwani
 
Distance Vector & Link state Routing Algorithm
Distance Vector & Link state Routing Algorithm
MOHIT AGARWAL
 
NFA Converted to DFA , Minimization of DFA , Transition Diagram
NFA Converted to DFA , Minimization of DFA , Transition Diagram
Abdullah Jan
 
Theory of Computation Unit 3
Theory of Computation Unit 3
Jena Catherine Bel D
 
Chapter 13
Chapter 13
Faisal Mehmood
 
ΠΛΗ20 ΤΥΠΟΛΟΓΙΟ ΣΥΝΔΥΑΣΤΙΚΗΣ
ΠΛΗ20 ΤΥΠΟΛΟΓΙΟ ΣΥΝΔΥΑΣΤΙΚΗΣ
Dimitris Psounis
 
Τεχνολογία Δικτύων Επικοινωνιών Θεωρία κεφ. 6
Τεχνολογία Δικτύων Επικοινωνιών Θεωρία κεφ. 6
Theodoros Leftheroudis
 
Arp and rarp
Arp and rarp
Mohd Arif
 
Theory of Computation Unit 2
Theory of Computation Unit 2
Jena Catherine Bel D
 
Arithmetic Expression
Arithmetic Expression
Mahmud Hasan Tanvir
 
ΦΕ. ΠΙΕΣΗ.docx
ΦΕ. ΠΙΕΣΗ.docx
chris09xgames
 
Trees
Trees
Selvaraj Seerangan
 
Unit 4 (1)
Unit 4 (1)
psaravanan1985
 
unit_2.pptx
unit_2.pptx
Venkatesh Goud
 

More Related Content

What's hot (20)

Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Make Mannan
 
Compiler: Syntax Analysis
Compiler: Syntax Analysis
Dr. Jaydeep Patil
 
Astar algorithm
Astar algorithm
Shuqing Zhang
 
8 number-system
8 number-system
Rohit Shrivastava
 
ΠΛΗ20 ΕΠΑΝΑΛΗΨΗ 1
ΠΛΗ20 ΕΠΑΝΑΛΗΨΗ 1
Dimitris Psounis
 
Data structure lecture 1
Data structure lecture 1
Kumar
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables
Nifras Ismail
 
Branch and bounding : Data structures
Branch and bounding : Data structures
Kàŕtheek Jåvvàjí
 
Selection sorting
Selection sorting
Himanshu Kesharwani
 
Distance Vector & Link state Routing Algorithm
Distance Vector & Link state Routing Algorithm
MOHIT AGARWAL
 
NFA Converted to DFA , Minimization of DFA , Transition Diagram
NFA Converted to DFA , Minimization of DFA , Transition Diagram
Abdullah Jan
 
Theory of Computation Unit 3
Theory of Computation Unit 3
Jena Catherine Bel D
 
Chapter 13
Chapter 13
Faisal Mehmood
 
ΠΛΗ20 ΤΥΠΟΛΟΓΙΟ ΣΥΝΔΥΑΣΤΙΚΗΣ
ΠΛΗ20 ΤΥΠΟΛΟΓΙΟ ΣΥΝΔΥΑΣΤΙΚΗΣ
Dimitris Psounis
 
Τεχνολογία Δικτύων Επικοινωνιών Θεωρία κεφ. 6
Τεχνολογία Δικτύων Επικοινωνιών Θεωρία κεφ. 6
Theodoros Leftheroudis
 
Arp and rarp
Arp and rarp
Mohd Arif
 
Theory of Computation Unit 2
Theory of Computation Unit 2
Jena Catherine Bel D
 
Arithmetic Expression
Arithmetic Expression
Mahmud Hasan Tanvir
 
ΦΕ. ΠΙΕΣΗ.docx
ΦΕ. ΠΙΕΣΗ.docx
chris09xgames
 
Trees
Trees
Selvaraj Seerangan
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Make Mannan
 
ΠΛΗ20 ΕΠΑΝΑΛΗΨΗ 1
ΠΛΗ20 ΕΠΑΝΑΛΗΨΗ 1
Dimitris Psounis
 
Data structure lecture 1
Data structure lecture 1
Kumar
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables
Nifras Ismail
 
Branch and bounding : Data structures
Branch and bounding : Data structures
Kàŕtheek Jåvvàjí
 
Distance Vector & Link state Routing Algorithm
Distance Vector & Link state Routing Algorithm
MOHIT AGARWAL
 
NFA Converted to DFA , Minimization of DFA , Transition Diagram
NFA Converted to DFA , Minimization of DFA , Transition Diagram
Abdullah Jan
 
ΠΛΗ20 ΤΥΠΟΛΟΓΙΟ ΣΥΝΔΥΑΣΤΙΚΗΣ
ΠΛΗ20 ΤΥΠΟΛΟΓΙΟ ΣΥΝΔΥΑΣΤΙΚΗΣ
Dimitris Psounis
 
Τεχνολογία Δικτύων Επικοινωνιών Θεωρία κεφ. 6
Τεχνολογία Δικτύων Επικοινωνιών Θεωρία κεφ. 6
Theodoros Leftheroudis
 
Arp and rarp
Arp and rarp
Mohd Arif
 
ΦΕ. ΠΙΕΣΗ.docx
ΦΕ. ΠΙΕΣΗ.docx
chris09xgames
 

Similar to EST 102 Programming in C-MODULE 4 (20)

Unit 4 (1)
Unit 4 (1)
psaravanan1985
 
unit_2.pptx
unit_2.pptx
Venkatesh Goud
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
unit_2 (1).pptx
unit_2 (1).pptx
JVenkateshGoud
 
Functions
Functions
Munazza-Mah-Jabeen
 
Detailed concept of function in c programming
Detailed concept of function in c programming
anjanasharma77573
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
ArshiniGubbala3
 
Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran
 
Functions
Functions
Dr.Subha Krishna
 
Module 3-Functions
Module 3-Functions
nikshaikh786
 
Functions
Functions
Golda Margret Sheeba J
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
Principals of Programming in CModule -5.pdfModule-3.pdf
Principals of Programming in CModule -5.pdfModule-3.pdf
anilcsbs
 
Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi
 
Dti2143 chapter 5
Dti2143 chapter 5
alish sha
 
C and C++ functions
C and C++ functions
kavitha muneeshwaran
 
11 functions
11 functions
Rohit Shrivastava
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
JAVVAJI VENKATA RAO
 
Function
Function
Kathmandu University
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
sangeeta borde
 
Ad

More from NIMMYRAJU (8)

EST 102 Programming in C-MODULE 1
EST 102 Programming in C-MODULE 1
NIMMYRAJU
 
EST 102 Programming in C-MODULE 2
EST 102 Programming in C-MODULE 2
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -PROBLEMS
CS 402 DATAMINING AND WAREHOUSING -PROBLEMS
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 6
CS 402 DATAMINING AND WAREHOUSING -MODULE 6
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 5
CS 402 DATAMINING AND WAREHOUSING -MODULE 5
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 4
CS 402 DATAMINING AND WAREHOUSING -MODULE 4
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 3
CS 402 DATAMINING AND WAREHOUSING -MODULE 3
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 2
CS 402 DATAMINING AND WAREHOUSING -MODULE 2
NIMMYRAJU
 
EST 102 Programming in C-MODULE 1
EST 102 Programming in C-MODULE 1
NIMMYRAJU
 
EST 102 Programming in C-MODULE 2
EST 102 Programming in C-MODULE 2
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -PROBLEMS
CS 402 DATAMINING AND WAREHOUSING -PROBLEMS
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 6
CS 402 DATAMINING AND WAREHOUSING -MODULE 6
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 5
CS 402 DATAMINING AND WAREHOUSING -MODULE 5
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 4
CS 402 DATAMINING AND WAREHOUSING -MODULE 4
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 3
CS 402 DATAMINING AND WAREHOUSING -MODULE 3
NIMMYRAJU
 
CS 402 DATAMINING AND WAREHOUSING -MODULE 2
CS 402 DATAMINING AND WAREHOUSING -MODULE 2
NIMMYRAJU
 
Ad

Recently uploaded (20)

Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Great power lithium iron phosphate cells
Great power lithium iron phosphate cells
salmankhan835951
 
Cadastral Maps
Cadastral Maps
Google
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
Great power lithium iron phosphate cells
Great power lithium iron phosphate cells
salmankhan835951
 
Cadastral Maps
Cadastral Maps
Google
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 

EST 102 Programming in C-MODULE 4

  • 1. Introduction to modular programming, writing functions, formal parameters, actual parameters .Pass by Value, Recursion, Arrays as Function Parameters ,structure, union, Storage Classes, Scope and life time of variables MODULE 4 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 1
  • 2. Functions • A function is a group of statements that together perform a task. • Every C program has at least one function, which is main(). 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 2
  • 3. Why to use Functions? • Development can be divided • Readable programs • Programming errors are easy to detect • Allows re-use of codes • Improves manageability • Collaboration 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 3
  • 4. Types of Functions There are two types of functions in C programming: • Standard library functions • User-defined functions 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 4
  • 5. Standard Library Functions • The standard library functions are built-in functions in C programming. • These functions are defined in header files. For example, • The printf() is defined in the stdio.h header file. • The sqrt() function is defined in the math.h header file. • The strlen() function is defined in the string.h header file.6/30/2020 NIMMY RAJU,AP,VKCET,TVM 5
  • 6. User-defined function • User can also create functions as per their own needs. • Such functions created by users are known as user-defined functions. Advantages of user-defined function • easier to understand, maintain and debug. • Reusable codes • A large program can be divided among many programmers. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 6
  • 7. • Three parts of a user defined functions are: – Function Declaration or Prototype – Function Definition – Function Call 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 7
  • 8. Function Declaration or function prototype • A function prototype or function declaration is simply the declaration of a function that specifies function's name, parameters and return type. • It doesn't contain function body. • A function prototype gives information to the compiler that the function may later be used in the program. Syntax • return_type function_name( parameter list ); • int add(int a, int b);6/30/2020 NIMMY RAJU,AP,VKCET,TVM 8
  • 9. Function Definition • A function definition in C programming consists of a function header and a function body. Here are all the parts of a function: Return Type: • A function may return a value. • The return_type is the data type of the value the function returns. • Some functions perform the desired operations without returning a value. • In this case, the return_type is the keyword void. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 9
  • 10. Function Definition • Function Name: The actual name of the function. • Arguments: When a function is invoked, you pass a value to the parameter. This value is referred to as argument. • Function Body: The function body contains a collection of statements that define what the function does. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 10
  • 11. • Syntax: return_type function_name( argument list ) { body of the function } 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 11
  • 12. Function Call • When a program calls a function from the main function , the program control is transferred to the called function. • 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. Syntax: • functionName(parameter list); 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 12
  • 14. • Return Statement • The return statement terminates the execution of a function and returns a value to the calling function. • The program control is transferred to the calling function after the return statement. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 14
  • 15. Types of user defined functions Types INPUT OUTPUT No arguments passed and no return value Sub function Sub function Arguments passed but no return value Main function Sub function No arguments passed but return value Sub function Main function Arguments passed with return value Main function Main function 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 15
  • 16. Without function Using function(No arguments passed and no return value ) #include<stdio.h> void main() { int a,b,sum; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); sum=a+b; printf("Sum=%d",sum); } #include<stdio.h> void add(); void main() { add(); } // function to read two number and print its sum void add() { int a,b,sum; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); sum=a+b; printf("Sum=%d",sum); } Program to find the sum of two numbers 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 16
  • 17. Without function Using function(arguments passed and no return value ) #include<stdio.h> void main() { int a,b,sum; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); sum=a+b; printf("Sum=%d",sum); } #include<stdio.h> void add(int ,int ); void main() { int a,b; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); add(a,b); } // function that takes two arguments and print their sum. void add(int a,int b) { int sum= a + b; printf("Sum=%d",sum); } Program to find the sum of two numbers 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 17
  • 18. Without function Using function(No arguments passed and but return value ) #include<stdio.h> void main() { int a,b,sum; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); sum=a+b; printf("Sum=%d",sum); } #include<stdio.h> int add( ); void main() { int S; S=add(); printf("Sum=%d",S); } // function to read two number and return its sum int add() { int a,b,sum; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); sum = a+b; return sum; }6/30/2020 NIMMY RAJU,AP,VKCET,TVM 18
  • 19. Without function Using function(arguments passed with return value ) #include<stdio.h> void main() { int a,b,sum; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); sum=a+b; printf("Sum=%d",sum); } #include<stdio.h> int add(int ,int ); void main() { int a,b,S; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); S=add(a,b); printf("Sum=%d",S); } // function that takes two arguments and print their sum. int add(int a,int b) { int sum; sum = a+b; return sum; } 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 19
  • 20. No arguments passed and no return value No arguments passed and no return value No arguments passed and no return value No arguments passed and no return value #include<stdio.h> void large(); void main() { large(); } void large() { int a,b; printf("Enter the values of a & b"); scanf("%d%d",&a,&b) ; If(a>b) printf(“Largest=%d",a ); else printf(“Largest=%d“,a ); } #include<stdio.h> void large(int,int); void main() { int a,b; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); large(a,b); } void large(int x,int y) { If(x>y) printf(“Largest=%d",x); else printf(“Largest=%d“,y); } #include<stdio.h> int large(); void main() { int l; l=large(); printf(“Large=%d”,l); } int large() { int a,b; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); If(a>b) return a; else return b; } #include<stdio.h> int large(int,int); void main() { int a,b,l; printf("Enter the values of a & b"); scanf("%d%d",&a,&b); l=large(a,b); printf(“Large=%d”,l); int large(int a,int b) { If(a>b) return a; else return b; } Largest of two numbers 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 20
  • 21. • Formal and Actual Parameters • Let us assume that a function B() is called from another function A(). • In this case A is called the “caller functionor calling function” and B is called the “called function or callee function”. • Also, the arguments which A sends to B are called actual arguments and the parameters of B are called formal arguments. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 21
  • 22. • Formal Parameter: A variable and its type as they appear in the prototype of the function or method. • Actual Parameter: The variable or expression corresponding to a formal parameter that appears in the function or method call in the calling environment. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 22
  • 23. Formal and Actual parameters  Let us assume that a function B() is called from another function A(). In this case A is called the “caller function or calling function” and B is called the “called function or callee function”.  The arguments which A sends to B are called actual arguments (parameters)and the arguments of B are called formal arguments(parameters).  Formal Parameter: A variable that appears in the prototype of the function.  Actual Parameter: The variable corresponding to a formal parameter that appears in the function call in the calling function. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 23
  • 24. Example: #include<stdio.h> int swap(int a,int b); //function declaration void main() { int x=10,y=20; printf("Before swapping : x=%dty=%dn",x,y); swap(x,y); //function call printf("After swapping : x=%dty=%d",x,y); } int swap(int a,int b) //function definition { int temp=a; a=b; b=temp; printf("a=%dt b=%d",a,b); } Output Before swapping: x=10 y=20 a=20 b=10 After swapping : x=10 y=20 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 24
  • 25. Pass by value(Call by value) • In this parameter passing technique, a copy of actual parameter is passed to formal parameter. • As a result, any changes or modification happened to formal parameter won’t reflect back to actual parameter 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 25
  • 26. Example: #include<stdio.h> int swap(int a,int b); //function declaration void main() { int x=10,y=20; printf("Before swapping : x=%dty=%dn",x,y); swap(x,y); //function call –pass by value printf("After swapping : x=%dty=%d",x,y); } int swap(int a,int b) //function definition { int temp=a; a=b; b=temp; printf("a=%dt b=%d",a,b); } Output Before swapping: x=10 y=20 a=20 b=10 After swapping : x=10 y=20 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 26
  • 27. Pass by reference(Call by reference) • Both the actual and formal parameters refer to same locations, so any changes made inside the function are actually reflected in actual parameters of caller. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 27
  • 28. Example: #include<stdio.h> int swap(int *a,int *b); //function declaration void main() { int x=10,y=20; printf("Before swapping : x=%dty=%dn",x,y); swap(&x,&y); //function call –pass by reference printf("After swapping : x=%dty=%d",x,y); } int swap(int *a,int *b) //function definition { int temp=*a; *a=*b; *b=temp; printf("a=%dt b=%d",*a,*b); } Output Before swapping: x=10 y=20 a=20 b=10 After swapping : x=20 y=10 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 28
  • 29. Recursive Function • A function that calls itself is known as a recursive function. And, this technique is known as recursion. • While using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop. • Recursion makes program elegant. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 29
  • 30. Write a program to find factorial of number using recursive function #include<stdio.h> int fact(int n) { if( n == 0) return 1; return n*fact(n-1); } void main() { int n,f; printf("Enter the number:"); scanf("%d",&n); f-=fact(n); printf("factorial=%d",f); } Output Enter the number:5 factorial=120 fact(5) fact(4) fact(3) fact(2) fact(1) fact(0) fact(5) { return 5*fact(4); } fact(4) { return 4*fact(3); } fact(3) { return 3*fact(2); } fact(2) { return 2*fact(1); } fact(1) { return 1*fact(0); } fact(0) { return 1; } 5*24=120 4*6=24 3*2=6 2*1=2 1*1=1 1 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 30
  • 31. Write a program to find sum of first n natural numbers using recursive function #include<stdio.h> int sum(int n) { if( n == 0) return 0; return n+sum(n-1); } void main() { int n,f; printf("Enter the limit:"); scanf("%d",&n); printf(“n Sum=%d",sum(n)); } Output Enter the limit:5 factorial=15 sum(5) sum(4) sum(3) sum(2) sum(1) sum(0) sum(5) { return 5+sum(4); } sum(4) { return 4+sum(3); } sum(3) { return 3+sum(2); } sum(2) { return 2+sum(1); } sum(1) { return 1+sum(0); } sum(0) { return 0; } 5+10=15 4+6=10 3+3=6 2+1=3 1+0=1 0 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 31
  • 32. Passing an array as Parameter  Like the value of simple variables, it is also possible to pass the values of an array to a function.  To pass a single dimensional array to a function, it is sufficient to list the name of the array without any subscripts and the size of the array as arguments 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 32
  • 33. Rules to pass an Array to Function  The function must be called by passing only the name of the array.  In function definition, formal parameter must be an array type; the size of the array does not need to be specified.  The function prototype must show that the argument is an array. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 33
  • 34. Write a function to sort an array. #include<stdio.h> int sort(int A[],int n) { int i,j,temp; for(i=0;i<n-1;i++) { for(j=0;j<n-i-1;j++) { if(A[j]>A[j+1]) { temp = A[j]; A[j] = A[j+1]; A[j+1]= temp; } } } void main() { int A[30],i,n; printf("Enter the limit:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the element:"); scanf("%d",&A[i]); } sort(A,n); printf("Sorted Arrayn"); for(i=0;i<n;i++) printf("%dt",A[i]); } Output Enter the limit:5 Enter the element:17 Enter the element:23 Enter the element:5 Enter the element:2 Enter the element:9 Sorted Array 2 5 9 17 23 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 34
  • 35. • Passing 2 D array as parameter • While passing 2D as parameter we need to mention the max size of element of each row that is column 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 35
  • 36. Write a program to pass a 2 D matrix as parameter and find its sum of all the elements #include<stdio.h> // function that takes a 2 D and its order int sum(int A[][30],int m,int n) { int i,j,sum =0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { sum = sum + A[i][j]; } } printf("Sum=%d",sum); } } void main() { int A[30][30]; int i,n,m,j; printf("Enter the order of matrix:"); scanf("%d%d",&m,&n); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("Enter the element:"); scanf("%d",&A[i][j]); } } sum(A,m,n); } Output Enter the order of matrix:2 2 Enter the element:1 Enter the element:2 Enter the element:3 Enter the element:4 Sum=10 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 36
  • 37. STRUCTURE • A structure is a user defined data type in C. • A structure creates a data type that can be used to group items of possibly different types into a single type. • ‘struct’ keyword is used to create a structure. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 37
  • 38. • Consider we want to create the structure of a person with following variable name, age and address. • Then such a structure can be created as struct Person { char name[30]; int age; char addr[50]; }p; 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 38
  • 39. The general format of a structure definition is as follows struct structure_name { data_type member1; data_type member2; --------------------- --------------------- }S; Here S is called structure variable.6/30/2020 NIMMY RAJU,AP,VKCET,TVM 39
  • 40. • A structure variable can either be declared with structure declaration or as a separate declaration like basic types. struct structure_name { data_type member1; data_type member2; --------------------- --------------------- }: void main() { struct structure_name S; } struct structure_name { data_type member1; data_type member2; --------------------- --------------------- }S; 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 40
  • 41. Declare a structure namely Student to store the details (roll number, name, mark_for_C) of two students and display the data. #include<stdio.h> struct Student { char name[30]; int rollnum; int mark; }; void main() { struct Student s1,s2; printf("Enter the Student name:"); scanf("%s",s1.name); printf("Enter the Student rollnum:"); scanf("%d",&s1.rollnum); printf("Enter the Student Mark for C:"); scanf("%d",&s1.mark); printf("Enter the Student name:"); scanf("%s",s2.name); printf("Enter the Student rollnum:"); scanf("%d",&s2.rollnum); printf("Enter the Student Mark for C:"); scanf("%d",&s2.mark); printf("NametRoll NumbertMark for Cn"); printf("%st%dt%dn",s1.name,s1.rollnum,s1.m ark); printf("%st%dt%dn",s2.name,s2.rollnum,s2.m ark); } Output Enter the Student name:Sangeeth Enter the Student rollnum: Enter the Student Mark for C:35 Enter the Student name:Pratheesh Enter the Student rollnum:24 Enter the Student Mark for C:40 Name Roll Number Mark for C Sangeeth 27 35 Pratheesh 24 40 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 41
  • 42. Array of Structures  DeclareastructurenamelyStudenttostorethedetails(roll number, name, mark_for_C) of a student. Then, write a program in C to find the average mark obtained by the students in a class for the subject Programming in C (using the field mark_for_C). Use array of structures to store the required data. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 42
  • 43. Declare a structure namely Student to store the details (roll number, name, mark_for_C) of two students and display the data. #include<stdio.h> struct Student { char name[30]; int rollnum; int mark; }; void main() { struct Student s1,s2; printf("Enter the Student name:"); scanf("%s",s1.name); printf("Enter the Student rollnum:"); scanf("%d",&s1.rollnum); printf("Enter the Student Mark for C:"); scanf("%d",&s1.mark); printf("Enter the Student name:"); scanf("%s",s2.name); printf("Enter the Student rollnum:"); scanf("%d",&s2.rollnum); printf("Enter the Student Mark for C:"); scanf("%d",&s2.mark); printf("NametRoll NumbertMark for Cn"); printf("%st%dt%dn",s1.name,s1.rollnum,s1.m ark); printf("%st%dt%dn",s2.name,s2.rollnum,s2.m ark); } Output Enter the Student name:Sangeeth Enter the Student rollnum: Enter the Student Mark for C:35 Enter the Student name:Pratheesh Enter the Student rollnum:24 Enter the Student Mark for C:40 Name Roll Number Mark for C Sangeeth 27 35 Pratheesh 24 40 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 43
  • 44. #include<stdio.h> struct Student { char name[30]; int rollno; int mark_for_C; }; void main() { struct Student s[30]; int i,n,sum=0; float avg; printf("Enter the no of Student:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the Student name:"); scanf("%s",s[i].name); printf("Enter the Student rollno:"); scanf("%d",&s[i].rollno); printf("Enter the Mark for C:"); scanf("%d",&s[i].mark_for_C); } printf("NametRoll NumbertMark for Cn"); for(i=0;i<n;i++) { printf("%st%dt%dn",s[i].name, s[i].rollnum,s[i].mark_for_C); sum = sum + s[i].mark_for_C; } avg = (float)sum /n; printf("Average Mark=%fn",avg); } 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 44
  • 45. Output Enter the no of Student:3 Enter the Student name:Sangeeth Enter the Student rollnum:27 Enter the Student Mark for C:35 Enter the Student name:Pratheesh Enter the Student rollnum:24 Enter the Student Mark for C:40 Enter the Student name:Poornima Enter the Student rollnum:26 Enter the Student Mark for C:45 Name Roll Number Mark for C Sangeeth 27 35 Pratheesh 24 40 Poornima 26 45 Average Mark=40.00 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 45
  • 46. UNION  A union is a user-defined type similar to struct in C programming.  We use the union keyword to define unions. Example of Employee Union creation and declartion union Employee { char name[30]; int age; double salary; }; union Employee e; 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 46
  • 47. Difference between structure and union Structure Union struct keyword is used to define a structure union keyword is used to define a union Members do not share memory in a structure. Members share the memory space in a union Any member can be retrieved at any time in a structure Only one member can be accessed at a time in a union. Several members of a structure can be initialized at once. Only the first member can be initialized. Sizeofthestructureisequalto thesumofsize of the each member. Size of the union is equal to the size of the largest member. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 47
  • 48. #include<stdio.h> struct Person { char pincode[6]; // Size = 6 bytes int age; // Size = 4 bytes double salary;// Size = 8 bytes }; union Employee { char pincode[6]; int age; double salary; }; void main() { struct Person p; union Employee e; printf("Size of Structure Person=%dn",sizeof(p)); printf("Size of Union Employee=%d",sizeof(e)); } Output Size of Structure Person=18 Size of Union Employee=86/30/2020 NIMMY RAJU,AP,VKCET,TVM 48
  • 49. Storage Classes, Scope and life time of variables 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 49
  • 50. • In C, not only do all the variables have a data type, they also have a storage class. • The following variable storage classes are most relevant to functions – Automatic Variables – External or Global Variables – Static Variables – Register Variables 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 50
  • 51. Automatic Variables • Automatic variables are declared inside a function in which they are to be utilized. • They are created when the function is called and destroyed automatically when the function is exited, hence the name automatic. • Automatic variables are therefore private or local to the function in which they are declared. • Because of this property, automatic variables are also referred to as local or internal variables. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 51
  • 52. Automatic Variables • A variable declared inside a function without storage class specification is by default an automatic variable. void main() { int num; } is same as void main() { auto int num; } 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 52
  • 53. • Example #include<stdio.h> void func1() { int max=10; printf("Max in func1()=%dn",max); } void func2() { int max=20; printf("Max in func2()=%dn",max); } void main() { int max=30; func1(); func2(); printf("Max in main()=%dn",max); } Output Max in func1()=10 Max in func2()=20 Max in main()=30 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 53
  • 54. External Variables • Variables that are both alive and active throughout the entire program are known as external variables. • They are called global variables. • Unlike local variables, global variables can be accessed by any function in the program. • External global variables are global variables visible to all file other than in which it is declared6/30/2020 NIMMY RAJU,AP,VKCET,TVM 54
  • 55. #include<stdio.h> int max; void func1() { printf("Max in func1()=%dn",max); } void func2() { printf("Max in func2()=%dn",max); } void main() { max=30; func1(); func2(); printf("Max in main()=%dn",max); } Max in func1()=30 Max in func1()=30 Max in main()=30 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 55
  • 56. Static Variables • 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 like static int x; • Static global variables are global variables visible only to the file in which it is declared. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 56
  • 57. Register Variables • We can tell the compiler that a variable should be kept in one of the machine’s registers instead of keeping in the memory. • Since a register access is fast than a memory access, keep in the frequently accessed variables in the register will lead to faster execution of programs. • This is done as follows register int count; 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 57
  • 58. • The scope of a variable is the part of the program within which the variable can be used. So, the scope describes the visibility of an identifier within the program. • The lifetime of a variable or function is the time duration for which memory is allocated to store it, and when that memory is released. 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 58
  • 59. StorageClass Wheredeclared Visibility Lifetime extern Beforeallfunctionsinafile Entire file plus other files where variable is declared. Global Static Beforeallthefunctioninafile Onlyinthatfile Global Noneorauto Inside afunction Only in that function orblock Until end of function Register Inside afunctionorblock Onlyinthatfunction orblock Until end of function 6/30/2020 NIMMY RAJU,AP,VKCET,TVM 59