Programming Fundamentals/Functions/Functions

A function is a sequence of program instructions that performs a specific task, packaged as a unit. Depending on the programming language, a function may be called a subroutine, a procedure, a routine, a method, or a subprogram. The generic term, callable unit, is sometimes used. This activity introduces functions. This activity will help you understand how to use functions in a program.

Objectives

 * Understand function concepts.
 * Understand how functions are specified in a program.
 * Single-step through a program to observe function execution.

Prerequisites
Learners should already be familiar with a visual programming environment.

Introduction


Review the flowchart example on the right.

Questions

 * What flowcharting symbol is used to call a function?
 * What syntax is used to call a function?
 * What naming convention is used to identify functions?

Activity
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 use a slow run speed and/or step through the program one shape at a time.
 * 6) Working together, create a list of function names that would be used to follow the input-process-output model and convert Fahrenheit temperature to Celsius.

Applications

 * Identify specific steps which must be followed when creating a program using functions.
 * 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?