Coding Python
Home›Python Coding Games›Function Forge

Function Forge — Functions

Six machines to build from a specification — and every call in the table is run against yours.

6 levels · Beginner → Intermediate
Level 1

Checks
    Your PythonPython 3
    1

    How to play

    Each level gives you a specification as a table of calls and the value each one should produce. Write the function, press Run, and every row is ticked or crossed individually — so a function that works for the obvious input and fails on zero or a negative number tells you exactly which case broke.

    What each level teaches

    1. Your first machine — def and return, and that returning is not printing.
    2. Two inputs — several parameters, matched by position.
    3. A sensible default — a default value makes a parameter optional.
    4. Name the argument — keyword arguments, which let a caller skip a middle parameter.
    5. The missing return — the single most common beginner function bug.
    6. Whose variable is it? — scope, and why passing in and returning out beats global.

    return versus print

    These look interchangeable while you are testing in a console and are nothing alike:

    • print(x) puts characters on the screen and gives back None. Nothing else in your program can use it.
    • return x hands the value to whoever called the function, so it can be stored, compared or passed on.

    Level 5 exists entirely for this. The function there prints the right answer, which makes it look correct, and returns None, which makes it useless — t = total([1, 2, 3]) leaves t as None. If a result ever "disappears" between a function and its caller, check for a missing return first.

    One default to never write

    A default is evaluated once, when the function is defined — not on each call. So this is a trap:

    def add(item, basket=[]):

    Every call that relies on the default shares the same list, and your basket slowly fills with other people's shopping. The fix is basket=None and if basket is None: basket = [] inside. The same bug appears in Bug Squash, and in our guide to the 15 common Python errors.

    Scope in one sentence

    Assigning to a name inside a function creates a local variable, even if a variable with that name exists outside. That is why level 6 bans global: the habit worth building is to take values in as parameters and send results back with return, which keeps a function testable and keeps its effects visible at the call site. The written version is in the Python functions tutorial.

    Practise functions on your phone — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
    Get it free

    Frequently asked questions

    What is the difference between return and print in Python?

    print displays a value and gives back None. return hands the value to the caller so it can be stored or used. A function that prints instead of returning looks correct in testing and is unusable in code.

    Why does my function return None?

    Because no return statement ran. Either there is no return at all, or every return is inside an if that was false. Python returns None by default when a function ends without returning.

    What is a default argument in Python?

    A value given in the signature, as in def greet(name, greeting="Hello"), making that parameter optional. Never use a mutable default such as [] or {} — it is created once and shared by every call.

    What are keyword arguments?

    Arguments passed by name, as in price(2, discount=20). They let you skip optional parameters that come earlier, and they make a call easier to read when several arguments are numbers.

    Why should I avoid the global keyword?

    Because it hides where a value changes. Taking the value in as a parameter and returning the new one makes the function testable on its own and makes the change visible where it is called.

    Keep going

    ← All Python coding games  Read the tutorial →

    Take the games with you

    Coding Python packs a real Python 3 compiler, 30 example programs, 112 quiz questions and 12 self-checking challenges into one free Android app.

    Get it on Google Play
    Coding Python app icon