Programming Fundamentals/Functions/Return Values
Appearance
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 | edit source]- 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 | edit source]Learners should already be familiar with functions and parameters.
Introduction
[edit | edit source]Review the flowchart example on the right.
Questions
[edit | edit source]- 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 | edit source]With a partner, perform the following:
- Using a visual programming language, create a program matching the flowchart on the right.
- Save the program.
- Test the program to verify that it works correctly.
- Trade places, so that both partners have an opportunity to "drive" the visual programming environment.
- Change the environment to display local variables and step through the program one shape at a time.
- 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 | edit source]- 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?