Nex2School

Parent guide · 6 min read

Python functions for kids: naming your own commands

Sooner or later a child who is learning Python writes the same few lines three times and wonders if there is a shortcut. There is, and it is called a function: a block of code with a name, so that one short command runs the whole block. Functions are where a program stops being a list of instructions and starts being something built from parts. They are also where several new ideas arrive at once: a keyword, a name, brackets, an indented block and a result that goes somewhere. Taking those one at a time makes the whole thing much easier.

Try this tonight

5 things you can do at home

  1. Spot repeated code first

    Have your child write a short program that prints a greeting for three different names using three separate print lines. Then ask what they would change if the greeting needed to say "Welcome" instead of "Hello". Feeling that pain in three places is what makes a function make sense.

  2. Define it, then call it

    Write def greet(name): and indent one line under it. Nothing happens until greet("Maya") is written on its own line later. Many beginners define a function and then wonder why the program prints nothing, so say "define, then call" out loud.

  3. Say what goes in and what comes out

    Before writing any function, have your child complete the sentence: "It takes ___ and gives back ___." For a function that squares a number, that is "takes a number and gives back that number times itself". The sentence becomes the parameter list and the return line.

  4. Test it with a few calls

    Run the function with two or three inputs whose answers the child already knows, such as square(3) should be 9 and square(0) should be 0. Checking against known answers is the habit that carries into every later program.

  5. Read the error messages together

    The most common messages here are a NameError, from calling a function before it is defined, and an IndentationError, from forgetting to indent the body. Reading the message and the line number together turns an error into a clue.

  1. Parameters and return: how data goes in and out

    In def square(n): return n * n, the name n is a parameter, a label for whatever value the function receives. When the code says square(4), the value 4 is the argument, and inside the function n is 4. The line return n * n sends the answer, 16, back to the place where square(4) was written, so result = square(4) stores 16 in result.

    A function can take more than one parameter, separated by commas. def area(width, height): return width * height gives area(3, 4) = 12. Because the result is returned, it can be used inside larger expressions: area(5, 2) + area(1, 1) is 10 + 1 = 11. A child who can trace that by hand understands how a function fits into a calculation.

  2. The print versus return mix-up

    The most common confusion is between showing a value and giving it back. Suppose a child writes def add(a, b): print(a + b) and then writes total = add(2, 3). The program prints 5, but total holds a special empty value called None, because nothing was returned. Printing a value shows it on the screen. Returning a value hands it to the rest of the program.

    A quick way to tell them apart is to ask: "Do I want to see this, or use this later?" If the answer is use later, the function needs return. If it only needs to display a message, such as a greeting, print is fine and no return is needed. Trying both versions of add and printing total shows the difference far better than any explanation.

  3. Worked example: a function that is called twice

    Take def double(x): return x * 2. The call double(3) gives 6. Because the result can be passed straight into another call, double(double(3)) first works out the inner call, 6, and then double(6) gives 12. Tracing this with a pencil, one call at a time, is a good test of whether a child understands that a function call is replaced by its returned value.

    The same idea explains why the order of the code matters. Python reads the file from the top, so a function must be defined before the line that calls it. If a program calls greet("Maya") on line 1 and defines greet on line 5, Python stops with a NameError, because at line 1 it has not yet met the name greet.

Signs it’s time for outside help

When home help has done what it can

  • Defines a function but never calls it, then wonders why nothing happens.
  • Uses print inside a function when the result is needed later, so the variable ends up as None.
  • Gets NameError or IndentationError messages and re-types the code rather than reading the message.
  • Copies the same lines into several places instead of reaching for a function.
  • Cannot say what the parameter is for, only that "you put it in the brackets".
  • Understands functions in a worked example but freezes when asked to write one from a blank page.

Questions parents ask

FAQ

What age can a child learn Python functions?

Many children are ready between ages 9 and 12, once they are comfortable with variables and loops. Younger children can use ready-made functions such as print long before writing their own, and an older beginner often picks functions up quickly.

What is the difference between a parameter and an argument?

A parameter is the name in the definition, such as n in def square(n). An argument is the actual value in the call, such as 4 in square(4). In everyday talk the two words are often swapped, and it rarely causes harm.

Does every function need return?

No. A function that only displays something, such as a greeting, can use print and have no return. A function whose result will be used later, such as a calculation, needs return. Without it, Python gives back None.

Why use a function if I could just copy the code?

Copying works until something needs to change, and then every copy has to be found and edited. A function keeps the code in one place, so a change is made once, and it also gives the code a name that says what it does.

Classes that fit

Live, 50–60 minutes, four learners at most or 1-on-1.

Not sure which is right for your child?

Tell us their age and what they have tried so far, and we will suggest where to start before you book anything.

Keep reading