0Pricing
Learn AI with Python · Lesson

What is a Function?

Learn how functions help organize and reuse your code.

What is a Function? is a free Learn AI with Python lesson on CoddyKit — lesson 1 of 5. 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 Learn AI with Python learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Introduction to Functions

Functions are reusable blocks of code designed to perform specific tasks. They help reduce redundancy and make programs more organized.

In this lesson, you will learn how to define, call, and use functions in Python.

What is a Function? — illustration 1

2

What is a Function?

A function is a named section of code that performs a specific task. Functions can take inputs, process data, and return outputs.

For example:

# Defining a simple function
def greet():
    print("Hello, World!")

3

4

Defining a Function

You define a function using the def keyword, followed by the function name and parentheses:

The code inside the function will only execute when the function is called.

# Defining a function
def greet():
    print("Hello!")

5

Calling a Function

To use a function, you call it by its name followed by parentheses:

This runs the code inside the function.

# Calling a function
def greet():
    print("Hello!")

greet()

6

Using Parameters in Functions

Parameters are inputs that you can pass to a function to customize its behavior. For example:

# Function with parameters
def greet(name):
    print(f"Hello, {name}!")

greet("Alice")

7

Using Multiple Parameters

Functions can take multiple parameters:

Here, a and b are parameters.

# Function with multiple parameters
def add(a, b):
    print(a + b)

add(3, 5)

8

Keyword Arguments

You can use keyword arguments to specify parameter values by name:

# Function with keyword arguments
def greet(name, age):
    print(f"Hello, {name}. You are {age} years old.")

greet(age=30, name="Alice")

9

def multiply(a, b):
    return a * b

result = multiply(2, 4)
print(result)

10

Best Practices for Functions

Here are some best practices for using functions:

  • Use descriptive names for functions and parameters.
  • Keep functions short and focused on a single task.
  • Avoid redundant code by reusing functions.

11

What Did We Learn?

In this lesson, you learned:

  • What functions are and how to define them.
  • How to call functions with parameters and keyword arguments.
  • Best practices for writing clean and reusable functions.

Great job! Let’s move to the next lesson.

What is a Function? — illustration 11

Frequently asked questions

Is the “What is a Function?” lesson free?

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

What will I learn in “What is a Function?”?

Learn how functions help organize and reuse your code. You practise Learn AI with Python 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 Learn AI with Python?

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

How long does the “What is a Function?” 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 Learn AI with Python lesson?

Yes. Every Learn AI with Python 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 is a Function?
  2. Return Values
  3. Scope and Lifetime of Variables
  4. Built-in Functions vs. User-Defined Functions
  5. Importing Modules
← Back to Learn AI with Python