Coding for All
Lesson 24: Function Inputs
Function Inputs Give a function information to work with.
1def greet(name):
2 print(f"Hello, {name}!")
3
4greet("Ada")
5greet("Grace")
Terminal
$ python3 main.py
Hello, Ada!
Hello, Grace!
Every line, explained
def greet(name):
def defines a function: your own named block of code. The name is followed by round brackets and a colon, and the body is indented. Defining a function does nothing on its own: it waits until somebody calls it. In Python you must define a function before the line that calls it. This one has a parameter called name between the brackets, a value the caller must hand in.
print(f"Hello, {name}!")
The f right before the opening quote makes this an f-string. Inside an f-string, curly braces are magic: Python swaps {name} for the value of the variable name. This is the modern Python way to mix variables into text. Inside the function, name holds whatever the caller passed in.
greet("Ada")
Calls greet and hands in "Ada", so inside the function, name is "Ada".
greet("Grace")
The same function, different input, different greeting. One function, endless greetings!