Defining Your Own Functions
A function's name is not merely a label for documentation.
From Repeated Code to Reusable Names
A function gives a reusable piece of code a name. Instead of treating that code as something that must be written again each time, you define it once and later invoke it by using its name. This makes a function a small, named unit that can be reused within a program.
The central idea is define once, then invoke by name whenever that stored piece of code is needed.
What Definition Creates
Defining a function does more than describe a reusable action. It creates a variable with the same name as the function. That named variable is associated with the function, allowing the program to refer to the stored code through the name.
One Definition, Two Uses
Imagine a function defined with the name format_report.
- 1Define
The program stores the function's code and creates a variable with the name
format_report. - 2Invoke once
The program uses
format_reportto invoke the stored code for the first needed report. - 3Invoke again
The program uses the same name again, reusing the function instead of defining that code a second time.
The function is defined once and invoked multiple times by its name.
Functions as Small Program Units
At a basic level, an object is some code together with data structures that are smaller than a whole program. A function fits this idea because it stores a bit of code and gives that code a name. The name provides the point of reference used later to invoke the stored code.
This perspective explains why functions help organize reuse. The whole program does not have to be treated as one indivisible block. A smaller named unit can hold part of the program's code, and that unit can be invoked when needed.
Moving Reusable Functions Between Programs
Defining functions once solves reuse inside a single program. A further question is how to reuse several functions in other programs. The source identifies modules as the answer: modules provide a way to reuse a number of functions across programs that you write.
| Reuse scope | Mechanism described in the source | What is reused |
|---|---|---|
| Within one program | Define a function once and invoke it by name | A named piece of code |
| Across programs | Use modules | A number of functions |
Functions support reuse within a program; modules address reuse across programs.
Practice the Reuse Pattern
A program needs to perform the same named operation in several places. Describe what should happen first, what name is created, and how the operation is reused afterward. Then explain what changes if another program also needs several of the same functions.
Hints
- Start with the act of defining the function.
- Include the fact that a variable with the function's name is created.
- Separate reuse within one program from reuse across programs.
Common mistake Thinking that defining a function means writing the same code again wherever it is needed.
What it looks likeA learner describes a separate copy of the function's code for every use.
Why it failsThe purpose of defining a function is to store a bit of code once and reuse it through its name.
Do this insteadDescribe one definition followed by later invocations of that named function.
Common mistake Treating the function name as only a descriptive comment.
What it looks likeA learner says the name explains the code but does not participate in using it.
Why it failsDefining a function creates a variable with the same name, and that name is used to invoke the code.
Do this insteadConnect the name to both the variable created by definition and the later invocation.
Common mistake Assuming that defining a function in one program automatically explains reuse across other programs.
What it looks likeA learner describes a function defined in one program but gives no mechanism for sharing functions with another program.
Why it failsThe source distinguishes reuse within a program from reuse across programs and identifies modules as the answer for the latter.
Do this insteadUse the idea of a module when discussing reuse of a number of functions in other programs.
Key Takeaways
- 1A function stores a bit of code and gives it a name.
- 2Defining a function creates a variable with the same name.
- 3The named function can later be invoked repeatedly within a program.
- 4Modules support reusing a number of functions in other programs.
At a glance
- Defining a function creates a named, reusable unit of code.
- The definition creates a variable with the function's name.
- Invoking the name allows the stored code to be reused within the program.
- Modules provide the path to reusing a number of functions across programs.