Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

Functions

A function is a re-usable block of code. When a function is called, values can be passed to the function to
be used in the code block. Execution jumps to the function, and the statements in the code block are
executed in sequence. When the function is finished, it returns; control it transferred back to the calling
function, and a single value might be returned.

Function Calls
A function is called as the name of the function, followed by parentheses containing the arguments (see
below). If the function has a return value (see below), the call evaluates to the return value.

A function may be called from within another function, referred to sometimes as hierarchical function
calls.

Advantages of Functions
 Functions make code, especially the main function, easier to understand.
 Functions can reduce redundant code.
 Functions can make testing easier.

Common Errors
 Creating a function by copy/pasting or cut/pasting code, but failing to make all the necessary
modifications for the new function.
 Returning the wrong value, or failing to return a value; the latter is especially problematic, as
such a function might sometimes still return the correct value, or other value.

You might also like