liveBook · Manning

Call

As explained in Get Programming: Learn to code with Python

A function call is an invocation of a function that executes its body with supplied arguments and produces a result.

Defining a function gives it a name, but does not run it. A function call runs its body with supplied values and can provide a result for the rest of the program. section 21.2

A call works like this:

  • Write the function name with parentheses and actual parameters—the values used in the call. These map to formal parameters in order. section 21.2
  • The call creates a scope for that invocation. Its variables are local to the call and disappear when it returns. section 21.2
  • The call expression is replaced by the returned value. A function can return one tuple containing multiple values; without an explicit return, it returns None. section 21.4
Listing 21.3. How to make a call to a function
def word_length(word1, word2):                            #1
    word = word1+word2                                    #1
    return len(word)                                      #1
    print("this never gets printed")

length1 = word_length("Rob", "Banks")                     #2
length2 = word_length("Barbie", "Kenn")                   #2
length3 = word_length("Holly", "Jolley")                  #2

print("One name is", length1, "letters long.")            #3
print("Another name is", length2, "letters long.")        #3
print("The final name is", length3, "letters long.")      #3

Follow calls to word_length with different names, then see how each returned length is used. section 21.2

How Get Programming: Learn to code with Python covers it

How deep it goes
Traces parameter mapping, call scope, and returned values, including tuples, implicit None, and chained calls.
Examples
Demonstrates word_length calls, tuple-returning addition and subtraction, and chained calls to nested functions.
What it assumes
Assumes function definitions and formal parameters, then distinguishes them from actual parameters supplied at call time.
What you can do after
Apply calls by supplying values, capturing returned results, and using those results in later statements.
How it connects
Connects calls to function parameters, scope, return values, tuples, docstrings, and nested functions.

Also explained in

Compare how 7 books explain call →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.