MCS-201 · Unit 5

Functions

Section 3 of 8

Block 2 · Unit 5Functions10 min · Section 3 of 8

5.3The return Statement

Covers IGNOU MCS-201 §5.5

A function communicates its result to the caller through the return statement. This section covers how return works, what a function’s return type means, and what happens when there are several return statements or none.

What return does

Syntax
return expression;     /* in a function that returns a value */
return;                /* in a void function */

The return statement does two things:

  1. It ends the function immediately; any statements after it are skipped.
  2. It sends the value of the expression back to the caller, converted to the function’s return type.

The parentheses in return (x); are optional; return x; means the same.

Return types

Return typeMeaningExample
int, float, char, …The function returns one value of that typefloat average(int a, int b)
voidThe function returns nothingvoid print_line(void)

A function can return only one value. (Returning several results needs pointers, arrays or structures, covered in later units.) The value of a call can be used anywhere a value of that type is allowed: x = max(a, b);, printf("%d", max(a, b));, if (max(a, b) > 10).

Several return statements

A function may contain more than one return, but only one of them executes in any single call.

Returning a list of values

return (2, 3); does not return two values. The comma here is the comma operator from Unit 2: the left operand is evaluated and discarded, and the value of the right operand, 3, is returned.

Functions that return nothing, and missing returns

A void function ends when it reaches its closing brace or a plain return;. In a function with a non-void return type, every path must end in a return with a value; if the caller uses the result of a call that ended without one, the behaviour is undefined.

Key takeaways

  • return ends the function and passes one value back to the caller.
  • The value is converted to the declared return type; void means no value.
  • Multiple return statements are allowed; one executes per call.
  • Every function needs an explicit return type; there is no default int in modern C.

Practice questions

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

Question 1 · multiple choice

How many values can a C function return through return?

Show answer

Answer: B. One

A return statement passes back a single value. Several results need pointers, arrays or structures.

Question 2 · multiple choice

What does return (4, 7); return from an int function?

Show answer

Answer: D. 7

The comma operator discards 4 and yields 7. It does not return two values or their sum.

Question 3 · multiple choice

Which return type is used for a function that only prints a message?

Show answer

Answer: C. void

void states that the function returns no value. null and empty are not C types.

Question 4 · multiple choice

What does this program print?

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

int check(int n)
{
    if (n < 0)
        return -1;
    printf("checked ");
    return 1;
}

int main(void)
{
    printf("%d\n", check(-5));
    return 0;
}
Show answer

Answer: A. -1

For −5 the first return executes and ends the function, so “checked ” is never printed.

Question 5 · multiple choice

What does this program print?

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

int half(int n)
{
    return n / 2.0;
}

int main(void)
{
    printf("%d\n", half(7));
    return 0;
}
Show answer

Answer: C. 3

7 / 2.0 is 3.5, but the return type is int, so the value is converted to 3 (truncated) before being returned.

Question 6 · short answer

Write a function is_even that returns 1 if its integer argument is even and 0 otherwise, and trace it for 10.

Show worked answer

int is_even(int n) { if (n % 2 == 0) return 1; return 0; } (or simply return n % 2 == 0;). Trace for 10: 10 % 2 is 0, the condition is true, so return 1; executes and the function ends; the final return 0; is not reached. The call is_even(10) has the value 1.

↑ ↓ move↵ openTab switch type