Question 5.24: Defining a Function in Python You want to avoid repeating th......

Defining a Function in Python

You want to avoid repeating the same code over and over in a program.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

Create a function that groups together lines of code, allowing it to be called from multiple places.

Creating and then calling function in Python is illustrated in the following example:

def count_to_10():
for i in range(1, 11):
print(i)
count_to_10()

In this example, we have defined a function using the def command that will print out the numbers between 1 and 10 whenever it is called:

count_to_10()

Discussion

The conventions for naming functions are the same as for variables in Recipe 5.5; that is, they should start with a lowercase letter, and if the name consists of more than one word, the words should be separated by underscores.

The example function is a little inflexible because it can only count to 10. If we wanted to make it more flexible—so it could count up to any number—then we can include the maximum number as a parameter to the function, as this example illustrates:

def count_to_n(n):
for i in range(1, n + 1):
print(i)
count_to_n(5)

The parameter name n is included inside the parentheses and then used inside the range command, but not before 1 is added to it.

Using a parameter for the number we want to count up to means that if we usually count to 10, but sometimes count to a different number, we will always have to specify the number. You can, however, specify a default value for a parameter, and hence have the best of both worlds, as shown in this example:

def count_to_n(n=10):
for i in range(1, n + 1):
print(i)
count_to_n()

This will now count to 10 unless a different number is specified when you call the function.

If your function needs more than one parameter, perhaps to count between two numbers, then the parameters are separated by commas:

def count(from_num=1, to_num=10):
for i in range(from_num, to_num + 1):
print(i)
count()
count(5)
count(5, 10)

All these examples are functions that do not return any value, they just do something. If you need a function to return a value, you need to use the return command.

The following function takes a string as an argument and adds the word please to the end of the string.

def make_polite(sentence):
return sentence + ” please”
print(make_polite(“Pass the cheese”))

When a function returns a value, you can assign the result to a variable, or as in this example, print out the result.

See Also

To return more than one value from a function, see Recipe 7.3.

Related Answered Questions

Question: 5.23

Verified Answer:

Use the Python break statement to exit either a wh...
Question: 5.22

Verified Answer:

Use the Python while statement. The while statemen...
Question: 5.21

Verified Answer:

Use the Python for command and iterate over a rang...
Question: 5.20

Verified Answer:

Use one of the logical operators: and, or, and not...
Question: 5.19

Verified Answer:

Use one of the comparison operators: <, >, &...
Question: 5.17

Verified Answer:

Use the upper or lower function as appropriate. Fo...
Question: 5.18

Verified Answer:

Use the Python if command. The following example w...
Question: 5.15

Verified Answer:

Use the Python [:] notation. For example, to cut o...
Question: 5.14

Verified Answer:

Use the find Python function. For example, to find...