PPS UNIT 4 - DETAILED 7-MARK ANSWERS
Q1. Differentiate between Call by Value and Call by Reference
Call by Value:
- In this method, the actual value of the variable is passed to the function.
- Changes made inside the function do not affect the original value.
- A copy of the variable is used in the function.
- Memory-wise, it uses more memory due to copying.
- It is safer as the original data cannot be altered.
Call by Reference:
- In this method, the address of the variable is passed to the function.
- Changes made inside the function affect the original value.
- The function accesses the original data through pointers.
- It uses less memory and allows manipulation of actual data.
- Less safe, but more flexible.
Example in C:
#include <stdio.h>
void callByValue(int x) { x = x + 10; }
void callByReference(int *x) { *x = *x + 10; }
int main() {
int a = 5;
callByValue(a);
printf("Call by Value: %d\n", a); // Output: 5
callByReference(&a);
printf("Call by Reference: %d\n", a); // Output: 15
return 0;
Q2. What is Sorting? Write a C program for Bubble Sort
Sorting is the process of arranging data in a specific order (ascending or descending). It helps in
searching and organizing data efficiently.
Bubble Sort works by repeatedly swapping adjacent elements if they are in the wrong order.
Example in C:
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for(int i=0; i<n-1; i++) {
for(int j=0; j<n-i-1; j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
int main() {
int arr[] = {5, 2, 9, 1, 3};
int n = 5;
bubbleSort(arr, n);
printf("Sorted array: ");
for(int i=0; i<n; i++) printf("%d ", arr[i]);
return 0;
Q3. Explain Recursion. Write a Program to Find Factorial using Recursion
Recursion is a method where the function calls itself to solve a smaller subproblem. It must have a
base case to stop recursion.
Factorial(n) = n * Factorial(n - 1)
Base case: Factorial(0) = 1
Example in C:
#include <stdio.h>
int factorial(int n) {
if(n == 0 || n == 1) return 1;
else return n * factorial(n - 1);
int main() {
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
Q4. Fibonacci Series using Recursion
Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, ...
Each term is the sum of the previous two.
Example in C:
#include <stdio.h>
int fibonacci(int n) {
if(n == 0) return 0;
else if(n == 1) return 1;
else return fibonacci(n-1) + fibonacci(n-2);
int main() {
int n = 10;
printf("Fibonacci series: ");
for(int i=0; i<n; i++) {
printf("%d ", fibonacci(i));
return 0;
Q5. Difference between Linear Search and Binary Search
Linear Search:
- Scans elements one by one.
- Works on unsorted arrays.
- Time complexity: O(n)
- Simple but slow.
Binary Search:
- Divides array and searches half.
- Requires sorted array.
- Time complexity: O(log n)
- Efficient and faster.
Linear Search Code:
int linearSearch(int arr[], int n, int key) {
for(int i=0; i<n; i++) {
if(arr[i] == key) return i;
return -1;
Binary Search Code:
int binarySearch(int arr[], int low, int high, int key) {
while(low <= high) {
int mid = (low + high) / 2;
if(arr[mid] == key) return mid;
else if(arr[mid] < key) low = mid + 1;
else high = mid - 1;
return -1;
Q6. What is a Function? Explain with Advantages and Syntax
A function is a block of code that performs a specific task and can be reused.
Advantages:
- Reduces code duplication.
- Makes code modular and readable.
- Easy to test and debug.
Types:
1. Function Definition:
int add(int a, int b) {
return a + b;
2. Function Declaration (Prototype):
int add(int, int);
3. Function Call:
int result = add(5, 10);