4.1 Parameters

Getting to Know Functions

Parameters

Functions can also take parameters, which are values that we pass into the function when we call it. Parameters allow us to make our functions more flexible and reusable. We define parameters by adding them inside the parentheses when we define the function. We have used these parameters (or arguments when we call the function) in previous chapters, such as when we used print() to print a string.

When we define a parameter, we can do so by adding a variable name inside the parentheses. That variable name then becomes a stand in for whatever value we pass in when we call the function. We can then use that variable inside the function to perform operations. For instance, we can create a function that takes a name as a parameter and prints a greeting.

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")
Hello, Alice!

When we employ the function greet() and pass in the string “Alice”, the function prints “Hello, Alice!”. The variable name inside the function is replaced with the value we passed in when we called the function. We can also employ the argument in greet() by the name of the parameter, like so:

greet(name="Alice")
Hello, Alice!

Using the name of the parameter is called a keyword argument. This is useful when we have multiple parameters and want to specify which one we are passing in. It also makes the code more readable, as it indicates what value is being passed to which parameter. Otherwise, we would have to pass the values in the order they are defined in the function. Keyword arguments can be used in any order, as long as we specify the parameter name.

Default Parameters

We can also set default values for parameters. This means that if we do not pass a value when we call the function, it will use the default value instead. We can set a default value by assigning it in the function definition.

def greet(name="World"):
    print(f"Hello, {name}!")

greet("Alice")
greet()
Hello, Alice!
Hello, World!

Using default parameters can be particularly valuable when we have a number of parameters for edge functionality (rare but still useful functionality) that we do not want to require every time we call the function. In the example above, if we do not pass a name when we call greet(), it will use “World” as the default value. This allows us to call the function without any arguments and still get a meaningful output. But in a more complex function with multiple parameters, we can set defaults for some parameters while requiring others. Consider the following example:

def greet(name="World", greeting="Hello"):
    print(f"{greeting}, {name}!")

greet("Alice", "Hi")
greet("Bob")
greet()
Hi, Alice!
Hello, Bob!
Hello, World!

In this example, we have two parameters: name and greeting. The name parameter has a default value of “World”, while the greeting parameter has a default value of “Hello”. When we call greet("Alice", "Hi"), it prints “Hi, Alice!”. When we call greet("Bob"), it uses the default greeting and prints “Hello, Bob!”. Finally, when we call greet(), it uses both default values and prints “Hello, World!”.