Function

noun

say it: “FUNK-shun” /ˈfʌŋkʃən/

A named block of code that does one job; you write it once and run it as often as you like by calling its name.

What is a function in Python?

A function is a named block of code you write once with def and run whenever you need it, by calling its name with parentheses. It can take values in and hand a value back.

def add_tip(bill):
    return round(bill * 1.18, 2)

print(add_tip(40))   # 47.2
print(add_tip(25))   # 29.5

In a sentence

  • “The step counter has one function per job: one adds up the days, one finds the best day.”
  • “She wrote a function that works out the tip, then called it for every table's bill.”

Level: Apprentice · More Python terms at this level

Updated

In other subjects