Title: Modularization
Course: COP1000: Introduction to Programming > Section: Module Eleven

Introduction

In computer science, a subroutine, also termed procedure, function, routine, method, or subprogram, is a part of source code within a larger computer program that performs a specific task and is relatively independent of the remaining code. As the name subprogram suggests, a subroutine behaves in much the same way as a computer program that is used as one step in a larger program or another subprogram. A subroutine is often coded so that it can be started (called) several times and/or from several places during one execution of the program, including from other subroutines, and then branch back (return) to the next instruction after the call once the subroutine's task is done. (http://en.wikipedia.org/wiki/Function_%28computer_science%29)

In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments. An ordered list of parameters is usually included in the definition of a subroutine, so that, each time the subroutine is called, its arguments for that call can be assigned to the corresponding parameters. (http://en.wikipedia.org/wiki/Parameter_%28computer_science%29)

A subroutine call may also have side effects, such as modifying data structures in computer memory, reading from or writing to a peripheral device, creating a file, halting the program or the machine, or even delaying the program's execution for a specified time. (http://en.wikipedia.org/wiki/Function_%28computer_science%29)

A return statement causes execution to leave the current subroutine and resume at the point in the code immediately after where the subroutine was called, known as its return address. The return address is saved, usually on the process's call stack, as part of the operation of making the subroutine call. Return statements in many languages allow a function to specify a return value to be passed back to the code that called the function. (http://en.wikipedia.org/wiki/Return_value)

Objectives

Upon completion of this module, the student will be able to:

Define the following: "Function", "parameter", "side effect", "return value"
Implement functions in a program
Pass parameters to a function
Return a value from a function

Readings & Resources

Chapter 6: Dragon Realm p. 56-76