Skip to main content

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

Prompt
Write a Python function called my_function that prints "Hello from a function". Call the function once.
Output: complete Python program

Example

Prompt
Write a Python function called my_function that prints "Hello from a function". Call the function three times.
Output: complete Python program

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 myFunction and myfunction are two separate names

Pick a descriptive name that says what the function does.

main.py
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:

Prompt
Convert three temperatures (77, 95, 50) from Fahrenheit to Celsius without using functions. Print each result.
Output: complete Python program

Example

Reusable code, with a function:

Prompt
Create a function fahrenheit_to_celsius that converts Fahrenheit to Celsius. Use it to convert and print 77, 95, and 50.
Output: complete Python program

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:

Prompt
Create a function get_greeting that returns "Hello from a function". Store the return value in a variable and print it.
Output: complete Python program

Example

Printing the returned value straight away:

Prompt
Create a function get_greeting that returns "Hello from a function". Print the return value directly without storing it.
Output: complete Python program

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

Prompt
Create an empty function my_function using the pass statement.
Output: complete Python program