4.1. Functions and ITS ARGUMENTS
Here's a simple function named say_hi() that says hi to user. This example shows the simplest structure of a function.
- Line ❶ uses the syntax def to define a function. This is the function definition. If applicable, you can assign something inside the parentheses. In this simple case, say_hi() no needs extra information.
- Starting at line ❷, just like for and if commands, any indented lines that follow def say_hi(): make up the body of the function. the line print("Hi!") is the only line in the body of this function, so say_hi() only do: print("Hi!").
- In line ❸, we call the function say_hi().
Passing Information to a Function
By modifying the function say_hi() a bit, we can also greet the user by name. To do this, you enter name in the () of def greet_user(). When you call say_hi(), you can pass it a name, such as Python, inside the ().
In case of function, you also need to remember these two terms:
name in this code is a parameter
Python in this code is an argument
Sometimes we need a function to have multiple parameters. We will see several way of doing so here.
Positional Arguments
The simplest way to have multiple parameters is based on the order of the arguments provided. This is called positional arguments.
See the following example about pets. The function tells us what kind of animal each pet is and the pet's name:
Order matters in positional arguments, so if you put pet name first then animal type, you will get something else.
Keyword Arguments
To avoid something like My nekoko's name is Cat, we can use keyword arguments. With this, you don't need to worry about the order. See the example on how to apply the keyword:
Default Values
When writing a function, you can define a default value for each parameter. Fo example, if you notice that most of the calls to pet_info() refers to dogs, you can set the default for animal_type to dog. Now, anyone calling pet_info() don't have to write dog.
To describe an animal other than a dog, you could use a function call like this:
Avoiding Argument Errors
Unmatched arguments can occur and produce error. Try the example to see what happens if we try to call pet_info() with no arguments.
In the error, Python tells the names of the arguments we need to provide. By reading the error message, we can see that we need one positional argument for pet_name.
Exercise 4.1
Message
Write a function called display_message() that prints one sentence saying:
Welcome to our book shop!
Call the function, and make sure the message displays correctly.Selling Attire
Write a function called selling_attire() that accepts one parameter, attire_name. The function should print a message, such as
Our shop sells {attire_name}.
Call the function, make sure to include one attire type (t-shirt, suit, etc.) as an argument in the function call.T-Shirt
Write a function called make_shirt() that accepts two arguments:
1. a size of the shirt
2. a color of the shirt
The function should print a sentence:
We sell a t-shirt of size {size titlecase}.
The available color is {color smallcase}.
Call the function once using positional arguments to make a red large t-shirt.Medium Suit
Make a new function similar to no.3 called make_suit() function so that suit are medium by default. It should print:
We sell a suit of size {size titlecase}.
The available color is {color smallcase}.
Then, make a medium suit of color
black, navy blue, and dark brown
and a suit with the default size and color black.Pants size
Write a function called make_pants() that is similar to no.3. The available pants color and size are
black: "5, 7, 9, 11"
dark brown: "7, 9, 11"
navy blue: "5, 9, 11"
Make a dictionary with those informations then print using for loop as follow:
We sell a suit pants of size {sizes}.
The available color is {color}.