Kids Coding

Functions

A function is a named recipe. Write it once. Call it whenever you need it.

KidsIntermediateFunctions

A named recipe

A function is a chunk of code with a name. You write it once with def. You run it by calling the name with ().

Say hello

def hello():
    print("Hello")
    print("Welcome")

hello()
hello()

Name a function for what it does: say_hello, not thing. Future-you will thank present-you when you read the code next week.

Pass something in

The name in the brackets is a parameter: a box that only exists inside the function.

hello(name)

def hello(name):
    print("Hello, " + name)

hello("Sam")
hello("Riley")

Why bother

  • You do not copy-paste the same three lines.
  • If you fix the function, every call is fixed.
  • The rest of the program reads like a list of steps.

Real life

Making the same sandwich every day.

You do not rewrite the recipe. You follow it. def is the recipe. Calling it is lunchtime.

Sandwich recipe

def sandwich(filling):
    print("Bread")
    print(filling)
    print("Bread")
    print("---")

sandwich("cheese")
sandwich("jam")

Change one word. Run it. That is how this idea shows up outside the lesson.

Try it

Exercise 1: Write cheer(team) that prints Go and the team name. Call it twice.

Show solution
def cheer(team):
    print("Go " + team)

cheer("Foxes")
cheer("Owls")

FAQ: Functions

Common questions about this page.

What is StudyGrid Kids?

StudyGrid Kids is a free coding path for children: Basic, Intermediate, and Advanced. Lessons use Python in the browser. No account and no install.

Should I run functions examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn functions in this Kids Coding Python lesson (Functions).

Which kids level should we start with?

Start at Basic if the child has never written code. Choose Intermediate if they can print, use variables, and write a simple if. Choose Advanced if they already use loops and lists.

Do kids need an account or an app?

No. Kids pages are public. Code runs in the Try Python editor at /try. We do not ask children for a name or an email.

What language do kids learn?

Python, with a first HTML page in Intermediate and Advanced. After Advanced, the main Python tutorial is the next step.

Is the kids coding track free?

Yes. The Kids home, the three levels, and the Python editor on StudyGrid (studygrid.in) are free.