Practical / Solution
Formal & Actual Arguments — Exercise 1
Problem
Write a C program to add two numbers using formal and actual arguments.
Program
#include <stdio.h>
int add(int a, int b)
{
// a and b are formal arguments
return a + b;
}
int main()
{
int x, y, result;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
// x and y are actual arguments
result = add(x, y);
printf("Sum = %d\n", result);
return 0;
}
Explanation
The parameters a and b in the function
definition are called formal arguments. The values x
and y passed during the function call are actual arguments.
Expected Output
Enter two numbers: 15 25
Sum = 40
Formal & Actual Arguments — Exercise 2
Problem
Write a C program to calculate the square of a number using a function
with a formal argument.
Program
#include <stdio.h>
int square(int num)
{
// num is a formal argument
return num * num;
}
int main()
{
int value, result;
printf("Enter a number: ");
scanf("%d", &value);
// value is the actual argument
result = square(value);
printf("Square = %d\n", result);
return 0;
}
Explanation
The variable num receives the value passed by
value. Therefore, num is the formal
argument and value is the actual argument.
Expected Output
Enter a number: 9
Square = 81
Formal & Actual Arguments — Exercise 3
Problem
Write a C program to find the larger of two numbers using formal
and actual arguments.
Program
#include <stdio.h>
int largest(int a, int b)
{
// a and b are formal arguments
if (a > b)
return a;
else
return b;
}
int main()
{
int x, y, result;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
// x and y are actual arguments
result = largest(x, y);
printf("Largest = %d\n", result);
return 0;
}
Explanation
The function receives two values through its formal arguments.
The variables used in the function call are the actual arguments.
Expected Output
Enter two numbers: 45 72
Largest = 72
Formal & Actual Arguments — Exercise 4
Problem
Write a C program to calculate the area of a rectangle using
formal and actual arguments.
Program
#include <stdio.h>
float area(float length, float width)
{
// length and width are formal arguments
return length * width;
}
int main()
{
float l, w, result;
printf("Enter length and width: ");
scanf("%f %f", &l, &w);
// l and w are actual arguments
result = area(l, w);
printf("Area = %.2f\n", result);
return 0;
}
Explanation
The function parameters length and width
receive values from the actual arguments l and w.
Expected Output
Enter length and width: 10 5
Area = 50.00
Formal & Actual Arguments — Exercise 5
Problem
Write a C program to calculate the simple interest using a function
with three arguments.
Program
#include <stdio.h>
float simpleInterest(float principal, float rate, float time)
{
// All three parameters are formal arguments
return (principal * rate * time) / 100;
}
int main()
{
float p, r, t, interest;
printf("Enter principal, rate and time: ");
scanf("%f %f %f", &p, &r, &t);
// p, r and t are actual arguments
interest = simpleInterest(p, r, t);
printf("Simple Interest = %.2f\n", interest);
return 0;
}
Explanation
The function receives three values through formal arguments.
The variables supplied during the function call are actual arguments.
Expected Output
Enter principal, rate and time: 10000 5 2
Simple Interest = 1000.00
Formal & Actual Arguments — Exercise 6
Problem
Write a C program to calculate the average of three numbers using a function.
Program
#include <stdio.h>
float average(int a, int b, int c)
{
// a, b and c are formal arguments
return (a + b + c) / 3.0;
}
int main()
{
int x, y, z;
float result;
printf("Enter three numbers: ");
scanf("%d %d %d", &x, &y, &z);
// x, y and z are actual arguments
result = average(x, y, z);
printf("Average = %.2f\n", result);
return 0;
}
Explanation
The function accepts three values as formal arguments and returns
their average. The variables from main() are actual arguments.
Expected Output
Enter three numbers: 10 20 30
Average = 20.00
Formal & Actual Arguments — Exercise 7
Problem
Write a C program to exchange two values using a function and
demonstrate the difference between actual and formal arguments.
Program
#include <stdio.h>
void exchange(int a, int b)
{
int temp;
// a and b are formal arguments
temp = a;
a = b;
b = temp;
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);
// x and y are actual arguments
exchange(x, y);
printf("After function call: x = %d, y = %d\n", x, y);
return 0;
}
Explanation
In this program, x and y are actual arguments,
while a and b are formal arguments.
Since ordinary arguments are passed by value, changes to the formal
arguments do not change the original variables.
Expected Output
Enter two numbers: 10 20
Before function call: x = 10, y = 20
Inside function: a = 20, b = 10
After function call: x = 10, y = 20
Formal & Actual Arguments — Exercise 8
Problem
Write a C program to calculate the maximum of three numbers using
a function with three arguments.
Program
#include <stdio.h>
int maximum(int a, int b, int c)
{
int max;
// Find the largest value
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);
// x, y and z are actual arguments
result = maximum(x, y, z);
printf("Maximum = %d\n", result);
return 0;
}
Explanation
The function's parameters are formal arguments, while the variables
passed from main() are actual arguments.
Expected Output
Enter three numbers: 35 90 62
Maximum = 90
Formal & Actual Arguments — Exercise 9
Problem
Write a C program to calculate the total and average marks of three subjects
using a function with arguments.
Program
#include <stdio.h>
int totalMarks(int m1, int m2, int m3)
{
// m1, m2 and m3 are formal arguments
return m1 + m2 + m3;
}
int main()
{
int marks1, marks2, marks3, total;
float average;
printf("Enter marks of three subjects: ");
scanf("%d %d %d", &marks1, &marks2, &marks3);
// Actual arguments are passed to the function
total = totalMarks(marks1, marks2, marks3);
average = total / 3.0;
printf("Total = %d\n", total);
printf("Average = %.2f\n", average);
return 0;
}
Explanation
The function receives the three subject marks through its formal
arguments. The variables from main() act as actual arguments.
Expected Output
Enter marks of three subjects: 70 80 90
Total = 240
Average = 80.00
Formal & Actual Arguments — Exercise 10
Problem
Write a C program using separate functions to calculate the area and
perimeter of a rectangle. Demonstrate the use of formal and actual arguments.
Program
#include <stdio.h>
float area(float length, float width)
{
// length and width are formal arguments
return length * width;
}
float perimeter(float length, float width)
{
// length and width are formal arguments
return 2 * (length + width);
}
int main()
{
float l, w;
printf("Enter length and width: ");
scanf("%f %f", &l, &w);
// l and w are actual arguments
printf("Area = %.2f\n", area(l, w));
printf("Perimeter = %.2f\n", perimeter(l, w));
return 0;
}
Explanation
Both functions receive values through their formal parameters.
The variables l and w supplied during each
function call are the actual arguments. The same actual arguments
can be passed to multiple functions.
Expected Output
Enter length and width: 10 5
Area = 50.00
Perimeter = 30.00