Codingal > Coding for kids > Blogs > How to use functions on Scratch

How to use functions on Scratch

Aleena Martin on August 23, 2025

Introduction 

Have you ever trained a pet? Giving them a certain set of commands and doing what they’re told? Now, imagine doing the same with your computer! That’s the magic of functions in Python. And Python for kids will be more fun with functions! Functions let you give instructions and make it do things automatically. It’s like teaching your computer its own set of skills. With functions, you can reuse your code, making your life as a programmer a whole lot easier. 

In this article, you will understand clearly how to master this feature of Python.

 

What is a Function? 

A function is like a little machine or a recipe. You give it any input, and then it does something according to it(like calculating or printing) and gives you an output. Functions help you organize your code, making it easier to read and use again and again.

Step 1: Creating a Function

In Python, you can create your own functions using the def keyword. After def, you give your function a name, and then you write the instructions (called the function body) inside the function.

Let’s see how to create a simple function. Now for this, we take 2 words: “Hello, World!” 

def say_hello():
    print(“Hello, World!”)

 

In this example:

  • def means “define a function.”
  • say_hello is the name of the function.
  • print(“Hello, World!”) is what the function does when you run it.

Step 2: Calling a Function

After you create a function, you need to call it (or run it) to see what it does. You do this by writing the function’s name followed by parentheses.

say_hello()  # This prints: Hello, World!

 

Step 3: Adding Parameters

Sometimes you want to give your function some information (input) to work with. You can do this by adding “parameters” to your function. Parameters are like special boxes inside the function that you fill with information when you call it.

For example, let’s make a function that says hello to a specific person:

def greet(name):
    print(“Hello, “ + name + “!”)

 

In this case, the function ‘greet’ has a parameter called ‘name’. When you call the function, you can put someone’s name inside the parentheses:

greet(“Alice”)  # This prints: Hello, Alice!
greet(“Bob”)    # This prints: Hello, Bob!

 

Step 4: Returning a Value

Sometimes, functions need to give you back a result. To do that, you use the return statement. Here’s an example of a function that adds two numbers and returns the result:

def add_numbers(num1, num2):
    result = num1 + num2
    return result

 

Now, when you call the function, it will give you the sum of the two numbers:

sum = add_numbers(5, 7)
print(sum# This prints: 12

 

Step 5: Why Use Functions?

Functions make your code shorter and easier to manage. Instead of writing the same block of code over and over again, you can put it in a function and call it whenever you need it.

For example, imagine you’re creating a game, and you need to check the player’s score often. Instead of writing the same code every time, you can use a function:

def check_score(score):
    if score >= 10:
        print(“You win!”)
    else:
        print(“Keep playing!”)

 

Then you can use this function whenever you need to check the score:

check_score(12)  # Prints: You win!
check_score(5)   # Prints: Keep playing!

 

Step 6: Functions with Multiple Parameters

You can also create functions that take multiple pieces of information. For example, here’s a function that calculates the area of a rectangle:

def calculate_area(length, width):
    return length * width

 

Now, you can call the function with different lengths and widths:

area = calculate_area(5, 3)
print(area)  # Prints: 15

 

Conclusion

Wasn’t that quick and easy-peasy? In Python, functions are quite essential that allows you to organize your code and reuse it easily. You can create simple functions that print messages or more complex ones that calculate results and return values. By learning how to use functions,you can make your programs more powerful, flexible, and easy to understand!

Jump right on to code camps for kids, where your kids learn to adapt to the digital world of coding through simple and fun learning experiences! Let them join their first trial class today!

Encourage learning everywhere you go and have fun with your coding journey! 

 

Share with your friends

Try a free class