C_Day 3
C_Day 3
6/24/2024
RECAP
6/24/2024
Topics
• Function
• Passing values to function
• Recursion
• Passing Array to Function
• Scope of variable
• Storage classes
6/24/2024
Function
6/24/2024
Function Introduction
• A large program in C can be divided to many
subprograms
• The subprogram is known as a function
• The job of function is to do something
• C program contains at least one function
6/24/2024
Advantages of Functions
• Divides the large problem to smaller and
simple task
• Easy to read and update
• Easy to debug
• Can be reused in programs
6/24/2024
Classification of Functions
• Based upon who develop the function
• Library Function
• User Defined Function
• Based upon the number of arguments and return
value
• Functions with arguments and with return value
• Functions without arguments and with return value
• Functions with arguments and without return value
• Functions without arguments and without return value
6/24/2024
Library Functions
• Pre Defined Functions
• Written in header files
• To use them, appropriate header file should be
included
Examples:
• printf()
• scanf()
• gets()
• sqrt()
6/24/2024
User Defined Functions
• Written by the programmer at the time of
writing program
• Three elements of user defined function
• Function Declaration
• Function Definition
• Function Call
6/24/2024
Function Declaration(Prototype)
• Provides basic information about function
• It must have
• Return Type
• Function Name
• Parameter List
Syntax:
Return-type Function-name(Parameter-list);
Example:
int sum(int,int);
6/24/2024
Function Definition
• It contains the code that will be executed
Syntax:
Return-type Function-name(Parameter-list)
{
Body of Function
}
Example:
int sum(int a, int b)
{
int s;
s=a+b;
return s;
}
6/24/2024
Function Call
• To use a function, we will have to call that
function
• When a program calls a function, program
control is transferred to the called function
Syntax:
Function-name(Parameter-list);
Example:
sum(x, y);
6/24/2024
Example
sum(int , int ); //FUNCTION DECLARATION
void main()
{
int p = 10, q = 14;
r = sum(int a, int b); // FUNCTION CALL
printf(“%d”, r);
}
6/24/2024
Classification of Functions based upon
number of arguments and return value
6/24/2024
Functions with arguments and with
return value
6/24/2024
Functions with arguments and with
return value
Example: int sum(int x, int y)
#include<stdio.h> {
#include<conio.h> int z;
int sum(int, int); Function Declaration
void main()
z=x+y;
{ return z;
int a, b, s; }
printf(“Eneter two Numbers”);
scanf(“%d%d”,&a,&b);
s=sum(a,b); Function Call
Function Definition
printf(“Addition=%d”,s);
getch();
}
6/24/2024
Functions with arguments and without
return value
Example: void sum(int x, int y)
#include<stdio.h> {
#include<conio.h> int z;
void sum(int, int); Function Declaration z=x+y;
void main() printf(“Addition=%d”,z);
{ }
int a, b;
printf(“Eneter two Numbers”);
scanf(“%d%d”,&a,&b);
sum(a,b);
getch(); Function Call Function Definition
}
6/24/2024
Functions without arguments and with
return value
Example: int sum( )
{
#include<stdio.h> int x,y,z;
#include<conio.h> printf(“Enter two Numbers”);
Function scanf(“%d%d”,&x,&y);
int sum( ); z=x+y;
Declaration
void main() return z;
}
{
int s;
s=sum( ); Function Call
Function Definition
printf(“Addition=%d”,s);
getch();
}
6/24/2024
Functions without arguments and
without return value
Example: void sum( )
{
#include<stdio.h> int x,y,z;
#include<conio.h> printf(“Enter two Numbers”);
Function scanf(“%d%d”,&x,&y);
void sum( ); z=x+y;
Declaration
void main() printf(“Addition=%d”,z);
}
{
sum( ); Function Call
getch(); Function Definition
}
6/24/2024
Passing Values to Function
6/24/2024
Types of Function Call
• Two types of Function Call
• Call by Value
• Call by Reference
6/24/2024
Call by Value
• Value of variables is passed.
• Copies the values of actual parameters into
formal parameters
• Changes in formal parameters do not reflect in
actual parameters
6/24/2024
Example of Call by Value
Example: void swap(int x,int y)
#include<stdio.h> {
#include<conio.h> int t;
void swap(int,int); t=x;
void main() x=y;
{ y=t;
int a,b; printf(“\nValue of formal
printf(“Enter two Number:\n”); parameters=%d,%d”,x,y);
scanf(“%d%d”,&a,&b); }
swap(a,b); Output:
printf(“\nValue of actual Enter two Number:
parameters=%d,%d”,a,b); 7
getch(); 10
} Value of formal parameters=10,7
Value of actual parameters=7,10
6/24/2024
Call by Reference
We will discuss this after Pointers
6/25/2024
Example of Call by Reference
Example: void swap(int *x,int *y)
#include<stdio.h> {
#include<conio.h> int t;
void swap(int *,int *); t=*x;
void main() *x=*y;
{ *y=t;
int a,b; printf(“Value of formal
printf(“Enter two Number”); parameters=%d,%d”,*x,*y);
scanf(“%d%d”,&a,&b); }
swap(&a,&b); Output:
printf(“Value of actual Enter two Number:
parameters=%d,%d”,a,b); 7
getch(); 10
} Value of formal parameters=10,7
Value of actual parameters=10,7
6/24/2024
Program to find the factorial of a
number using function
#include<stdio.h> int fact(int x)
#include<conio.h> {
int fact(int); int i, fa=1;
void main() for(i=1;i<=x; i++)
{ {
int n,f; fa=fa*i;
printf(“Enter a Number:\n”); }
scanf(“%d”,&n); return fa;
f=fact(n); }
printf(“Factorial of %d=%d”,n,f);
getch(); Output:
} Enter a Number:
5
Factorial of 5=120
6/24/2024
Recursion
6/24/2024
Recursion
• Process in which a function calls itself
• It must have a base condition
Example:
void main()
{
printf(“This is Recursion”);
main(); Recursive Function Call
}
6/24/2024
Program to calculate the factorial of a
number using recursion
#include<stdio.h> int fact(int x)
#include<conio.h> {
int fact(int); if(x==0)
void main() return (1);
{ else
int n,f; return (x*fact(x-1));
printf(“Enter a Number”); }
scanf(“%d”,&n);
f=fact(n);
printf(“Factorial=%d”,f); Output:
getch(); Enter a Number:
} 5
Factorial of 5=120
6/24/2024
Program to display the Fibonacci series using
recursion
#include<stdio.h> int rec(int x)
#include<conio.h> {
int rec(int); if(x==0)
void main() return (0);
{ else if(x==1)
int i,n; return (1);
printf(“Enter the Limit:\n”); else
scanf(“%d”,&n); return (rec(x-1)+rec(x-2));
for(i=0;i<n;i++) }
{
printf(“%d\t”,rec(i));
} Output:
getch(); Enter the Limit:
} 6
011235
6/24/2024
Program to solve Tower of Hanoi Problem using
recursion
The Tower of Hanoi is a classic problem that can be solved
recursively.
The goal is to move all disks from the source rod to the
destination rod, using an auxiliary rod, while following these
rules:
1. Only one disk can be moved at
a time.
2. A disk can only be placed on
top of a larger disk or an empty
rod.
6/25/2024
Program to solve Tower of Hanoi Problem using
recursion
6/25/2024
Program to solve Tower of Hanoi Problem using
recursion
6/25/2024
QUIZ
6/24/2024
Quiz
• A function in C cannot be invoked by another
function.
a. True
b. False
Ans: False
• A function in C language cannot call itself.
a. True
b. False
Ans: False
• A function is called _________when
recursive it calls
itself.
6/24/2024
Quiz
• A block is enclosed with a pair of:
(a) {} (b) ()
(c) [] (d) < >
Ans: a
• The parameters used in function call are
called ______________.
actual parameters
• The _______function
scanf( ) reads data from the
keyboard.
6/24/2024
Programs for Hands-on
Programs
• Write a function that returns 1 if number
passed as argument is prime, else returns 0.
• Write a recursive procedure to find the sum of
'n' positive integers, that is,
n + (n-1) + (n-2) + ...+ 3 + 2 + 1.
• Write a function to find the sum of first 'n'
integers; where 'n' is the argument to the
function.
6/24/2024
Programs
6/24/2024
Scope of Variable
Scope refers to the visibility or accessibility of a variable within
different parts of a program.
In C, the scope of a variable determines where it can be used in
the code.
6/25/2024
Local Variables
• Declared inside a function or block.
• Only accessible within that function or block.
• Memory is allocated when the block is entered and
deallocated when the block is exited.
6/25/2024
Global Variables
• Declared outside of all functions.
• Accessible from any function within the program.
• Memory is allocated when the program starts and deallocated
when the program ends.
6/25/2024
Nesting of Scope
• Nesting of Scope occurs when a block or function is placed
inside another block or function, creating multiple levels of
variable visibility.
• Inner blocks can access variables declared in outer blocks, but
outer blocks cannot access variables declared in inner blocks
6/25/2024
Storage Classes
6/25/2024
Storage Classes
1. auto
2. register
3. static
4. extern
6/25/2024
auto
• By default a variable is auto
Scope: Local to the block in which the variable is defined.
Lifetime: Duration of the block in which the variable is defined.
Keyword: auto
int auto x = 5
Or
auto int x =5
6/25/2024
register
Scope: Local to the block in which the variable is defined.
Lifetime: Duration of the block in which the variable is defined.
Keyword: register
register int x = 5
Or
int register x =5
6/25/2024
static
Scope: For local variables: Local to the block in which the
variable is defined.
For global variables: Local to the file in which the variable is
defined.
Lifetime: Duration of the entire program (for both local and
global variables).
Keyword: static
static int x = 5
Or
int static x =5
6/25/2024
extern
Scope: Global across all files (provides external linkage).
Lifetime: Duration of the entire program.
Keyword: extern
static int x = 5
Or
int static x =5
6/25/2024
Example
6/25/2024