Defining Your Own Functions
Create reusable blocks of code.
Defining Your Own Functions is a free Python For Kids lesson on CoddyKit — lesson 2 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
Defining Your Own Functions
Welcome to the Functions lesson! Today, we'll learn how to create your own functions in Python, allowing you to reuse and organize your code efficiently.
2
Why Define Your Own Functions?
Defining your own functions helps you:
- Reuse Code: Write code once and use it multiple times.
- Organize Code: Break down complex problems into smaller, manageable tasks.
- Improve Readability: Make your code cleaner and easier to understand.
Let's see how to create your own functions!
3
Creating a Function
You can create 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 greet function
def greet():
print("Hello, Python!") # Prints a greeting message
# Calls the greet 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
Using Default Parameters
You can assign default values to parameters. If no argument is provided for that parameter when calling the function, the default value is used.
Example:
# Defines a function with a default parameter
def greet(name="Python Learner"):
print("Hello, " + name + "!") # Prints a personalized greeting
# Calls the function without an argument
greet() # Output: Hello, Python Learner!
# Calls the function with an argument
greet("Alice") # Output: Hello, Alice!
7
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
8
Multiple Return Values
Functions can return multiple values by separating them with commas. These values can then be unpacked into separate variables.
Example:
# Defines a function that returns multiple values
def get_user_info(name, age):
return name, age # Returns name and age
# Calls the function and unpacks the returned values
user_name, user_age = get_user_info("Bob", 25)
print("Name:", user_name) # Output: Name: Bob
print("Age:", user_age) # Output: Age: 25
9
# Function that checks if a number is even
def is_even(number):
if number % 2 == 0:
return True # Returns True if number is even
else:
return False # Returns False if number is odd
# Calls the function with number 7
result = is_even(7)
print(result)
10
Great Job!
You've learned how to define your own 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!

Frequently asked questions
Is the “Defining Your Own Functions” lesson free?
Yes — the full text of “Defining Your Own 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 “Defining Your Own Functions”?
Create reusable blocks of code. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Defining Your Own 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
- What are Functions?
- Defining Your Own Functions
- Arguments and Return Values
- Lambda Functions