MCS-201 · Unit 5

Functions

Section 6 of 8

Block 2 · Unit 5Functions7 min · Section 6 of 8

5.6Categories of Functions: Arguments and Return Values

Covers IGNOU MCS-201 §5.7

Functions differ in how they exchange data with the caller: some need inputs, some give back a result, some do both and some neither. This section covers the four combinations and the terms for the values passed in a call.

Actual and formal arguments

TermAlso calledWhere it appearsExample
Actual argumentsArgumentsIn the function callsum(a1, a2, a3)
Formal argumentsParametersIn the function headervoid sum(int f1, int f2, int f3)

When the call happens, the value of each actual argument is copied into the matching formal argument. They must agree in number, order and type (or be convertible to the parameter’s type).

The four categories

CategoryArgumentsReturn valueData flowExample prototype
1NoNoNone; only control is transferredvoid message(void);
2NoYesFunction → callerint cal_sum(void);
3YesNoCaller → functionvoid show_sum(int, int, int);
4YesYesBoth waysint sum(int, int, int);

Which category to choose

  • Prefer category 4 for calculations: inputs come as arguments, and the result goes back with return. The caller decides what to do with the result.
  • Use category 3 for actions on given data, such as printing a report.
  • Use category 1 or 2 when the function needs no information from the caller, such as printing a menu or reading a value from the user.

Key takeaways

  • Actual arguments are in the call; formal arguments (parameters) are in the definition.
  • They must match in number, order and type.
  • Four categories: no arguments/no return, no arguments/return, arguments/no return, arguments/return.
  • Functions with arguments and a return value are the most flexible.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In result = area(len, wid); with the definition int area(int l, int w), which are the formal arguments?

Show answer

Answer: B. l and w

Formal arguments are the parameters in the function header. len and wid are the actual arguments in the call.

Question 2 · multiple choice

To which category does void line(void) belong?

Show answer

Answer: A. No arguments, no return value

The parameter list is void and the return type is void, so nothing is passed either way.

Question 3 · multiple choice

In which category is there two-way data communication between caller and function?

Show answer

Answer: D. With arguments and a return value

Arguments carry data into the function and the return value carries a result back.

Question 4 · multiple choice

What does this program print?

C · arg_order.c
#include <stdio.h>

int diff(int a, int b)
{
    return a - b;
}

int main(void)
{
    int x = 3, y = 10;
    printf("%d\n", diff(y, x));
    return 0;
}
Show answer

Answer: C. 7

Arguments are matched by position: a receives y (10) and b receives x (3), so the result is 7. Matching by name would wrongly give −7.

Question 5 · multiple choice

Which prototype belongs to the category “no arguments, with a return value”?

Show answer

Answer: C. int read_choice(void);

read_choice takes nothing (void) and returns an int.

Question 6 · short answer

Write a category 4 function simple_interest that takes principal, rate and time and returns the interest, and compute it for 10000, 5 and 2.

Show worked answer

float simple_interest(float p, float r, float t) { return p * r * t / 100; }. Call: si = simple_interest(10000, 5, 2);. The actual arguments 10000, 5 and 2 are copied to the formal arguments p, r and t. The function computes 10000 × 5 × 2 ÷ 100 = 1000 and returns 1000.0, which is stored in si.

↑ ↓ move↵ openTab switch type