C Programming • Arrays & Functions
C Programming / Parameter Passing in Functions — Exercises

Parameter Passing in Functions — Exercises

Practical 3 Arrays & Functions

Write a C program to pass two integers to a function and display their sum.

Practical / Solution

Parameter Passing — Exercise 1

Problem

Write a C program to pass two integers to a function and display their sum.

Program

#include <stdio.h> void add(int a, int b) { // Display the sum of received values printf("Sum = %d\n", a + b); } int main() { int x, y; printf("Enter two numbers: "); scanf("%d %d", &x, &y); // Pass values to the function add(x, y); return 0; }

Explanation

The values of x and y are passed to the function parameters a and b. The function works with these received values.

Expected Output

Enter two numbers: 10 20

Sum = 30

Parameter Passing — Exercise 2

Problem

Write a C program to pass a number to a function and display its square.

Program

#include <stdio.h> void square(int n) { // Calculate square of received value printf("Square = %d\n", n * n); } int main() { int num; printf("Enter a number: "); scanf("%d", &num); // Pass num to the function square(num); return 0; }

Explanation

The value stored in num is passed to the function. The function parameter n receives that value.

Expected Output

Enter a number: 9

Square = 81

Parameter Passing — Exercise 3

Problem

Write a C program to pass the length and width of a rectangle to a function and calculate its area.

Program

#include <stdio.h> float area(float length, float width) { // Return calculated area return length * width; } int main() { float l, w, result; printf("Enter length and width: "); scanf("%f %f", &l, &w); // Pass both values to the function result = area(l, w); printf("Area = %.2f\n", result); return 0; }

Explanation

Two values are passed to the function at the same time. The function uses both parameters to calculate and return the area.

Expected Output

Enter length and width: 12 5

Area = 60.00

Parameter Passing — Exercise 4

Problem

Write a C program to pass three marks to a function and calculate their average.

Program

#include <stdio.h> float average(int a, int b, int c) { // Calculate average of three values return (a + b + c) / 3.0; } int main() { int m1, m2, m3; float result; printf("Enter marks of three subjects: "); scanf("%d %d %d", &m1, &m2, &m3); // Pass all three marks to the function result = average(m1, m2, m3); printf("Average = %.2f\n", result); return 0; }

Explanation

The three actual values are passed to the three function parameters. The function then performs the required calculation.

Expected Output

Enter marks of three subjects: 70 80 90

Average = 80.00

Parameter Passing — Exercise 5

Problem

Write a C program to pass two values to a function and demonstrate that changes made to the function parameters do not change the original values.

Program

#include <stdio.h> void changeValues(int a, int b) { // Modify local copies a = a + 10; b = b + 10; printf("Inside function: 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 values to the function changeValues(x, y); printf("After function call: x = %d, y = %d\n", x, y); return 0; }

Explanation

In normal value parameter passing, the function receives copies of the original values. Changes to the parameters therefore do not modify the original variables in main().

Expected Output

Enter two numbers: 10 20

Before function call: x = 10, y = 20

Inside function: a = 20, b = 30

After function call: x = 10, y = 20

Parameter Passing — Exercise 6

Problem

Write a C program to pass a number to a function and determine whether the number is even or odd.

Program

#include <stdio.h> void checkEvenOdd(int num) { // Check divisibility by 2 if (num % 2 == 0) printf("The number is even.\n"); else printf("The number is odd.\n"); } int main() { int number; printf("Enter a number: "); scanf("%d", &number); // Pass number to the function checkEvenOdd(number); return 0; }

Explanation

The value is passed to the function, which performs the decision-making operation using its parameter.

Expected Output

Enter a number: 27

The number is odd.

Parameter Passing — Exercise 7

Problem

Write a C program to pass principal, rate and time to a function and calculate simple interest.

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 three values to the function interest = simpleInterest(principal, rate, time); printf("Simple Interest = %.2f\n", interest); return 0; }

Explanation

Three values are passed as arguments to the function. The function calculates simple interest using all three parameters.

Expected Output

Enter principal, rate and time: 10000 5 2

Simple Interest = 1000.00

Parameter Passing — Exercise 8

Problem

Write a C program to pass two numbers to a function and return their greatest common divisor (GCD).

Program

#include <stdio.h> int gcd(int a, int b) { int remainder; // Apply Euclidean algorithm while (b != 0) { remainder = a % b; a = b; b = remainder; } return a; } int main() { int x, y, result; printf("Enter two numbers: "); scanf("%d %d", &x, &y); // Pass both values to the function result = gcd(x, y); printf("GCD = %d\n", result); return 0; }

Explanation

The two input values are passed to the gcd() function. The function processes the parameters and returns the GCD.

Expected Output

Enter two numbers: 48 18

GCD = 6

Parameter Passing — Exercise 9

Problem

Write a C program to pass an array and its size to a function and calculate the sum of all array elements.

Program

#include <stdio.h> int arraySum(int arr[], int n) { int i, sum = 0; // Add all array elements for (i = 0; i < n; i++) { sum = sum + arr[i]; } return sum; } int main() { int arr[100], n, i, result; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Pass array and its size result = arraySum(arr, n); printf("Sum = %d\n", result); return 0; }

Explanation

An array and its size are passed to the function. The function can access the array elements using the array parameter.

Expected Output

Enter number of elements: 5

Enter 5 elements: 10 20 30 15 25

Sum = 100

Parameter Passing — Exercise 10

Problem

Write a C program that passes two numbers to separate functions to calculate their sum, product and difference.

Program

#include <stdio.h> int add(int a, int b) { return a + b; } int multiply(int a, int b) { return a * b; } int subtract(int a, int b) { return a - b; } int main() { int x, y; printf("Enter two numbers: "); scanf("%d %d", &x, &y); // Pass the same values to different functions printf("Sum = %d\n", add(x, y)); printf("Product = %d\n", multiply(x, y)); printf("Difference = %d\n", subtract(x, y)); return 0; }

Explanation

The same actual arguments can be passed to multiple functions. Each function receives its own parameters and performs a different operation.

Expected Output

Enter two numbers: 20 5

Sum = 25

Product = 100

Difference = 15