0% found this document useful (0 votes)
51 views67 pages

PPL Unit 4

Uploaded by

ausera1991a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views67 pages

PPL Unit 4

Uploaded by

ausera1991a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

SUBPROGRAMS AND BLOCKS

Fundamentals of Subprograms, difference between procedures and functions.


Functions in C: Introduction to functions, Function definition, function declaration,
function call, return statement, passing parameters to functions, scope of variables,
recursive functions, concept of pointers, call by value, call by reference.
Fundamentals of Subprograms
 A Subprogram is a program inside any larger program that can be reused any
number of times.
 Characteristics of a Subprogram:
 A Subprogram is implemented using the Call & Return instructions in Assembly Language.
 The Call Instruction is present in the Main Program and the Return(Ret) Instruction is present in
the subprogram itself.
 It is important to note that the Main Program is suspended during the execution of any subprogram.
Moreover, after the completion of the subprogram, the main program executes from the next
sequential address present in the Program Counter.
 For the implementation of any subprogram, a “Stack” is used to store the “Return Address” to the
Main Program . Here, Return Address means the immediately next instruction address after the Call
Instruction in the Main program. This Return Address is present inside the Program Counter. Thus
during the execution of the Call Instruction, the Program Counter value is first pushed to the Stack
as the Return Address and then the Program Counter value is updated to the given address in the
Call Instruction. Similarly, during the execution of Return(Ret) Instruction, the value present in the
stack is popped and the Program Counter value is restored for further execution of the Main
Program.
 The Main advantage of Subprogram is that it avoids repetition of Code and allows us to reuse the
same code again and again.
Flow of control during function call
Function Definition
Prototype Declaration
Why use function ?
 Function are used for divide a large code into module, due
to this we can easily debug and maintain the code.
 For example if we write a calculator programs at that time we
can write every logic in a separate function (For addition
sum(), for subtraction sub()). Any function can be called
many times.
Function vs Procedure

 A function would return the returning value/control to


the code or calling function.
 The procedures perform certain tasks in a particular
order on the basis of the given inputs.
 A procedure, on the other hand, would return the
control, but would not return any value to the calling
function or the code.
Function Aspects
There are three aspects of a C function.
 Function Declaration
 Function Definition
 Function Call

Function Declaration
The function declaration tells the compiler about function name, the data type of the return value
and parameters.
The function declaration is also called a function prototype.
The function declaration is performed before the main function or inside the main function or
any other function.
Function declaration syntax -
returnType functionName(parametersList/argument list);
E.g void add();
In the above syntax, returnType specifies the data type of the value which is sent as a return value
from the function definition.
The functionName is a user-defined name used to identify the function uniquely in the program.
The parametersList is the data values that are sent to the function definition.
Function Aspects
Function Definition
The function definition provides the actual code of that function.
The function definition is also known as the body of the function. The actual task of the function is
implemented in the function definition.
That means the actual instructions to be performed by a function are written in function definition.
The actual instructions of a function are written inside the braces "{ }".
The function definition is performed before the main function or after the main function.

Function definition syntax –

returnType functionName(parametersList)
{

Actual code...

}
Function Aspects
Function Call
The function call tells the compiler when to execute the function definition.
When a function call is executed, the execution control jumps to the function definition
where the actual code gets executed and returns to the same functions call once the
execution completes.

The function call is performed inside the main function or any other function or
inside the function itself.

Function call syntax –

functionName(parameters);
Return Statement
 When program execution reaches the return statement, it
will leave the current function and the program execution
will resume to the next statement, where it was actually
called.
Syntax
Example
 return;
 return (value or variable);
 return 10;
 return a;
 return a+b;
Return Statement
 The value will be
passed back to the
function where it was
called. We can receive
the value and do the
further programming
from that point.
Types of Functions in C
In C Programming Language, based on providing the
function definition, functions are divided into two types.
Those are as follows...
 System Defined Functions
 User Defined Functions
System Defined Functions
 The C Programming Language provides pre-defined functions to make
programming easy.

 These pre-defined functions are known as system defined functions. The system
defined function is defined as follows...

 The function whose definition is defined by the system is called as system defined
function.

 The system defined functions are also called as Library Functions or Standard
Functions or Pre-Defined Functions. The implementation of system defined
functions is already defined by the system.

 In C, all the system defined functions are defined inside the header files like
stdio.h, conio.h, math.h, string.h etc., For example, the funtions printf() and
scanf() are defined in the header file called stdio.h.
System Defined Functions
 Whenever we use system defined functions in the program, we must
include the respective header file using #include statement.
 For example, if we use a system defined function sqrt() in the
program, we must include the header file called math.h because the
function sqrt() is defined in math.h.
 Points to be Remembered
 System defined functions are declared in header files
 System defined functions are implemented in .dll files. (DLL stands
for Dynamic Link Library).
 To use system defined functions the respective header file must be
included.
User Defined Functions
 In C programming language, users can also create their own functions.
 The functions that are created by users are called as user defined functions.
The user defined function is defined as follows...
 The function whose definition is defined by the user is called as user defined
function.
 That means the function that is implemented by user is called as user defined
function. For example, the function main is implemented by user so it is
called as user defined function.
 In C every user defined function must be declared and implemented.
Whenever we make function call the function definition gets executed.
 For example, consider the following program in which we create a function
called addition with two parameters and a return value.
User Defined Functions
 That means the function that is implemented by user is called as user defined
function. For example, the function main is implemented by user so it is called
as user defined function.

 In C every user defined function must be declared and implemented. Whenever


we make function call the function definition gets executed.

 For example, consider the program in which we create a function called


addition with two paramenters and a return value.
 E.g int addition(int,int)
Advantage of functions in C
There are the following advantages of C functions.
 By using functions, we can avoid rewriting same logic/code again and
again in a program.
 We can call C functions any number of times in a program and from
any place in a program.
 We can track a large C program easily when it is divided into multiple
functions.
 Reusability is the main achievement of C functions.
 Using functions larger programs can be divided into smaller module.
 Using functions the program implementation becomes easy.
Argument Vs Parameter
What is Argument?
The values that are declared within a function when the function is called are
known as an argument.
These values are considered as the root of the function that needs the
arguments while execution, and it is also known as Actual arguments or
Actual Parameters.
What is Parameter?
The variables that are defined when the function is declared are known as a
parameter.
These are also known as formal parameters or formal arguments.
Argument Vs Parameter
Sr. No. Argument Parameter
1 The values that are declared The variables that are defined
within a function when the when the function is declared are
function is called are known as known as parameters.
an argument.
2 These are used in function call These are used in the function
statements to send value from header of the called function to
the calling function to the receive the value from the
receiving function. arguments.
3 During the time of call each Parameters are local variables
argument is always assigned to which are assigned values of the
the parameter in the function arguments when the function is
definition. called.
4 They are also known as Actual They are also known as Formal
Parameters. Parameters.
Function Aspects/Methods of
calling Functions
Based on the data flow between the calling function and
called function, the functions are classified as follows...
 Function without Parameters and without Return
value
 Function with Parameters and without Return
value
 Function without Parameters and with Return
value
 Function with Parameters and with Return value
C program to add two numbers using a function
without return type and without parameters
#include<stdio.h>
void add( );
void main()
{
add();
}
void add( )
{
int a, b;
int sum = 0;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
Output:
printf("Sum of the two numbers is: %d", sum);
Enter two numbers: 10 20
}
Sum of the two numbers is: 30
Function without Parameters and with Return value

 In this type of functions there is no data transfer from calling-function to called-


function (parameters) but there is data transfer from called function to calling-
function (return value).
 The execution control jumps from calling-function to called function and executes
called function, and finally comes back to the calling function along with a return
value.
 For example, consider the following program...
C program to add two numbers using a function with
return type and without parameters
#include<stdio.h>
int add( );
void main( )
{
int sum;
sum =add() ;
printf("Sum of the two numbers is: %d", sum);
}
int add()
{
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum= a+b; Output:
return(sum); Enter two numbers: 10 20
} Sum of the two numbers is: 30
Function with Parameters and without Return value

 In this type of functions there is data transfer from calling-function to called


function (parameters) but there is no data transfer from called function to
calling-function (return value).
 The execution control jumps from calling-function to called function along with
the parameters and executes called function, and finally comes back to the
calling function.
 For example, consider the following program...
C program to add two numbers using a function without
return type with parameters
#include<stdio.h>
#include<stdio.h>
void add(int a, int b);
void add(int a, int b)
void main()
{
{
int sum = 0;
int a, b;
sum = a + b;
printf("Enter two numbers: ");
printf("Sum of the two numbers is: %d",
sum); scanf("%d %d", &a, &b);
} add(a, b);
void main() }
{ void add(int a, int b)
int a, b; {
printf("Enter two numbers: "); int sum = 0;
scanf("%d %d", &a, &b); sum = a + b;
add(a, b); printf("Sum of the two numbers is: %d",
Output:
Enter two numbers: 10 20
sum);
}
Sum of the two numbers is: 30 }
Function with Parameters and with Return value

 In this type of functions there is data transfer from calling-function to


called-function (parameters) and also from called function to calling-
function (return value).
 The execution control jumps from calling-function to called function
along with parameters and executes called function, and finally comes
back to the calling function along with a return value.
 For example, consider the following program...
C program to add two numbers using a function with
return type and with parameters
#include <stdio.h>
// Function declaration
int add(int, int);
int main() // User defined function to add two
//numbers
{
int add(int a, int b)
int num1, num2, res; {
printf("Enter two numbers: "); int sum;
scanf("%d %d", &num1, &num2); sum = a + b;
// Call the add() function return sum;
res = add(num1, num2); }
// Print the result
printf("Sum of the two numbers is: %d", res);
return 0; Output:
} Enter two numbers: 10 20
Sum of the two numbers is: 30
Some More Functions for
Practice
Demonstration of Function
#include<stdio.h>
#include<conio.h>
void message();//function declaration Output:
void main() Good morning
{
I am proud to be Indian
Clrscr();
printf("Good morning"); I live in India
message(); //function call& calling function
printf(“\n I live in India”);
}

void message() //Function definition & called function


{
printf("\n I am proud to be Indian");
}
Demonstration of Function
#include <stdio.h>
#include<conio.h>
void hi();// function declaration Output:
void main() Hello World
{ I am the learning function
clrscr(); This is my first program of function
printf("\nHello World");
hi(); //function call
getch();
}
void hi()//Function definition
{
printf("\nI am the learning function");
printf("\nThis is my first program of
function");
}
#include <stdio.h>
void fun(int);
main()
void fun(int x)
{
int x=5;
{
clrscr(); printf("\nThe value of x is= %d", x);
printf("The Demonstration program for }
passing parameter/argument to function");
fun(x);
getch();
}
Program to calculate the average of
five numbers
#include<stdio.h>
void average(int, int, int, int, int); //function
declaration
void main()
{
void average(int a, int b, int c, int d, int e) //
int a,b,c,d,e; function definition
printf("\nGoing to calculate the average of {
five numbers:");
float avg;
printf("\nEnter five numbers:");
avg = (a+b+c+d+e)/5;
scanf("%d %d %d %d %d",&a,&b,&c,&d,
printf("The average of given five numbers : %
&e); f",avg);
average(a,b,c,d,e); // function call }
}
What is Fibonacci series?
 Fibonacci series is a series of numbers where the current number is the sum of previous two terms.
For Example: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... , (n-1th + n-2th)
void fibb(int s)
#include<stdio.h> {
void fibb(int); int a,b,c,i,n;
void main() a=0;
{ b=1;
int n ,fib; printf("%d , %d,", a,b);
printf("Enter the count of numbers in series: "); for(i=0;i<s-2;i++)
scanf("%d", &n); {
fibb(n); c=a+b;
} printf(" %d ",c);
a=b;
b=c;
}
}
Output:
Enter the count of numbers in series: 7
0 , 1, 1 2 3 5 8
Find Cube of A no
#include <stdio.h> int cube()
int cube(); {
main()
int cube,n;
{
printf("\nEnter The no");
int result;
printf("Hello World"); scanf("%d",&n);
result=cube(); cube=n*n*n;
printf("\nThe cube of no is=%d",result); return cube;
} }
Program to calculate the
area of the square
#include<stdio.h> int square() // function definition
int square(); // function declaration {
void main() float side;
{
printf("Enter the length of the side in meters: ");
printf("Going to calculate the area of the
scanf("%f",&side);
square\n");
float area = square(); // function call return side * side;
}
printf("The area of the square: %f\n",area);
}
Area of Circle
#include<stdio.h>
int circle(); // function declaration int circle() // function definition
{
void main()
float r, res;
{ printf("Enter the radius of the circle: ");
float area; #include<stdio.h> scanf("%f", &r);
int circle(); // function declaration res=3.14*r*r;
void main() return res;
}
{
printf("Going to calculate the area of the circle\n");
float area; area = circle(); // function call
printf("Going to calculate the area of the printf("The area of the circle: %f\n", area);
circle\n"); }
area = circle(); // function call
printf("The area of the circle: %f\n",
area); Output:
} Going to calculate the area of the circle
Enter the radius of the circle: 5
The area of the circle: 78.000000
Square root of a function
#include <stdio.h>
#include<math.h>
float squrt(float);
void main()
{
float n,result;
printf("Enter a no: ");
scanf("%f", &n);
result=squrt(n);
printf("The square root of a no is=%f", result);
}
float squrt(float a)
{
float sol;
sol=sqrt(a); Output:
return sol; Enter a no: 16
} The square root of a no is=4.000000
Factorial of a number n
Factorial of a number n is product of all positive integers less than or equal to n. It is
denoted as n!. For example factorial of 5 = 1 * 2 * 3 * 4 * 5 = 120

#include <stdio.h> int fact(int a)


int fact(int); {
main() int i,fact=1;
{ int n,res; for(i=1;i<=a;i++)
printf("Enter a no: "); {
scanf("%d",&n); fact=fact*i;
res=fact(n); }
printf("The factorial of a given no return fact;
is=%d",res); }
}
Output:
Enter a no: 7
The factorial of a given no is=5040
To check whether given no is even or not
#include<stdio.h>
int even(int);
void main()
Output:
{
Going to check whether a number is even
int n,res;
Enter the number: 5
printf("\nGoing to check whether a number is even");
The number is not even
printf("\nEnter the number: ");
scanf("%d", &n);
res = even(n);
Going to check whether a number is even
if(res == 0)
Enter the number: 8
printf("\nThe number is not even");
The number is even
else
printf("\nThe number is even");
}
int even(int n)
{
if(n%2 == 0)
return 1;
else
return 0;
}
Program to check prime or not using Function
#include <stdio.h> int prime(int n)
#include <conio.h> {
void main() int i;
{ for(i=2;i<=n/2;i++)
int num, res=0; {
printf("\nENTER A NUMBER: "); if(n%i!=0)
scanf("%d",&num); continue;
res=prime(num); else
if(res==0) return 1;
printf("\n%d IS A PRIME NUMBER",num); }
else return 0;
printf("\n%d IS NOT A PRIME NUMBER",num); }
}

Output:
ENTER A NUMBER: 7
7 IS A PRIME NUMBER
ENTER A NUMBER: 6
6 IS NOT A PRIME NUMBER
Scope rules of Identifiers
 File Scope: Scope of a Identifier starts at the beginning of the file and ends
at the end of the file. It refers to only those Identifiers that are declared
outside of all functions. The Identifiers of File scope are visible all over the
file Identifiers having file scope are global
 Block Scope: Scope of a Identifier begins at opening of the block ‘{‘ and
ends at the end of the block ‘}’. Identifiers with block scope are local to
their block
 Function Prototype Scope: Identifiers declared in function prototype are
visible within the prototype
 Function scope: Function scope begins at the opening of the function and
ends with the closing of it. Function scope is applicable to labels only. A
label declared is used as a target to goto statement and both goto and label
statement must be in same function
Call by value and Call by reference in C

 There are two methods to pass the data into the function in C language,
i.e., call by value and call by reference.
 Before we discuss function call by value, lets understand the
terminologies that we will use while explaining this:
 Actual parameters: The parameters that appear in function calls.
Formal parameters: The parameters that appear in function
declarations.
Call by value

 In call by value method, the value of the actual parameters is copied into
the formal parameters.
 In other words, we can say that the value of the variable is used in the
function call in the call by value method.
 In call by value method, we can not modify the value of the actual
parameter by the formal parameter.
 In call by value, different memory is allocated for actual and formal
parameters since the value of the actual parameter is copied into the formal
parameter.
 The actual parameter is the argument which is used in the function call
whereas formal parameter is the argument which is used in the function
definition.
Example – call by value
#include<stdio.h>
void change(int num)
{
printf("Before adding value inside function
num=%d \n",num);
num=num+100;
printf("After adding value inside function
num=%d \n", num);
}
int main()
{
int x=100;
printf("Before function call x=%d \n", x);
change(x);//passing value in function Output:
printf("After function call x=%d \n", x); Before function call x=100
return 0; Before adding value inside function num=100
After adding value inside function num=200
} After function call x=100
Swapping numbers using Call
by value
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10; int b = 20;
// printing the value of a and b in main
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
// The value of actual parameters do not change by changing the formal parameters in call
// by value, a = 10, b = 20

return 0; }
Output:
void swap (int a, int b) Before swapping the values in main a = 10, b = 20
{ After swapping values in function a = 20, b = 10

int temp; After swapping values in main a = 10, b = 20

temp = a; a=b; b=temp;


// Formal parameters, a = 20, b = 10
printf("After swapping values in function a = %d, b = %d\n",a,b);
}
Call by Reference
• In call by reference, the address of the variable is passed into the function call as the
actual parameter.

• The value of the actual parameters can be modified by changing the formal
parameters since the address of the actual parameters is passed.

• In call by reference, the memory allocation is the same for both formal parameters
and actual parameters.

• All the operations in the function are performed on the value stored at the address of
the actual parameters, and the modified value gets stored at the same address.
Pointers
The pointer in C language is a variable which stores the address of another variable.
This variable can be of type int, char, array, function, or any other pointer.

void main()
{
int n = 10;
int* p = &n;
printf("\nThe value of P=%d",p); Output:
printf("\nThe value of N=%d",n); The value of P=-1757119676
} The value of N=10
Example of Call by Reference
#include<stdio.h>
void change(int *)
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(&x);//passing reference in function
printf("After function call x=%d \n", x);
return 0;
}
void change(int *num) {
printf("Before adding value inside function num=%d \n",*num);
*num=*num+100;
printf("After adding value inside function num=%d \n", *num);
} Output:
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200
Swapping numbers using Call by
Reference
#include <stdio.h> void swap (int *a, int *b)
void swap(int *, int *); //prototype of the function
{
int main()
{ int temp;
int a = 10; temp = *a;
int b = 20; *a=*b;
printf("Before swapping the values in main a = %d, b = *b=temp;
%d\n",a,b); printf("After swapping
// printing the value of a and b in main (10,20) values in function a = %d, b =
swap(&a,&b); %d\n",*a,*b);
printf("After swapping values in main a = %d, b = // Formal parameters, a = 20, b =
%d\n",a,b); 10
// The values of actual parameters do change in call by reference, a = 20, b = 10 }
}
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
Difference between Call by Value
and Call by Reference in C
No. Call by value Call by reference
A copy of the value is passed An address of value is passed into
1
into the function the function
Changes made inside the
Changes made inside the function
function are limited to the
validate outside of the function
function only. The values of
2 also. The values of the actual
the actual parameters do not
parameters do change by changing
change by changing the
the formal parameters.
formal parameters.
Actual and formal arguments Actual and formal arguments are
3 are created at the different created at the same memory
memory location location
INTRODUCTION TO POINTERS
Location of i in memory can be represented by following memory map

i location name
3 value at location
65524 location number

It can be see that computer has selected memory location 65524 as the place to
store the value 3
The location number 65524 is not a number to relied upon, because some other time
the computer may choose a different location for storing value 3.
The important point is, i’s address in memory is a number
Address number can be printed through following program
Hence it is printed using %u which is format specifier for unsigned integer
Pointer Notation
#include<stdio.h>
void main()
{
int n = 10;
Output:
int *p = &n;
Address of n = 2050626372
// ‘&’ is ‘Address of’ operator Address of n = 2050626372
// ‘*’ is ‘Value at’ operator The value at p = 10
int **q =&p; The value of P=2050626372
printf("\nAddress of n = %u",&n); The value at p = 10
The value of Q=2050626360
printf("\nAddress of n = %u",p );
The value at q = 10
printf("\nThe value at p = %d", The value at q = 2050626372
*(&n));
printf("\nThe value of P=%u", p);
printf("\nThe value at p = %d", *p);
printf("\nThe value of Q=%u", q);
printf("\nThe value at q = %d", **q);
printf("\nThe value at q = %d", p++);
❖ The other pointer operator available in C is ‘*’, called ‘value at address’
operator.

❖ It gives the value stored at a particular address.

❖ The ‘value at address’ operator is also called ‘indirection’ operator.


❖ Printing the value of *(&p) is same as printing the value of p.

❖ The expression &p gives address of variable p


❖ This address can be collected in a variable, by saying, q=&p;

❖q is not an ordinary variable like any other integer variable

❖ It is a variable which contains the address of other variable (p in this case )


POINTER NOTATION
int * alpha; char *ch;
float *q;
❖Here, alpha, ch and q are declared as pointer variables,i.e. variables capable
of holding addresses.
❖The declaration float *s does not mean that s is going to contain floating
point value. It means s is going to contain the address of a floating point
value.
❖Similarly , char *ch means that ch is going to contain the address of a char
value.
❖Pointer is a variable which contains the address of other variable
❖This pointer itself might be another pointer
❖Consider the following example
POINTER NOTATION
❖ Here, i is an ordinary int, j is a pointer to an int (often called an integer
pointer), whereas k is a pointer to a integer pointer .
❖ int i=3;
❖ int *j= &i;
❖ int **k=&j;

i j k
3 65524 65522

65524 65522 65520


Recursive Functions in C
 In C programming language, function calls can be made from the main() function, other
functions or from the same function itself. The recursive function is defined as
follows...
 A function called by itself is called recursive function.
 The recursive functions should be used very carefully because, when a function called
by itself it enters into the infinite loop. And when a function enters into the infinite
loop, the function execution never gets completed.
 We should define the condition to exit from the function call so that the recursive
function gets terminated.
 When a function is called by itself, the first call remains under execution till the last
call gets invoked.
 Every time when a function call is invoked, the function returns the execution control
to the previous function call.
Recursion
❖ A function is called as ‘recursive’ if a statement within the body of a
function calls the same function.
❖ Sometimes called ‘circular definition’ , recursion in thus the process of
defining something in terms of itself.
❖ Recursion is a process where function call itself repeatedly untill a
particular condition is satisfied. It is just like a loop.
❖ Therefore it is necessary to give halting or terminating condition for
recursive function. Generally, if condition is employed to terminate
recursive function.
Recursion
e.g.
main()
{
printf(“hello”); main();
}
Output is hello hello hello ………..infinite
times
Without Recursion / Iterative Method
/*Program for factorial of a number using function (Non
recursive)*/
void main()
{
int x,fact;
int factorial(int); clrscr();
printf("\n Enter any number"); scanf("%d",&x);
fact=factorial(x);
printf("Factorial of %d = %d",x,fact); getch();
}
Without Recursion / Iterative Method
factorial(int y)
{
int f=1,i;
for(i=y;i>=1;i--)
f=f*i;
return(f);
}
Output
Enter any number 5
factorial of 5 = 120
Factorial---Recursive Method
#include<stdio.h>
#include<conio.h> int factorial( int n )
{
int factorial( int ) ;
int temp ;
int main()
if( n == 0) //Termination condition
{
return 1 ;
int fact, n ;
else
printf("Enter any integer: ") ;
scanf("%d", &n) ; temp = n * factorial( n-1 ) ;
fact = factorial( n ) ; // recursive function call
printf("\nFactorial of %d is %d\n", n, fact) ; return temp ;
return 0; }
}
RECURSION
Output
Enter any integer: 5
Factorial of 5 is 120

f = 5*rec(4);
= 5*4*rec(3);
= 5*4*3*rec(2);
= 5*4*3*2*rec(1);
= 5*4*3*2*1
= 120
Practical Exercise 9
1.Write a program to find sum of two numbers using functions.
2. Write a program to find product of two numbers using functions without
arguments, without return type.
3. Write a program to find difference of two numbers using functions without
arguments, with return type.
4. Write a program to find sum of two numbers using functions with arguments
& without return type.
5. Write a program to find product of two numbers using functions with
arguments, with return type.
6. Write a program to swap two numbers using a) Call By Value B) Call By
Reference.

You might also like