ProceduralOCR J277
DashboardCoursePracticeReviewProgressGlossary
Saved on this deviceContinue learning

Procedural · OCR J277 programming practice

Progress stays in this browser unless you export it.

Course/Procedures & functions/Functions and returned values
J277 · 2.2.3

Unit 9 · Lesson 3

Functions and returned values

Lesson progress0 / 7

Build the idea

Start with the concept and its key vocabulary.

Key idea

Write a function that returns a result and use the returned value in the main program.

A function produces a value for the rest of the program. ERL encloses it with function and endfunction; Python uses def. In both languages, return sends the result back to the call. Printing inside a function only displays something—it does not give the caller a value to store or reuse.

Function

A sub-program that returns a value.

Return

Send a value from a function back to the instruction that called it.

Returned value

The result supplied by a completed function call.

Assignment

Storing a value, including a returned value, in a variable.

Keep these in mind

  • A function must use return to send its result to the caller.
  • Store a returned value with assignment, such as total = calculateTotal(4, 3).
  • print and return have different jobs.
  • Code after an executed return in the same function is not reached.
  • A returned value can be printed, compared or used in another calculation.

Learning objective

Write a function that returns a result and use the returned value in the main program.

Exam tip

If a question asks for a function, return the result. Then show a separate call such as result = newPrice(5, "premium").

Common mistake

print(total) inside a function displays the result but returns no usable value. Write return total and print it later if required.

PreviousNext