You've called print and len — both functions Python ships with. Today you write your own. What do you think the shape looks like?
A name, some inputs, some code that runs?
Exactly. The keyword is def ("define"):
def greet(name):
print(f"Hello, {name}!")Three parts: def, the function's name, parentheses with parameter names, colon, then an indented body. After the definition runs, greet is a name you can call:
greet("Ada") # Hello, Ada!
greet("Bob") # Hello, Bob!Why define instead of just writing the print line each time?
Two reasons. First, reuse: you write the recipe once, run it many times with different inputs. Second, naming: a function gives a chunk of code a name that explains what it does. greet(name) reads more cleanly than four lines of f-string and print.
And name inside the function — is that a normal variable?
Local to the function. Each call sets name to whatever you passed in (the argument). After the call ends, name is gone. Variables defined outside the function aren't visible inside unless you pass them as arguments.
def name(params):A function is a named, reusable block of code that takes inputs and runs a body.
def greet(name):
print(f"Hello, {name}!")def — the keyword that starts a function definitiongreet — the function's name (same naming rules as variables)(name) — the parameter list; names that will hold the values you pass when calling: and indented body — the recipe Python runs each time the function is calledUse the function's name with parentheses:
greet("Ada") # arguments go in the parensThe value "Ada" is the argument — the actual value passed to the call. Inside the function it's bound to the parameter name name.
def greet(first, last):
print(f"Hello, {first} {last}!")
greet("Ada", "Lovelace") # Hello, Ada Lovelace!Separate parameters with commas. Order matters — Python matches by position.
def greet_world():
print("Hello, world!")
greet_world() # call with empty parensStill need the parentheses on definition and on call.
Names defined inside a function only exist inside the function:
def greet(name):
message = f"Hello, {name}"
print(message)
greet("Ada")
print(message) # NameError — `message` was local to the functionFunctions are walls. What's defined inside doesn't leak out. (To get a value out, you return it — next lesson.)
For a one-line job, def adds overhead. The break-even is around three uses or three lines of body. Past that, functions:
Three wins for one keyword. Most Python programs are mostly functions.
You've called print and len — both functions Python ships with. Today you write your own. What do you think the shape looks like?
A name, some inputs, some code that runs?
Exactly. The keyword is def ("define"):
def greet(name):
print(f"Hello, {name}!")Three parts: def, the function's name, parentheses with parameter names, colon, then an indented body. After the definition runs, greet is a name you can call:
greet("Ada") # Hello, Ada!
greet("Bob") # Hello, Bob!Why define instead of just writing the print line each time?
Two reasons. First, reuse: you write the recipe once, run it many times with different inputs. Second, naming: a function gives a chunk of code a name that explains what it does. greet(name) reads more cleanly than four lines of f-string and print.
And name inside the function — is that a normal variable?
Local to the function. Each call sets name to whatever you passed in (the argument). After the call ends, name is gone. Variables defined outside the function aren't visible inside unless you pass them as arguments.
def name(params):A function is a named, reusable block of code that takes inputs and runs a body.
def greet(name):
print(f"Hello, {name}!")def — the keyword that starts a function definitiongreet — the function's name (same naming rules as variables)(name) — the parameter list; names that will hold the values you pass when calling: and indented body — the recipe Python runs each time the function is calledUse the function's name with parentheses:
greet("Ada") # arguments go in the parensThe value "Ada" is the argument — the actual value passed to the call. Inside the function it's bound to the parameter name name.
def greet(first, last):
print(f"Hello, {first} {last}!")
greet("Ada", "Lovelace") # Hello, Ada Lovelace!Separate parameters with commas. Order matters — Python matches by position.
def greet_world():
print("Hello, world!")
greet_world() # call with empty parensStill need the parentheses on definition and on call.
Names defined inside a function only exist inside the function:
def greet(name):
message = f"Hello, {name}"
print(message)
greet("Ada")
print(message) # NameError — `message` was local to the functionFunctions are walls. What's defined inside doesn't leak out. (To get a value out, you return it — next lesson.)
For a one-line job, def adds overhead. The break-even is around three uses or three lines of body. Past that, functions:
Three wins for one keyword. Most Python programs are mostly functions.
Create a free account to get started. Paid plans unlock all tracks.