liveBook · Manning

Function argument

As explained in A Pythonic Adventure: From Python basics to a working web app

A function argument is a value supplied to a function call for the function to operate on.

A function argument is information supplied inside a function call’s parentheses. The function uses that information to do its job: it might print a message, calculate a result, or work with a string. Arguments can be strings, numbers, variables, or several values at once. section 1.2 section 1.4

A call such as print("Welcome!") passes a string argument to print(). input("Please enter your name: ") passes a prompt to input(). A function may return an answer instead of printing it; store that returned value in a variable, then use or print the variable afterward. section 1.2

Arguments make one reusable function handle changing parts of a task. When code repeats, identify what stays the same and what changes; pass the changing choices, title, and prompt as arguments rather than rewriting the whole menu. section 3.0

When calling a function, keep this small checklist handy:

  • Put the values the function needs between its parentheses. section 1.4
  • Pass each value in the order the function expects, such as choices, then a title, then a prompt. section 3.0
  • Save a returned result in a variable when the program needs it later. section 1.2 section 3.1

One menu function, three orders

The menu function receives a list of choices plus optional title and prompt text. Calling menu(drinks) uses its default title and prompt; calls for flavors and toppings supply new text. Each call returns the selected item, which is saved in drink, flavor, or topping. section 3.0

Listing 3.1 The full menu program with a function
def menu(choices, title="Erik's Menu", prompt="Choose your item: "):
    print(title)
    print(len(title) * "-")
    i = 1
    for c in choices:
        print(i, c)
        i = i + 1
    choice = input(prompt)
    answer = choices[int(choice) - 1]
 
    return answer
 
 
drinks = ["chocolate", "coffee", "decaf"]
flavors = ["caramel", "vanilla", "peppermint", "raspberry", "plain"]
toppings = ["chocolate", "cinnamon", "caramel"]
 
drink = menu(drinks)
flavor = menu(flavors, "Erik's flavors", "Choose your flavor: ")
topping = menu(toppings, "Erik's toppings", "Choose your topping: ")
 
print("Here is your order: ")
print("Main product: ", drink)
print("Flavor: ", flavor)
print("Topping: ", topping)
print("Thanks for your order!")

This complete coffee-order program shows arguments passed to menu() and the returned selections used in the final order. section 3.0

How A Pythonic Adventure: From Python basics to a working web app covers it

How deep it goes
Explains passing strings, numbers, and variables through calls, then works through a three-argument menu function with defaults.
Examples
Demonstrates print and input prompts, then a coffee-shop menu selecting drinks, flavors, and toppings.
What it assumes
Starts from zero by defining calls and strings while using previously written Python functions before creating one.
What you can do after
Build it from scratch by writing and calling a menu function that accepts choices, titles, and prompts.
How it connects
Connects function arguments with functions, return values, variables, strings, lists, loops, and default values.

Also explained in

Compare how 5 books explain function argument →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of A Pythonic Adventure: From Python basics to a working web app. The section links point to the parts of the book it draws on.