Python · live now

User-defined functions

A function is a named block of code you call with inputs. Its signature names the arguments, defaults let a caller leave some out, and a return value hands something back. Then scope, docstrings, lambdas, and recursion - a function that calls itself.

No account needed to read a lesson · 30-day money-back guarantee

See the full learning path

10

lessons

92

practice questions

72

prerequisite lessons

See it taught

2 of the 10 lessons here are free to preview - the whole explanation and worked example, with no account and no email.

The editor you get

A still of the editor, not a live one

Python Editor

1 2 3 4
def area(w, h): return w * h print(area(3, 4))

Editor Output

1

Output:

12

You write and run Python in the browser - nothing to install, no setup. Questions are marked on the values your code produces. The snippet here is an example of the kind of code you write.

The 10 lessons

The 10 lessons in this unit. Expand any one to see what it covers. Each unlocks when its own prerequisites are passed.

01

Functions

12 questions

Understand how functions allow code to be organised into reusable blocks, how to call both built-in and user-defined functions with arguments, and how to use or store the values functions return.

Covers

Functions overview

Calling a function

Function return values

02

Function signatures and arguments

12 questions

Learn how to read and interpret a function's signature in Python, distinguish between required and optional parameters, and use both positional and keyword arguments to call functions correctly.

Covers

Function signatures

Positional arguments

Keyword arguments

03

User-defined functions

Preview

8 questions

Learn how to define and use functions in Python by specifying parameters, implementing logic in the function body, and returning values to make code reusable and robust against invalid input.

Covers

Syntax and definition

Writing basic functions

Preview this lesson - no account needed

04

Function return types

12 questions

Understand how Python functions can return no value (resulting in None), use multiple return statements to exit at different points with specific values, and return multiple values at once as tuples, including best practices for consistency and clarity.

Covers

Functions with no return values

Functions with multiple return statements

Functions returning multiple values

05

Functions with default arguments

8 questions

Learn how to define Python functions with default argument values so that some parameters can be omitted during a function call, and understand the rule requiring all non-default parameters to precede those with default values to prevent ambiguity.

Covers

Functions with default arguments

Ordering of positional and default arguments

06

Function scope

8 questions

Understand how local variables exist only within a function's scope, how global variables are accessible throughout a program, and how to correctly access or modify global variables from within functions using the global keyword.

Covers

Local variables

Global variables

07

Function documentation

7 questions

Learn how to document Python functions using docstrings, describe their parameters and return values clearly, and access this documentation programmatically or with the built-in help() function.

Covers

Function docstrings

Function help

08

Lambda functions in Python

9 questions

Learn how to define anonymous, single-expression functions using the lambda keyword in Python and apply them directly as inline functions.

Covers

What is a lambda function?

Lambda and regular functions

Passing lambdas to other functions

09

Introduction to recursion

Preview

12 questions

Learn how recursion allows a function to solve problems by calling itself with simpler inputs, how base cases prevent infinite loops, and how recursive definitions can elegantly capture problems like factorial and Fibonacci sequences.

Covers

What is recursion

Base case and recursive case

Recursion in practice

Preview this lesson - no account needed

10

Activity: simple voting system

4 questions

Learn how to implement a simple voting system in Python by writing functions to register votes, count them, determine the winner, and summarise the election results.

Covers

Register a vote

Get the vote count

Determine the winner

Summarise the results

Where this unit sits in the curriculum

This unit is part of our Python Done Right learning path, which contains 100 lessons. Every one of them is drawn below.

The diagnostic test lets you skip any lesson you already know, including the ones in this unit.

Every dot is a lesson and every line a prerequisite. The 10 red dots are the lessons in User-defined functions; the 72 blue dots feeding into them are the lessons User-defined functions depends on; the 18 pale dots are the rest of the learning path, which come after User-defined functions or alongside it.User-defined functionsFirst lessons on the left100 lessons

Hover any dot to name its lesson.

10 lessons in this unit

72 prerequisite lessons across the unit, counting every step back to the start

18 other lessons in the learning path - after this unit, or alongside it

Every dot is a lesson, every line a prerequisite. You can only start a lesson once you have mastered all of its prerequisites, so you are always building on solid foundations.

Start User-defined functions

One subscription covers every learning path, and you can test out of anything you already know.

One subscription covers every learning path · 30-day money-back guarantee

More in Functions: Built-in functions