Tutelage

Lessons › coding and computer science · taught by Leo

Python functions: reusable code basics

Learn how Python functions reduce repetition, use parameters and arguments, and return values with correct indentation.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

Why functions save repeated work

When you keep doing the same job in code, a function lets you pack that job into one reusable block.

That means less copy-paste, fewer mistakes, and easier changes later.

Think of it like making one sandwich recipe instead of writing the steps again and again.

Big idea: input → work → output

A function can take input, do some work, and give back output.

In Python, you usually give it a name, optional inputs, and a return value if you want something back.

The big idea is: call the name, send in values, get a result.

Predict a function call

If a function is defined with one parameter, what do you usually do when you call it?

  1. Pass one argument
  2. Write return first
  3. Use two colons
Show answer

Pass one argument: Yes. A parameter is a placeholder in the function definition, and an argument is the value you send in when you call it.

Write and call a function

Here is a tiny function that greets a person by name.

The indent matters because it tells Python what belongs inside the function.

Then we call it with a real value, like Ava, to make it run.

What belongs in the body?

In this function, what should be indented under def?

  1. The code that runs inside the function
  2. The function name only
  3. Nothing at all
Show answer

The code that runs inside the function: Correct. The indented lines are the function body, so Python knows what to repeat when the function is called.

Why indentation matters

Indentation is not just style in Python. It is part of the syntax.

That is how Python knows where the function starts and ends.

If the indent is wrong, the code may not run the way you expect.

Return gives back a value

Sometimes you do not want to print the answer. You want to return it.

Return hands a value back to the line that called the function.

Then you can store that result in a variable and use it later.

Print or return?

What does return do in a function?

  1. Sends a value back
  2. Starts the function
  3. Deletes the variable
Show answer

Sends a value back: Yes. Return gives a result back to the caller so the program can use it.

Calling with arguments

The order of arguments matters. The first value goes to the first parameter.

That is why area of 4 and 5 gives twenty here.

A function is like a machine: same steps, different inputs.

Your turn: choose the call

A function is defined as def double(x): return 2 * x. Which call gives 14?

  1. double(7)
  2. double(2, 7)
  3. double = 7
Show answer

double(7): Correct. One input of 7 makes 2 times 7, which is 14.

Watch out: names and values

A common mistake is mixing up parameter and argument.

The parameter is in the function definition. The argument is the value you pass in.

Also watch your parentheses and commas, because Python is strict about both.

Recap: the function checklist

Here is your checklist: define it, indent it, call it, and return a result when needed.

If you can read that flow, you can write basic Python functions.

Try making tiny functions first. Small code is easier to test and fix.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app