Day 8 Call by Value Reference and Recursive Functions
Day 8 Call by Value Reference and Recursive Functions
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:
// Function body
PROGRAM:
#include <iostream>
int main() {
int number = 5;
square(number);
std::cout << "Outside the function: " << number << std::endl
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:
// Function body
PROGRAM:
#include <iostream>
int main() {
int number = 5;
OUTPUT:
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) {
// If the base case is true, the function returns a value without calling itself.
if (base_case_condition) {
return base_case_value;
} else {
return recursive_function_call(small_subproblems);
PROGRAM:
int main() {
int num = 5;
int result = factorial(num);
std::cout << "Factorial of " << num << " is: " << result <<
return 0;
}
OUTPUT:
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.
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.