0Pricing
Python For Kids · Lesson

What are Functions?

Understand the purpose and structure of functions.

What are Functions? is a free Python For Kids lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python For Kids learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Functions

Welcome to the Functions lesson! Today, we'll learn about functions in Python, which help you reuse and organize your code efficiently.

What are Functions? — illustration 1

2

What are Functions?

Functions are blocks of code that perform specific tasks. They allow you to write code once and reuse it multiple times, making your programs more organized and efficient.

  • Reusability: Write code once and use it wherever needed.
  • Modularity: Break down complex problems into smaller, manageable tasks.
  • Readability: Make your code cleaner and easier to understand.

3

Defining a Function

You can define a function using the def keyword, followed by the function name and parentheses. Inside the parentheses, you can specify parameters.

Syntax:

def function_name(parameters):
    # Code to execute
    print("Hello, Python!")

4

Calling a Function

Once you've defined a function, you can call it by using its name followed by parentheses. If the function has parameters, you can pass arguments inside the parentheses.

Example:

# Defines the function
def greet():
    print("Hello, Python!")  # Prints a greeting message

# Calls the function
greet()

5

Function Parameters

Parameters allow you to pass data into functions, making them more flexible and powerful. You can define functions that accept one or more parameters.

Example:

# Defines a function with two parameters
def add(a, b):
    total = a + b  # Adds the two numbers
    print("Total:", total)  # Prints the sum

# Calls the function with arguments
add(5, 3)

6

Returning Values from Functions

Functions can return values using the return statement. This allows you to get the result of a function and use it elsewhere in your code.

Example:

# Defines a function that returns the sum of two numbers
def add(a, b):
    total = a + b  # Adds the two numbers
    return total  # Returns the sum

# Calls the function and stores the returned value
result = add(10, 7)
print("Total:", result)  # Prints the sum

7

Example Function

Let's create a function that greets the user by name.

Example:

# Defines a function that takes a name as a parameter and returns a greeting message
def greet(name):
    message = "Hello, " + name + "!"  # Creates a greeting message
    return message  # Returns the message

# Calls the function and stores the returned message
greeting = greet("Alice")
print(greeting)  # Prints the greeting

8

Advantages of Using Functions

Using functions offers several benefits:

  • Reusability: Avoid writing the same code multiple times.
  • Organization: Keep your code structured and manageable.
  • Maintainability: Make it easier to update and debug your code.

9

# Function that checks if a person is an adult
def check_adult(age):
    if age >= 18:
        return "You are an adult."
    else:
        return "You are not an adult."

# Calls the function with age 16
message = check_adult(16)
print(message)

10

Great Job!

You've learned how to use functions to organize and reuse your code in Python. Functions make your programs more efficient, readable, and easier to manage. Keep practicing by creating your own functions and using them in different scenarios!

What are Functions? — illustration 10

Frequently asked questions

Is the “What are Functions?” lesson free?

Yes — the full text of “What are Functions?” is free to read here on the web, and the Python For Kids course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python For Kids course, upgrade to CoddyKit PRO.

What will I learn in “What are Functions?”?

Understand the purpose and structure of functions. You practise Python For Kids with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python For Kids?

No prior experience is required. Python For Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “What are Functions?” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python For Kids lesson?

Yes. Every Python For Kids lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What are Functions?
  2. Defining Your Own Functions
  3. Arguments and Return Values
  4. Lambda Functions
← Back to Python For Kids