Concepts / Defining Your Own Functions

Defining Your Own Functions

You have seen how you can reuse code in your program by defining functions once. What if you wanted to reuse a number of functions in other programs that you write? As you might have guessed, the answer is modules.

  • Programming

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.

createsinvokesinvokesFunction definitionstored codeFunction namenamed variableInvocation 1uses the nameInvocation 2uses the name
What happens between defining a function and invoking it multiple times in a program?

One Definition, Two Uses

Imagine a function defined with the name format_report.

Define: The program stores the function's code and creates a variable with the name format_report.

Invoke once: The program uses format_report to invoke the stored code for the first needed report.

Invoke 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.

containsreused byreused byModulereusable functionsFunctionnamed codeProgram Ainvokes functionProgram Binvokes function
How does a function move from one module into another program so it can be reused?
Reuse scopeMechanism described in the sourceWhat is reused
Within one programDefine a function once and invoke it by nameA named piece of code
Across programsUse modulesA number of functions

Practice the Reuse Pattern

EASY

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.
  • Thinking that defining a function means writing the same code again wherever it is needed.

    The purpose of defining a function is to store a bit of code once and reuse it through its name.

    Fix: Describe one definition followed by later invocations of that named function.

  • Treating the function name as only a descriptive comment.

    Defining a function creates a variable with the same name, and that name is used to invoke the code.

    Fix: Connect the name to both the variable created by definition and the later invocation.

  • Assuming that defining a function in one program automatically explains reuse across other programs.

    The source distinguishes reuse within a program from reuse across programs and identifies modules as the answer for the latter.

    Fix: Use the idea of a module when discussing reuse of a number of functions in other programs.

Key Takeaways

  1. A function stores a bit of code and gives it a name.
  2. Defining a function creates a variable with the same name.
  3. The named function can later be invoked repeatedly within a program.
  4. Modules support reusing a number of functions in other programs.

Key Takeaways

  • 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.