We're making some improvements. Some features may be temporarily unavailable.
beginner20 min

Functions

Defining and calling functions, parameters, and return values

What are Functions?

A function is a reusable block of code that performs a specific task. You define a function once and call it many times.

Your First Function

Output
Run your code to see output here

Functions with Parameters

Parameters let you pass data into a function:

Functions with Parameters

Output
Run your code to see output here

Return Values

Functions can return values using the return keyword:

Return Values

Output
Run your code to see output here

Multiple Parameters & Default Values

Default Parameters

Output
Run your code to see output here

Variable Scope

Variables defined inside a function are local — they don't exist outside:

Variable Scope

Output
Run your code to see output here

Check Your Understanding

What keyword is used to define a function in Python?

Check Your Understanding

What does this function return: `def greet(): print('Hi')`?

Exercise

Write a function called `is_even` that takes a number and returns `True` if it's even, `False` if it's odd. Then test it with the number 10.

Python will load on first run

Questions & Discussion

Sign in to ask a question or join the discussion.

Loading comments...