Concepts / Defining Your Own Functions

Defining Your Own Functions

A function's name is not merely a label for documentation.

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

Key point

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.

Function definitionstored codeFunction namenamed variableInvocation 1uses the nameInvocation 2uses the namecreatesinvokesinvokes
What happens between defining a function and invoking it multiple times in a program?
Worked example

One Definition, Two Uses

Imagine a function defined with the name format_report.

  1. 1
    Define

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

  2. 2
    Invoke once

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

  3. 3
    Invoke again

    The program uses the same name again, reusing the function instead of defining that code a second time.

Result

The function is defined once and invoked multiple times by its name.

Functions as Small Program Units

Definition

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.

Modulereusable functionsFunctionnamed codeProgram Ainvokes functionProgram Binvokes functioncontainsreused byreused by
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

Functions support reuse within a program; modules address reuse across programs.

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

    Thinking that defining a function means writing the same code again wherever it is needed.

    What it looks like

    A learner describes a separate copy of the function's code for every use.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Treating the function name as only a descriptive comment.

    What it looks like

    A learner says the name explains the code but does not participate in using it.

    Why it fails

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

    Do this instead

    Connect 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 like

    A learner describes a function defined in one program but gives no mechanism for sharing functions with another program.

    Why it fails

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

    Do this instead

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

Key Takeaways

  1. 1
    A function stores a bit of code and gives it a name.
  2. 2
    Defining a function creates a variable with the same name.
  3. 3
    The named function can later be invoked repeatedly within a program.
  4. 4
    Modules 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.

Related concepts

More concepts starting with D