What is Return value?

The value a function returns to its caller when it finishes. In pseudocode, the value of the expression written after return becomes the return value, and the function ends when return is executed.

Japanese name: 戻り値

Return value appears in 9 practice questions across 6 topics, including ISA & Assembly, Linkers, Loaders & Executables, Programming Basics (Pseudocode Tracing).

Field: Programming Basics (Pseudocode Tracing)

Try it: questions about Return value

In the standard RISC-V calling convention, which registers pass integer arguments and return values?

  1. AArguments in s0-s7; return value in ra
  2. BArguments in a0-a7; return value in a0 (and a1)
  3. CAll arguments on the stack; return value in sp
Show answer and explanation

Answer: B. Arguments in a0-a7; return value in a0 (and a1)

In the standard RISC-V calling convention, up to eight integer arguments are passed in a0-a7 (x10-x17), and the return value comes back in a0, or in a0 and a1 for values such as 64-bit integers on RV32. Passing through registers, which are faster than memory, keeps call overhead low. Arguments beyond eight go on the stack. Floating-point arguments use fa0-fa7.

A function f(x, y) is defined as “if y is 0, return x; otherwise return f(y, x mod y)”. When f(84, 36) is called, what is the return value, and how many times is f called, including the first call?

  1. AReturns 12; called 3 times
  2. BReturns 12; called 4 times
  3. CReturns 6; called 3 times
Show answer and explanation

Answer: A. Returns 12; called 3 times

f(84, 36) calls f(36, 12) because 84 mod 36 = 12; that calls f(12, 0) because 36 mod 12 = 0; and since y is 0, it returns 12. The calls are f(84, 36), f(36, 12) and f(12, 0), three in total, and this is the Euclidean algorithm for the greatest common divisor. Because the function returns the result of its recursive call directly (tail recursion), a compiler that performs tail call optimization can turn it into a loop, so it does not keep using stack space however deep the recursion goes.

Return value in the explanations

Questions that mention Return value (9)

  1. In the standard RISC-V calling convention, which registers pass integer arguments and return values?
  2. A function f(x, y) is defined as “if y is 0, return x; otherwise return f(y, x mod y)”. When f(84, 36) is called, what is the return value, and how many times is f called, including the first call?
  3. When the function f above is called three times in a row, which of the following is the return value of the third call?
  4. What is the main purpose of defining a calling convention?
  5. Which program error can be detected at compile time?
  6. In a C program, which statement is correct about the address where the OS first starts execution (the entry point)?
  7. When a C main function ends with "return 3;", what generally happens to the value 3?
  8. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. When the following function pow is called as pow(2, 10), how many times is pow called, including the first call?
  9. When the following function c is called as c(4, 2), how many times is c called, including the first call?

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Argument Stack Calling convention Recursion Variable Modulo operation Application binary interface (ABI) Entry point Linker Shell

Back to the glossary