Functions & Debugging
A function is a reusable block of code that does one job, and debugging is the process of finding and fixing mistakes in that code.
Reading is good — doing is better. Practice Functions & debugging as an interactive lesson.
Try the lessonDefinition
In coding, a function is like a mini-program inside your program. You give it a name, write the steps once, and then call it by name whenever you need it. Debugging means acting like a detective to find out why your code is not working the way you expected and then fixing the problem.
Remember the rule
Define it once, call it many times — and when something breaks, READ the error message first!
Key words
- Function
- A named block of code that does a specific job and can be used over and over.
- Define (a function)
- Writing the instructions for a function — you are telling the computer what the function should do.
- Call (a function)
- Using the function's name in your code so the computer runs those instructions.
- Parameter
- A piece of information you hand to a function so it can do its job — like giving a recipe the number of servings.
- Return value
- The answer or result a function gives back to you after it finishes running.
- Bug
- A mistake in the code that makes the program behave in the wrong way.
- Debugging
- The step-by-step process of finding a bug and fixing it so the program works correctly.
- Syntax error
- A typo or grammar mistake in your code that the computer cannot understand, like a missing parenthesis.
Worked examples
You need to greet three different students by name. Without a function you write: print('Hello Alice'), print('Hello Bob'), print('Hello Carol'). With a function, how do you do it?
→ Define: def greet(name): print('Hello ' + name). Then call it three times: greet('Alice'), greet('Bob'), greet('Carol'). · The function uses the parameter 'name' so it can work for any student, not just one.
A function is supposed to add two numbers and give back the answer. def add(a, b): return a + b. What does add(3, 5) return?
→ 8, because the function adds 3 + 5 and returns the result. · The return value can be stored in a variable: total = add(3, 5) gives total the value 8.
A student writes: def double(n) print(n * 2). When they run it, they get an error. What is the bug?
→ There is a missing colon after def double(n). The correct line is: def double(n): · This is a syntax error — the computer cannot read the code because the punctuation is wrong.
A student writes a function to check if a number is even: def is_even(n): if n % 2 = 0: return True. It gives an error. Why?
→ The equal sign for comparison should be two equal signs (==), not one (=). One equal sign means 'assign a value'; two means 'compare'. · This is one of the most common bugs beginners make — mixing up = and ==.
A student calls greet() but forgets to pass a name: greet(). The function definition is def greet(name): print('Hello ' + name). What happens?
→ The program gives an error saying the function is missing a required argument. You must call it with a name, like greet('Maria'). · If a function expects a parameter, you must always provide it when you call the function.
A student wants a function that returns double a number, but they write: def double(n): n * 2. They call double(4) and get nothing back. What is wrong?
→ The function is missing the word 'return'. It should be: def double(n): return n * 2. Without return, the result is calculated but never sent back. · A function does the math but throws away the answer unless you use return.
Common mistakes
- Forgetting the colon (:) at the end of the def line, which always causes a syntax error.
- Using a single equal sign (=) instead of double equal signs (==) when trying to compare two values inside a function.
- Defining a function but never calling it — the code exists but nothing happens because the student never used the function's name.
- Forgetting the return statement, so the function does the work but never gives back an answer.
- Not reading the error message — most error messages tell you exactly which line has the problem, but students skip past them.
FAQs
Why do we even use functions? Can't we just write all the code in a row?
You can, but if you need to do the same task ten times, you would have to write the same code ten times. A function lets you write it once and call it whenever you need it, which saves time and makes it much easier to fix mistakes.
What is the difference between defining a function and calling a function?
Defining is writing the recipe — you are telling the computer what steps to follow. Calling is actually cooking the recipe — you are telling the computer to run those steps right now.
How do I start debugging when my code does not work?
Start by reading the error message — it usually names the line number where the problem is. Then check that line for typos, missing colons, wrong symbols, or a missing return. If there is no error message but the answer is wrong, add a print statement to check what value the function is actually producing.
Do all functions need parameters?
No. A function can have no parameters if it always does the exact same thing. For example, def say_hello(): print('Hello!') works fine with no parameter.
Do all functions need a return statement?
No. If the function just does something (like printing a message), it does not need to return a value. But if you want the function to calculate something and give you the result to use later, you need return.
What does it mean when the error says 'name is not defined'?
It usually means you either misspelled the function or variable name, or you are trying to use a function before you defined it. Check your spelling and make sure the def block appears in your code before the line that calls it.
Want the whole picture for your child?
Every K–6 subject, an AI tutor that teaches step by step, unlimited practice, and a reward world.
Start a 3-day free trial