Introduction
Function arguments allow you to send data from one part of a C program to a function. In C, ordinary function arguments are passed using call by value, which means the function receives a copy of the original value. Changes made to the parameter inside the function do not change the original variable. In this chapter, you will practice function arguments and call by value through 10 examples covering calculations, comparisons, swapping, conditions, and practical programming situations. Function Arguments and Call by Value in C practice questions with solutions to help you understand the concepts.
Q1. Pass One Number to a Function
Problem Statement
Write a C program that passes a number to a function and prints the number inside the function.
C Program
#include <stdio.h>
void displayNumber(int number)
{
printf("Number = %d", number);
}
int main()
{
int n = 25;
displayNumber(n);
return 0;
}
Sample Output
Number = 25
Explanation
The variable:
int n = 25;
is passed to:
displayNumber(n);
The function receives the value in its parameter:
void displayNumber(int number)
So:
n = 25
number = 25
The function then prints the value.
Concepts Covered
- Function argument
- Function parameter
- Function call
intparameter
Q2. Pass Two Numbers to a Function
Problem Statement
Write a C program that passes two numbers to a function and prints their sum.
C Program
#include <stdio.h>
void add(int a, int b)
{
int sum;
sum = a + b;
printf("Sum = %d", sum);
}
int main()
{
int x = 15;
int y = 25;
add(x, y);
return 0;
}
Sample Output
Sum = 40
Explanation
The function has two parameters:
void add(int a, int b)
The call is:
add(x, y);
At the time of the call:
x → 15 → a
y → 25 → b
The function calculates:
15 + 25 = 40
Concepts Covered
- Multiple arguments
- Multiple parameters
- Function call
- Addition
Q3. Find the Largest of Two Numbers
Problem Statement
Write a C program that passes two numbers to a function and finds the larger number.
C Program
#include <stdio.h>
void findLargest(int a, int b)
{
if (a > b)
{
printf("%d is larger.", a);
}
else
{
printf("%d is larger.", b);
}
}
int main()
{
int x = 45;
int y = 30;
findLargest(x, y);
return 0;
}
Sample Output
45 is larger.
Explanation
The values of x and y are passed to the function.
findLargest(x, y);
The function compares:
if (a > b)
Since:
45 > 30
the program prints 45.
Concepts Covered
- Function arguments
- Parameters
if-else- Relational operator
Q4. Demonstrate Call by Value
Problem Statement
Write a C program to demonstrate that changing a function parameter does not change the original variable.
C Program
#include <stdio.h>
void changeValue(int number)
{
number = 100;
printf("Value inside function = %d\n", number);
}
int main()
{
int number = 50;
printf("Before function call = %d\n", number);
changeValue(number);
printf("After function call = %d", number);
return 0;
}
Sample Output
Before function call = 50
Value inside function = 100
After function call = 50
Explanation
Initially:
number = 50
We call:
changeValue(number);
The function receives a copy of 50.
Inside the function:
number = 100;
changes the function’s local parameter.
It does not change the original variable in main().
Therefore:
Inside function → 100
Original variable → 50
This is the basic idea of call by value in C.
Concepts Covered
- Call by value
- Function parameter
- Local copy
- Original variable
Q5. Try to Change Two Variables Using Call by Value
Problem Statement
Write a C program that attempts to change two variables inside a function and observe whether the original variables change.
C Program
#include <stdio.h>
void changeValues(int a, int b)
{
a = 100;
b = 200;
printf("Inside function:\n");
printf("a = %d\n", a);
printf("b = %d\n", b);
}
int main()
{
int x = 10;
int y = 20;
printf("Before function call:\n");
printf("x = %d\n", x);
printf("y = %d\n", y);
changeValues(x, y);
printf("\nAfter function call:\n");
printf("x = %d\n", x);
printf("y = %d\n", y);
return 0;
}
Sample Output
Before function call:
x = 10
y = 20
Inside function:
a = 100
b = 200
After function call:
x = 10
y = 20
Explanation
The function receives copies:
x → copy → a
y → copy → b
Inside the function:
a = 100;
b = 200;
only changes the copies.
The original values remain:
x = 10
y = 20
This is why ordinary C function arguments use call by value.
Concepts Covered
- Multiple arguments
- Call by value
- Original variables
- Local parameters
Q6. Swap Two Variables Using Call by Value
Problem Statement
Write a C program that tries to swap two numbers inside a function using ordinary function arguments.
Observe whether the original values are swapped.
C Program
#include <stdio.h>
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf("Inside function:\n");
printf("a = %d\n", a);
printf("b = %d\n", b);
}
int main()
{
int x = 10;
int y = 20;
printf("Before function call:\n");
printf("x = %d\n", x);
printf("y = %d\n", y);
swap(x, y);
printf("\nAfter function call:\n");
printf("x = %d\n", x);
printf("y = %d\n", y);
return 0;
}
Sample Output
Before function call:
x = 10
y = 20
Inside function:
a = 20
b = 10
After function call:
x = 10
y = 20
Explanation
Inside the function, the values are successfully swapped:
a = 20
b = 10
But a and b are copies of x and y.
Therefore, the original variables remain:
x = 10
y = 20
This is an important example of call by value.
To actually modify the original variables, pointers are used. That topic will be covered later.
Concepts Covered
- Call by value
- Swapping
- Temporary variable
- Function arguments
Q7. Calculate Area Using Function Arguments
Problem Statement
Write a C program that passes the length and width of a rectangle to a function and calculates its area.
Formula:
Area = Length × Width
C Program
#include <stdio.h>
void rectangleArea(float length, float width)
{
float area;
area = length * width;
printf("Area = %.2f", area);
}
int main()
{
float length = 10.5;
float width = 5.0;
rectangleArea(length, width);
return 0;
}
Sample Output
Area = 52.50
Explanation
Two values are passed:
rectangleArea(length, width);
The function receives them as:
float length
float width
Then:
area = length * width;
calculates:
10.5 × 5.0 = 52.5
Concepts Covered
- Function arguments
floatparameters- Multiplication
- Area calculation
Q8. Check Whether a Number Is Even or Odd
Problem Statement
Create a function that accepts a number and checks whether it is even or odd.
C Program
#include <stdio.h>
void checkEvenOdd(int number)
{
if (number % 2 == 0)
{
printf("%d is even.", number);
}
else
{
printf("%d is odd.", number);
}
}
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
checkEvenOdd(number);
return 0;
}
Sample Output
Enter a number: 28
28 is even.
Explanation
The user enters a number.
For example:
number = 28
The value is passed to:
checkEvenOdd(number);
The function checks:
number % 2 == 0
Because:
28 % 2 = 0
the number is even.
Concepts Covered
- Function arguments
- User input
- Modulus operator
if-else
Q9. Calculate Simple Interest Using Function Arguments
Problem Statement
Create a function that accepts principal, rate, and time and calculates simple interest.
Formula:
Simple Interest = (P × R × T) / 100
C Program
#include <stdio.h>
void simpleInterest(float principal, float rate, float time)
{
float interest;
interest = (principal * rate * time) / 100;
printf("Simple Interest = %.2f", interest);
}
int main()
{
float principal = 10000;
float rate = 5;
float time = 2;
simpleInterest(principal, rate, time);
return 0;
}
Sample Output
Simple Interest = 1000.00
Explanation
Three arguments are passed:
simpleInterest(principal, rate, time);
Their values are:
Principal = 10000
Rate = 5
Time = 2
The function calculates:
(10000 × 5 × 2) / 100
= 1000
Concepts Covered
- Multiple function arguments
float- Mathematical formula
- Function parameters
Q10. Perform Multiple Calculations Using Call by Value
Problem Statement
Create separate functions to calculate the sum, difference, product, and average of two numbers. Pass the numbers to each function using ordinary arguments.
C Program
#include <stdio.h>
void sum(int a, int b)
{
printf("Sum = %d\n", a + b);
}
void difference(int a, int b)
{
printf("Difference = %d\n", a - b);
}
void product(int a, int b)
{
printf("Product = %d\n", a * b);
}
void average(int a, int b)
{
float result;
result = (a + b) / 2.0;
printf("Average = %.2f\n", result);
}
int main()
{
int x = 20;
int y = 10;
sum(x, y);
difference(x, y);
product(x, y);
average(x, y);
return 0;
}
Sample Output
Sum = 30
Difference = 10
Product = 200
Average = 15.00
Explanation
The same two variables are passed to four different functions:
sum(x, y);
difference(x, y);
product(x, y);
average(x, y);
Each function receives its own copies of the values.
The original x and y remain unchanged.
This demonstrates how function arguments allow the same data to be used by different functions.
Concepts Covered
- Multiple functions
- Multiple arguments
- Call by value
- Arithmetic operations
- Reusable functions
Key Takeaways
- Function arguments are values passed to a function.
- Function parameters are variables that receive those values.
- In ordinary C function calls, arguments are passed by value.
- Call by value means the function receives a copy of each argument’s value.
- Changing a parameter inside the function does not change the original variable.
- Multiple arguments can be passed to a function.
- Different data types such as
intandfloatcan be used as parameters. - Call by value is useful when a function only needs to work with copies of the data.
- A simple
swap()function using ordinary parameters cannot change the caller’s original variables. - Pointers allow functions to work with the original objects and will be covered later.
- Understanding arguments and parameters is essential before learning return values and pointers.
- Functions can receive the same variables in multiple function calls without changing them.
FAQs
1. What are function arguments in C?
Function arguments are the actual values passed to a function when it is called.
Example:
add(10, 20);
Here, 10 and 20 are arguments.
2. What are function parameters in C?
Parameters are variables declared in a function definition that receive the arguments.
Example:
void add(int a, int b)
Here, a and b are parameters.
3. What is call by value in C?
Call by value means the function receives a copy of the value passed by the caller. Changes to the parameter do not change the original variable.
4. Does C support call by value?
Yes. C passes ordinary function arguments by value.
For example:
void change(int x)
{
x = 100;
}
Changing x does not change the caller’s original variable.
5. Can a function have multiple arguments?
Yes. A function can accept multiple parameters.
Example:
void add(int a, int b, int c)
{
printf("%d", a + b + c);
}
It can be called as:
add(10, 20, 30);
6. Why does swapping two variables not work with ordinary function arguments?
Because the function receives copies of the values.
Changing the copies does not change the original variables in the calling function.
Pointers can be used when you need a function to modify the original variables.
7. What is the difference between an argument and a parameter?
An argument is the actual value passed during a function call.
A parameter is the variable that receives that value inside the function.
Example:
add(10, 20);
Here 10 and 20 are arguments.
void add(int a, int b)
Here a and b are parameters.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
