MCS-201 · Unit 5

Functions

Section 1 of 8

Block 2 · Unit 5Functions11 min · Section 1 of 8

5.1Defining and Calling Functions

Covers IGNOU MCS-201 §5.2

Until now every program has consisted of one function, main. Real programs are built from many functions that call one another. This section explains what a function is, how to write one and what happens when it is called.

Why functions are needed

Splitting a program into functions is called modular programming. Its benefits:

BenefitMeaning
ReuseWrite the code once and call it from many places
Readabilityaverage(marks, n) says what happens without showing how
Easier testing and debuggingEach function can be checked separately
TeamworkDifferent people can write different functions
Less repetitionA change is made in one place only

Functions are of two kinds: library functions such as printf, sqrt and strlen, which come with the compiler, and user-defined functions, which you write.

Parts of a function definition

Syntax
return-type function-name(parameter-list)
{
    local variable declarations;
    statements;
    return expression;
}
PartMeaningExample
Return typeType of the value sent back; void if noneint
Function nameAn identifier, following the usual naming rulessquare
Parameter listTyped variables that receive the caller’s values; void if none(int no)
BodyThe statements between { and }return no * no;

The first line, int square(int no), is called the function header.

Calling a function

A function runs only when it is called by writing its name followed by arguments in parentheses, such as sq = square(n);. The function that makes the call is the calling function; the one that runs is the called function.

1main runs until it reaches the call square(n)
→
2main pauses; the value of n is copied into the parameter no
→
3square runs its body
→
4return sends the result back
→
5main resumes at the point of the call
Flow of control in a function call

Key takeaways

  • Functions split a program into reusable, testable modules.
  • A definition has a return type, a name, a parameter list and a body.
  • A function runs only when it is called; control then returns to the caller.
  • Library functions are supplied; user-defined functions are written by the programmer.

Practice questions

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

Question 1 · multiple choice

Which of these is a function call?

Show answer

Answer: C. show();

A call is the name followed by parentheses, with no type in front. The first option is a declaration and the second a definition.

Question 2 · multiple choice

What happens to the calling function while the called function runs?

Show answer

Answer: D. It is suspended and resumes after the call returns

The caller pauses at the call and continues from that point when the called function finishes.

Question 3 · multiple choice

In float area(float r), what is float r?

Show answer

Answer: A. A parameter

Items in the parentheses of the header are parameters, which receive the caller’s values. The return type is the float before the name.

Question 4 · multiple choice

What does this program print?

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

void greet(void)
{
    printf("B ");
}

int main(void)
{
    printf("A ");
    greet();
    greet();
    printf("C\n");
    return 0;
}
Show answer

Answer: C. A B B C

main prints A, then each call to greet prints B, then main prints C. The definition’s position above main does not make it run first.

Question 5 · multiple choice

Which is NOT an advantage of functions?

Show answer

Answer: C. The program always uses less memory

Functions improve structure and reuse; they do not guarantee lower memory use, since each call needs some memory of its own.

Question 6 · short answer

Write a function cube that returns the cube of an integer, and show the statement in main that prints the cube of 4.

Show worked answer

int cube(int n) { return n * n * n; }. In main: printf("%d\n", cube(4));. The call passes 4 to the parameter n, the function computes 4 × 4 × 4 = 64 and returns it, and printf prints 64.

↑ ↓ move↵ openTab switch type