Programming Fundamentals/Functions/Return Values

A return statement causes execution to leave the current function and resume at the point in the code immediately after where the function was called. Return statements in many languages allow a function to specify a return value to be passed back to the code that called the function.[1] This activity introduces return values. This activity will help you understand how to use return values in a program.

Objectives

edit
 
Main function
 
GetName function
 
DisplayHello function
  • Understand function return values.
  • Understand how function return values are specified in a program.
  • Single-step through a program to observe function return values.

Prerequisites

edit

Learners should already be familiar with functions and parameters.

Introduction

edit

Review the flowchart example on the right.

Questions

edit
  • What syntax is used to return a value from a function?
  • How are return values defined inside a called function?
  • How are return values referenced from the calling function?

Activity

edit

With a partner, perform the following:

  1. Using a visual programming language, create a program matching the flowchart on the right.
  2. Save the program.
  3. Test the program to verify that it works correctly.
  4. Trade places, so that both partners have an opportunity to "drive" the visual programming environment.
  5. Change the environment to display local variables and step through the program one shape at a time.
  6. Working together, create a list of function return values that would be used to follow the input-process-output model and convert Fahrenheit temperature to Celsius.

Applications

edit
  • Identify specific steps which must be followed when creating a program using function return values.
  • Discuss your activity experience with your classmates. What surprised you? What have you learned that you can apply to your own school or work environment?

References

edit