0Pricing
Python For Kids · Lesson

Arguments and Return Values

Learn to pass data into functions and get results.

Arguments and Return Values is a free Python For Kids lesson on CoddyKit — lesson 3 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

Arguments and Return Values

Welcome back to the Functions lesson! Today, we'll learn how to pass data into functions using arguments and get results back using return values.

Arguments and Return Values — illustration 1

2

What are Function Arguments?

Arguments are values you pass into a function to provide it with the data it needs to perform its task. They make functions more flexible and reusable.

  • Parameters: Variables listed inside the function's parentheses.
  • Arguments: Values passed to the function when it is called.

Let's see how arguments work in Python functions.

3

Using Arguments in Functions

When you define a function, you can specify parameters that act as placeholders for the arguments you'll pass when calling the function.

Example:

# Defines a function that greets a user by name
def greet(name):
    greeting = "Hello, " + name + "!"  # Creates a greeting message
    print(greeting)  # Prints the greeting message

# Calls the function with the argument "Alice"
greet("Alice")  # Output: Hello, Alice!

4

Default Arguments

You can assign default values to function 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"):
    greeting = "Hello, " + name + "!"  # Creates a greeting message
    print(greeting)  # Prints the greeting message

# Calls the function without an argument
greet()  # Output: Hello, Python Learner!

# Calls the function with an argument
greet("Bob")  # Output: Hello, Bob!

5

Keyword Arguments

When calling functions, you can specify arguments by their parameter names, allowing you to pass them in any order.

Example:

# Defines a function that describes a person
def describe_person(name, age):
    description = name + " is " + str(age) + " years old."  # Creates a description
    print(description)  # Prints the description

# Calls the function using keyword arguments
describe_person(age=25, name="Charlie")  # Output: Charlie is 25 years old.

6

Variable-Length Arguments (*args)

Sometimes, you might not know how many arguments will be passed to your function. You can use *args to handle an arbitrary number of positional arguments.

Example:

# Defines a function that sums any number of numbers
def sum_numbers(*args):
    total = 0  # Initializes the total
    for number in args:
        total += number  # Adds each number to the total
    return total  # Returns the sum

# Calls the function with different numbers of arguments
print(sum_numbers(1, 2, 3))       # Output: 6
print(sum_numbers(4, 5))          # Output: 9
print(sum_numbers(10, 20, 30, 40))  # Output: 100

7

Variable-Length Keyword Arguments (**kwargs)

Similar to *args, **kwargs allows you to handle an arbitrary number of keyword arguments (i.e., named arguments).

Example:

# Defines a function that prints keyword arguments
def print_info(**kwargs):
    for key, value in kwargs.items():
        print(key + ": " + str(value))  # Prints each key-value pair

# Calls the function with various keyword arguments
print_info(name="Diana", age=28, city="New York")
# Output:
# name: Diana
# age: 28
# city: New York

8

Return Values

The return statement sends back a value from the function to the caller. This allows you to use the result of a function in other parts of your code.

Example:

# Defines a function that returns the square of a number
def square(number):
    result = number ** 2  # Calculates the square
    return result  # Returns the square

# Calls the function and stores the returned value
squared_value = square(5)
print("Square:", squared_value)  # Output: Square: 25

9

# Function that multiplies two numbers and returns the result
def multiply(a, b):
    product = a * b  # Calculates the product
    return product  # Returns the product

# Calls the function and stores the returned value
result = multiply(4, 5)
print("Product:", result)

10

Great Job!

You've learned how to pass arguments into functions and retrieve results using return values in Python. These concepts make your functions more versatile and powerful, allowing you to handle dynamic data and perform complex tasks. Keep practicing by creating your own functions with different types of arguments and return values!

Arguments and Return Values — illustration 10

Frequently asked questions

Is the “Arguments and Return Values” lesson free?

Yes — the full text of “Arguments and Return Values” 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 “Arguments and Return Values”?

Learn to pass data into functions and get results. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Arguments and Return Values” 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