Python Functions
A function is a named block of code that stays idle until you call it.
It can hand data back as a result.
It also saves you from repeating the same code.
Creating a Function
Start a function with the def keyword, then give it a name and a pair of parentheses.
The next section defines a function called my_function this way. Each time it is called, it prints "Hello from a function".
Indent the code that belongs to the function, because indentation is how Python marks a code block.
Calling a Function
Calling a function takes nothing more than its name and a pair of parentheses.
The same function can be called as many times as you like.
Example
my_function that prints "Hello from a function".
Call the function once.
Output: complete Python program
def my_function():
print("Hello from a function")
my_function()
Example
my_function that prints "Hello from a function".
Call the function three times.
Output: complete Python program
def my_function():
print("Hello from a function")
my_function()
my_function()
my_function()
Function Names
Naming a function works the same way as naming a variable in Python:
- The first character has to be a letter or an underscore
- Only letters, numbers and underscores are allowed
- Upper and lower case count as different, so
myFunctionandmyfunctionare two separate names
Pick a descriptive name that says what the function does.
calculate_sum()
_private_function()
myFunction2()
Why Use Functions?
Suppose your program has to turn Fahrenheit temperatures into Celsius in several places. With no functions, the same calculation gets written out again and again.
A function lets you write it once and then use it wherever you need it.
Example
Repetitive code, without a function:
Output: complete Python program
temp1 = 77
celsius1 = (temp1 - 32) * 5 / 9
print(celsius1)
temp2 = 95
celsius2 = (temp2 - 32) * 5 / 9
print(celsius2)
temp3 = 50
celsius3 = (temp3 - 32) * 5 / 9
print(celsius3)
Example
Reusable code, with a function:
fahrenheit_to_celsius that converts Fahrenheit to Celsius.
Use it to convert and print 77, 95, and 50.
Output: complete Python program
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5 / 9
print(fahrenheit_to_celsius(77))
print(fahrenheit_to_celsius(95))
print(fahrenheit_to_celsius(50))
Return Values
The return statement lets a function pass data back to its caller.
As soon as it runs, the function stops and the result goes back.
That value can be stored in a variable or used on the spot.
With no return statement at all, a function gives back None.
Example
Storing a returned value in a variable:
get_greeting that returns "Hello from a function".
Store the return value in a variable and print it.
Output: complete Python program
def get_greeting():
return "Hello from a function"
message = get_greeting()
print(message)
Example
Printing the returned value straight away:
get_greeting that returns "Hello from a function".
Print the return value directly without storing it.
Output: complete Python program
def get_greeting():
return "Hello from a function"
print(get_greeting())
The pass Statement
A function definition must have a body. For a placeholder that has no code yet, write the pass statement inside it.
Developers often reach for pass to lay out the structure of a program first and add the details later.
Example
my_function using the pass statement.
Output: complete Python program
def my_function():
pass