C Programming • Arrays & Functions
C Programming / Call by Reference — Exercises

Call by Reference — Exercises

Practical 3 Arrays & Functions

Write a C program to change the value of an integer using a function and demonstrate modification of the original variable.

Practical / Solution

Call by Reference — Exercise 1

Problem

Write a C program to change the value of an integer using a function and demonstrate modification of the original variable.

Program

#include <stdio.h> void changeValue(int *n) { // Modify the original variable through its address *n = 100; } int main() { int num = 20; printf("Before function call: %d\n", num); // Pass the address of num changeValue(&num); printf("After function call: %d\n", num); return 0; }

Explanation

The function receives the address of num through a pointer. Using *n, the function accesses and modifies the original variable.

Expected Output

Before function call: 20

After function call: 100

Call by Reference — Exercise 2

Problem

Write a C program to swap two numbers using a function and pointers.

Program

#include <stdio.h> void swap(int *a, int *b) { int temp; // Swap the original values temp = *a; *a = *b; *b = temp; } int main() { int x, y; printf("Enter two numbers: "); scanf("%d %d", &x, &y); printf("Before swap: x = %d, y = %d\n", x, y); // Pass addresses of x and y swap(&x, &y); printf("After swap: x = %d, y = %d\n", x, y); return 0; }

Explanation

The addresses of x and y are passed to the function. Therefore, changes made through the pointers affect the original variables.

Expected Output

Enter two numbers: 10 20

Before swap: x = 10, y = 20

After swap: x = 20, y = 10

Call by Reference — Exercise 3

Problem

Write a C program to increase a number by 10 using a function and pointer.

Program

#include <stdio.h> void increase(int *n) { // Modify the original value *n = *n + 10; } int main() { int num; printf("Enter a number: "); scanf("%d", &num); // Pass address of num increase(&num); printf("Updated value = %d\n", num); return 0; }

Explanation

The pointer parameter stores the address of the original variable. Dereferencing it with *n changes the original value.

Expected Output

Enter a number: 25

Updated value = 35

Call by Reference — Exercise 4

Problem

Write a C program to calculate the quotient and remainder of two numbers using a function with pointer parameters.

Program

#include <stdio.h> void divide(int dividend, int divisor, int *quotient, int *remainder) { // Store both results through pointers *quotient = dividend / divisor; *remainder = dividend % divisor; } int main() { int a, b, quotient, remainder; printf("Enter dividend and divisor: "); scanf("%d %d", &a, &b); if (b != 0) { // Pass addresses for output values divide(a, b, &quotient, &remainder); printf("Quotient = %d\n", quotient); printf("Remainder = %d\n", remainder); } else { printf("Division by zero is not allowed.\n"); } return 0; }

Explanation

Pointer parameters allow a function to write results directly into variables supplied by the caller. Here, two results are returned through quotient and remainder.

Expected Output

Enter dividend and divisor: 17 5

Quotient = 3

Remainder = 2

Call by Reference — Exercise 5

Problem

Write a C program to find the largest and smallest of two numbers using pointer parameters.

Program

#include <stdio.h> void findValues(int a, int b, int *largest, int *smallest) { // Determine largest value if (a > b) { *largest = a; *smallest = b; } else { *largest = b; *smallest = a; } } int main() { int x, y, largest, smallest; printf("Enter two numbers: "); scanf("%d %d", &x, &y); // Pass addresses of result variables findValues(x, y, &largest, &smallest); printf("Largest = %d\n", largest); printf("Smallest = %d\n", smallest); return 0; }

Explanation

The input values are passed normally, while the addresses of the result variables are passed through pointers so the function can store both results directly.

Expected Output

Enter two numbers: 45 12

Largest = 45

Smallest = 12

Call by Reference — Exercise 6

Problem

Write a C program to calculate the total and average of three marks using pointer parameters to return both results.

Program

#include <stdio.h> void calculate(int m1, int m2, int m3, int *total, float *average) { // Calculate total *total = m1 + m2 + m3; // Calculate average *average = *total / 3.0; } int main() { int m1, m2, m3, total; float average; printf("Enter three marks: "); scanf("%d %d %d", &m1, &m2, &m3); // Pass addresses of output variables calculate(m1, m2, m3, &total, &average); printf("Total = %d\n", total); printf("Average = %.2f\n", average); return 0; }

Explanation

The function uses pointer parameters to return two values: total and average. This is useful when a function needs to provide multiple results.

Expected Output

Enter three marks: 70 80 90

Total = 240

Average = 80.00

Call by Reference — Exercise 7

Problem

Write a C program to increment a value using a function and display the changed value in main().

Program

#include <stdio.h> void increment(int *n) { // Increment the original variable (*n)++; } int main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("Before increment: %d\n", num); // Pass address of num increment(&num); printf("After increment: %d\n", num); return 0; }

Explanation

Because the function receives the address of num, it can modify the original variable using the dereference operator.

Expected Output

Enter a number: 50

Before increment: 50

After increment: 51

Call by Reference — Exercise 8

Problem

Write a C program to modify two numbers using a function so that the first number becomes twice its original value and the second number becomes three times its original value.

Program

#include <stdio.h> void modify(int *a, int *b) { // Modify the original values *a = *a * 2; *b = *b * 3; } int main() { int x, y; printf("Enter two numbers: "); scanf("%d %d", &x, &y); printf("Before modification: x = %d, y = %d\n", x, y); // Pass addresses of both variables modify(&x, &y); printf("After modification: x = %d, y = %d\n", x, y); return 0; }

Explanation

Both variables are modified through their addresses. Therefore, the changes are visible in main() after the function returns.

Expected Output

Enter two numbers: 10 20

Before modification: x = 10, y = 20

After modification: x = 20, y = 60

Call by Reference — Exercise 9

Problem

Write a C program to find the sum and product of two numbers using a function with pointer parameters for the results.

Program

#include <stdio.h> void calculate(int a, int b, int *sum, int *product) { // Store results using pointers *sum = a + b; *product = a * b; } int main() { int x, y, sum, product; printf("Enter two numbers: "); scanf("%d %d", &x, &y); // Pass addresses of result variables calculate(x, y, &sum, &product); printf("Sum = %d\n", sum); printf("Product = %d\n", product); return 0; }

Explanation

The function returns two results through pointer parameters. This demonstrates a common use of pointers when a function needs to modify or produce more than one output value.

Expected Output

Enter two numbers: 8 7

Sum = 15

Product = 56

Call by Reference — Exercise 10

Problem

Write a C program to sort three numbers in ascending order using a function and pointer parameters.

Program

#include <stdio.h> void sortThree(int *a, int *b, int *c) { int temp; // Compare first and second values if (*a > *b) { temp = *a; *a = *b; *b = temp; } // Compare second and third values if (*b > *c) { temp = *b; *b = *c; *c = temp; } // Check first and second again if (*a > *b) { temp = *a; *a = *b; *b = temp; } } int main() { int x, y, z; printf("Enter three numbers: "); scanf("%d %d %d", &x, &y, &z); // Pass addresses of the three variables sortThree(&x, &y, &z); printf("Ascending order: %d %d %d\n", x, y, z); return 0; }

Explanation

The function receives the addresses of all three variables and directly rearranges their original values. Therefore, the sorted values are available in main() after the function call.

Expected Output

Enter three numbers: 30 10 20

Ascending order: 10 20 30