liveBook · Manning

Function

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A function is a named or callable program unit that performs an operation and can return a result.

A function is a named or callable program unit that performs an operation and can return a result. It gives other code a clear interface for using that operation, much like a small service inside an application. section 3.2

Useful functions do one thing well. Returning a value instead of printing inside the function keeps the operation reusable: callers can print the result, place it in a web page, store it in a data structure, or convert it to JSON. section 3.2.4

Design functions with these practical habits:

  • Give the function a meaningful name that communicates its operation and fits the surrounding namespace. section 2.3.5
  • Choose parameters that let callers control important variations without forcing them to duplicate conditional logic. section 2.3.5
  • Return computed data rather than combining computation with output or other unrelated side effects. section 3.2.4
  • Keep the function short enough to understand; when it becomes difficult to hold in context, refactor functionality into cooperating functions or a class. section 3.2.5

From output routine to reusable operation

A routine that builds a full name and prints it forces every caller to expect console output and makes testing awkward. A function that builds and returns the name lets each caller decide what to do with the result, while preserving a focused responsibility. section 3.2.4

def full_name(fnameNone, mname=None, lname=None) -> str:
    """Concatenates the names together and returns the full name
    
    Arguments:
        fname {str} -- first name
        mname {str} -- middle name
        lname {str} -- last name
    
    Returns:
        str -- the full name with only a single space between names
    """
    full_name = " ".join(name for name in [fname, mname, lname] if name)
    return full_name

Inspect a focused function that accepts optional name parts, joins available values, and returns the resulting string. section 3.2.4

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Explains naming, parameters, return values, single responsibility, testing, and refactoring, but stops short of language-runtime internals.
Examples
Uses community email name formatting and full-name construction to contrast conditional dispatch, parameters, printing, and returned values.
What it assumes
Assumes familiarity with callers, parameters, return values, and basic object-oriented programming terminology.
What you can do after
Apply single-responsibility design, return reusable results, choose parameters, and refactor functions that become difficult to understand.
How it connects
Connects functions to APIs, methods, object-oriented programming, single responsibility, testing, refactoring, and classes.

Also explained in

Compare how 20 books explain function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.