runtric-logo
Python Basics 3 – Functions and ReuseDefining functions with def (2/6)
Chapter 2
Python Basics 3 – Functions and Reuse

Defining functions with def

15 minutes read

Beginner friendly

Includes practice

Why bother with functions?

Without functions you’ll quickly find yourself repeating blocks of code again and again.
Functions help you:

  • Reuse common logic instead of duplicating code
  • Organize problems into manageable chunks
  • Name logic clearly so your code explains itself

Everyday analogy

  • Think about how you tie your shoes or make a coffee
  • You don’t think through every step each time—you follow a known sequence
  • Functions let you name those steps and reuse them in your code

Defining your first function

Functions begin with the def keyword. You name the function, add parentheses (), and end with a colon. Then indent the body.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

When run, this prints Hello!

Important parts:

  • def starts the function definition
  • say_hello is the function name
  • () — no input parameters for now
  • : followed by indented lines (the function’s body)

Try editing the message inside print() to personalize it.

Adding parameters (inputs)

To make functions flexible, we use parameters—placeholders for input values.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Each time you call greet("Ada"), the string "Ada" is passed to the parameter name.
The function then prints "Hi Ada 😊" and so on.

Returning a value with return

The print() function only shows something on-screen.
Want your function to produce output you can reuse? Use return.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Once Python hits return, it exits the function immediately and hands back the result.

Mini Practice – Square That Number!

Practice Challenge

Interactive Code Editor
📝 Your Task:

Write a function called squarethat takes one number and returns that number multiplied by itself. Then call square(4)and store the result in result.Finally, print the result to verify.

python

Define a function that returns n * n, then call square(4) and store the result.

Optional Parameters (Default Values)

Functions can have optional defaults. You can assign a value to certain parameters so they can be skipped when you call the function.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

⚠️ Defaults must come after any required parameters.

Practice – Average of Three Numbers

Practice Challenge

Interactive Code Editor
📝 Your Task:

Write a function called avg_of_three(n1,n2,n3)that takes three numbers and returns their average. Then call the function with values 5,10,and 15,store the result in avg,and print it. The result should be 10.0.

python

Define avg_of_three(n1,n2,n3) that returns (n1 + n2 + n3) / 3 and store the result.

Quick Practice

2 minutes

Can you spot common mistakes when using functions in Python?

What happens if you write say_hello instead of say_hello()?

What happens to code placed after a return in a function?

Quick Summary

  • Define functions using def name(): followed by indented code

  • Functions can accept parameters and return results using return

  • Use parentheses to call a function; say_hi() vs say_hi

Lesson Complete!
100%
Progress
What You've Learned:
  • You learned how to define and call simple functions using def
  • You practiced using parameters and return statements for flexible logic
  • You explored default values and wrote your first multi-argument function
  • You identified common pitfalls like forgetting parentheses or misusing return

Sign in to track your progress

Chapter Progress

0 secs

AI Tutor

Powered by Runtric

Solve first — tutor's here to guide.

Runtric might be wrong. Think critically.