Practical / Solution
Call by Value — Exercise 1
Problem
Write a C program to pass an integer to a function and increase its
value by 10. Display the value before, inside and after the function call.
Program
#include <stdio.h>
void increase(int n)
{
// Modify the local copy
n = n + 10;
printf("Inside function: %d\n", n);
}
int main()
{
int num = 20;
printf("Before function call: %d\n", num);
// Value of num is copied to n
increase(num);
printf("After function call: %d\n", num);
return 0;
}
Explanation
In call by value, a copy of the actual value is passed to the function.
Therefore, changes made to the parameter do not change the original variable.
Expected Output
Before function call: 20
Inside function: 30
After function call: 20
Call by Value — Exercise 2
Problem
Write a C program to pass two integers to a function and swap them.
Display the values before, inside and after the function call.
Program
#include <stdio.h>
void swap(int a, int b)
{
int temp;
// Swap the local copies
temp = a;
a = b;
b = temp;
printf("Inside function: a = %d, b = %d\n", a, b);
}
int main()
{
int x = 10, y = 20;
printf("Before function call: x = %d, y = %d\n", x, y);
// Pass values to the function
swap(x, y);
printf("After function call: x = %d, y = %d\n", x, y);
return 0;
}
Explanation
The function receives copies of x and y.
The copies are swapped, but the original variables remain unchanged.
Expected Output
Before function call: x = 10, y = 20
Inside function: a = 20, b = 10
After function call: x = 10, y = 20
Call by Value — Exercise 3
Problem
Write a C program to pass a number to a function and change its value
to zero. Show that the original value remains unchanged.
Program
#include <stdio.h>
void makeZero(int n)
{
// Change only the local copy
n = 0;
printf("Inside function: %d\n", n);
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Before function call: %d\n", num);
// Pass num by value
makeZero(num);
printf("After function call: %d\n", num);
return 0;
}
Explanation
The function changes only its local parameter. Since the original
variable and the parameter occupy separate storage locations, the
original value is not changed.
Expected Output
Enter a number: 50
Before function call: 50
Inside function: 0
After function call: 50
Call by Value — Exercise 4
Problem
Write a C program to pass two numbers to a function and increase both
values by 5. Demonstrate that the original variables are unchanged.
Program
#include <stdio.h>
void increase(int a, int b)
{
// Modify local copies
a = a + 5;
b = b + 5;
printf("Inside function: a = %d, b = %d\n", a, b);
}
int main()
{
int x = 10, y = 20;
printf("Before function call: x = %d, y = %d\n", x, y);
increase(x, y);
printf("After function call: x = %d, y = %d\n", x, y);
return 0;
}
Explanation
Both arguments are passed as values. The function receives separate
copies and modifications affect only those copies.
Expected Output
Before function call: x = 10, y = 20
Inside function: a = 15, b = 25
After function call: x = 10, y = 20
Call by Value — Exercise 5
Problem
Write a C program to calculate the square of a number using call by value.
Program
#include <stdio.h>
int square(int n)
{
// Calculate square using the received copy
return n * n;
}
int main()
{
int num, result;
printf("Enter a number: ");
scanf("%d", &num);
// Pass num by value
result = square(num);
printf("Square = %d\n", result);
return 0;
}
Explanation
The function receives a copy of the number and uses that copy to
perform the calculation. The original value does not need to change.
Expected Output
Enter a number: 12
Square = 144
Call by Value — Exercise 6
Problem
Write a C program to calculate the largest of three numbers by passing
the three values to a function.
Program
#include <stdio.h>
int largest(int a, int b, int c)
{
int max;
// Compare the received values
if (a > b)
max = a;
else
max = b;
if (c > max)
max = c;
return max;
}
int main()
{
int x, y, z, result;
printf("Enter three numbers: ");
scanf("%d %d %d", &x, &y, &z);
// Pass values by value
result = largest(x, y, z);
printf("Largest = %d\n", result);
return 0;
}
Explanation
The function receives copies of all three values. Since the function
only reads these copies, the original variables are unaffected.
Expected Output
Enter three numbers: 25 70 45
Largest = 70
Call by Value — Exercise 7
Problem
Write a C program to calculate simple interest by passing principal,
rate and time to a function using call by value.
Program
#include <stdio.h>
float simpleInterest(float p, float r, float t)
{
// Calculate simple interest
return (p * r * t) / 100;
}
int main()
{
float principal, rate, time, interest;
printf("Enter principal, rate and time: ");
scanf("%f %f %f", &principal, &rate, &time);
// Pass values to the function
interest = simpleInterest(principal, rate, time);
printf("Simple Interest = %.2f\n", interest);
return 0;
}
Explanation
The values are copied into the function parameters. The function
performs the calculation without modifying the original variables.
Expected Output
Enter principal, rate and time: 10000 5 2
Simple Interest = 1000.00
Call by Value — Exercise 8
Problem
Write a C program to demonstrate call by value using a function that
attempts to change the marks of a student.
Program
#include <stdio.h>
void modifyMarks(int marks)
{
// Modify only the local copy
marks = marks + 10;
printf("Inside function: marks = %d\n", marks);
}
int main()
{
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
printf("Before function call: marks = %d\n", marks);
// Pass marks by value
modifyMarks(marks);
printf("After function call: marks = %d\n", marks);
return 0;
}
Explanation
The function receives a copy of the student's marks. Adding 10 inside
the function changes only that copy, not the original variable.
Expected Output
Enter marks: 65
Before function call: marks = 65
Inside function: marks = 75
After function call: marks = 65
Call by Value — Exercise 9
Problem
Write a C program to calculate the area and perimeter of a rectangle
using functions with parameters passed by value.
Program
#include <stdio.h>
float area(float length, float width)
{
// Calculate area
return length * width;
}
float perimeter(float length, float width)
{
// Calculate perimeter
return 2 * (length + width);
}
int main()
{
float l, w;
printf("Enter length and width: ");
scanf("%f %f", &l, &w);
// Values are passed by value
printf("Area = %.2f\n", area(l, w));
printf("Perimeter = %.2f\n", perimeter(l, w));
return 0;
}
Explanation
The length and width values are copied into the parameters of both
functions. Neither function changes the original variables.
Expected Output
Enter length and width: 10 5
Area = 50.00
Perimeter = 30.00
Call by Value — Exercise 10
Problem
Write a C program to demonstrate call by value by passing two numbers
to a function that attempts to swap them. Clearly display the values
before and after the function call.
Program
#include <stdio.h>
void swap(int a, int b)
{
int temp;
// Swap only the copied values
temp = a;
a = b;
b = temp;
printf("Inside function after swap: a = %d, b = %d\n", a, b);
}
int main()
{
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("Before function call: x = %d, y = %d\n", x, y);
// Pass x and y by value
swap(x, y);
printf("After function call: x = %d, y = %d\n", x, y);
return 0;
}
Explanation
This is the key demonstration of call by value. The function can
modify its parameters, but the original variables remain unchanged
because only copies were passed.
Expected Output
Enter two numbers: 100 200
Before function call: x = 100, y = 200
Inside function after swap: a = 200, b = 100
After function call: x = 100, y = 200