0% found this document useful (0 votes)
0 views

Day 8 Call by Value Reference and Recursive Functions

The document explains the concepts of call by value and call by reference in programming, highlighting how they affect function parameters. It provides examples of both methods, demonstrating that call by value does not alter the original argument while call by reference does. Additionally, it introduces recursive functions, detailing their structure and providing an example of a factorial function that utilizes recursion.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Day 8 Call by Value Reference and Recursive Functions

The document explains the concepts of call by value and call by reference in programming, highlighting how they affect function parameters. It provides examples of both methods, demonstrating that call by value does not alter the original argument while call by reference does. Additionally, it introduces recursive functions, detailing their structure and providing an example of a factorial function that utilizes recursion.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Day 8 : Call by value & reference

and recursive functions

CALL BY VALUE AND CALL BY REFERENCE:


Call by value and call by reference are two ways of passing arguments to functions in
programming languages.

CALL BY VALUE:
In call by value, a copy of the argument's value is passed to the function. Any
modifications made to the parameter inside the function do not affect the original
argument outside the function.
Syntax:

void functionName(data_type parameter_name) {

// Function body

PROGRAM:

#include <iostream>

void square(int& num) {


num *= num;
std::cout << "Inside the function: " << num << std::endl;
}

int main() {
int number = 5;
square(number);
std::cout << "Outside the function: " << number << std::endl

Day 8 : Call by value & reference and recursive functions 1


return 0;
}

OUTPUT:
Inside the function: 25
Outside the function: 5

EXPLANATION:
In this example, we have a function called square, which takes an integer parameter
num. Inside the function, we calculate the square of num and print the result.
However, since the parameter is passed by value, any changes made to num inside
the function do not affect the original value of number in the main function.

CALL BY REFERENCE:
In call by reference, the memory address of the argument is passed to the function,
allowing the function to directly modify the original argument.
Syntax:

void functionName(data_type &parameter_name) {

// Function body

PROGRAM:

#include <iostream>

void square(int& num) {


num = num * num;
std::cout << "Inside the function: " << num << std::endl;
}

int main() {
int number = 5;

Day 8 : Call by value & reference and recursive functions 2


square(number);
std::cout << "Outside the function: " << number << std::endl
return 0;
}

OUTPUT:

Inside the function: 25


Outside the function: 25

EXPLANATION:

In this example, the square function takes an integer reference parameter num
(denoted by int &num). This means that any changes made to num inside the
function will directly affect the original value of number in the main function.

RECURSIVE FUNCTIONS:

A recursive function is a function that calls itself within its definition. In other words,
a function invokes its own instance to solve a smaller subproblem of the original
problem.
Syntax:

return_type function_name(parameters) {

// Base case: The condition that stops the recursion.

// If the base case is true, the function returns a value without calling itself.

if (base_case_condition) {

return base_case_value;

} else {

// Recursive call(s): The function calls itself with smaller subproblems.

return recursive_function_call(small_subproblems);

PROGRAM:

Day 8 : Call by value & reference and recursive functions 3


#include <iostream>

// Recursive function to calculate the factorial of a number


int factorial(int n) {
// Base case: The factorial of 0 is 1.
if (n == 0) {
return 1;
} else {
// Recursive call: Calculate the factorial of (n-1) and
return n * factorial(n - 1);
}
}

int main() {
int num = 5;
int result = factorial(num);
std::cout << "Factorial of " << num << " is: " << result <<
return 0;
}

OUTPUT:

Factorial of 5 is: 120.

EXPLANATION:

In this program, we define a recursive function called factorial that calculates the
factorial of a given number n. The factorial of a non-negative integer n, denoted by n!,
is the product of all positive integers less than or equal to n. The factorial of 0 is
defined to be 1.

The base case of the recursive function is when n is equal to 0. In this case, the
function returns 1 because 0! is 1.

For n greater than 0, the recursive function calculates the factorial of (n-1) and
multiplies it by n. The function keeps calling itself with smaller subproblems
(decreasing n by 1) until it reaches the base case.

Day 8 : Call by value & reference and recursive functions 4


Let's say we want to calculate factorial(5):

1. The function factorial(5) calls factorial(4).


2. factorial(4) calls factorial(3).

3. factorial(3) calls factorial(2).

4. factorial(2) calls factorial(1).

5. factorial(1) calls factorial(0).


At this point, factorial(0) returns 1 (base case). The recursion starts to unwind:

6. factorial(1) multiplies 1 (returned from factorial(0)) by 1 and returns 1.

7. factorial(2) multiplies 1 (returned from factorial(1)) by 2 and returns 2.

8. factorial(3) multiplies 2 (returned from factorial(2)) by 3 and returns 6.


9. factorial(4) multiplies 6 (returned from factorial(3)) by 4 and returns 24.

10. factorial(5) multiplies 24 (returned from factorial(4)) by 5 and returns 120.

Practice Time

1. Recursion Basics:
Write a recursive function to calculate the sum of all numbers from 1 to a given
positive integer n.

2. Function Parameters:
Define a function that takes two integers as parameters and returns their product.
Write a simple program to demonstrate the use of this function.

3. Conditional Statements:
Create a program that checks whether a given number is even or odd and prints the
result.

4. Loops and Factorials:


Write a program to calculate the factorial of a number using a loop (not recursion)
and print the result.

Day 8 : Call by value & reference and recursive functions 5

You might also like