0Pricing
Python Academy · Lesson

Defining and Calling Functions

Create functions with def and call them with arguments.

Defining and Calling Functions is a free Python Academy 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 Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Functions are reusable blocks of code defined with def. They avoid repetition and make programs easier to test and maintain.

def Keyword

def greet(name): defines a function. The body is indented. Call it with greet('Alice').
def greet(name):
    print('Hello', name)
greet('Alice')

Positional Arguments

Arguments are passed by position. def add(a, b): return a + b — add(3,4) gives 7.
def add(a, b):
    return a + b
print(add(3, 4))

Return Statement

return sends a value back to the caller. A function without return returns None implicitly.
def square(x):
    return x * x
result = square(5)
print(result)

Multiple Return Values

def min_max(lst): return min(lst), max(lst) returns a tuple. Unpack: lo, hi = min_max(lst).
def min_max(lst):
    return min(lst), max(lst)
lo, hi = min_max([3,1,4,1,5])
print(lo, hi)

Docstrings

The first string in a function body is its docstring: """One-line summary.""" Access it with help() or func.__doc__.
def greet(name):
    """Return a greeting string."""
    return f"Hello {name}"
print(greet.__doc__)

Variable Number of Arguments

def func(*args) collects extra positional args into a tuple. Useful when you don't know how many there will be.
def total(*nums):
    return sum(nums)
print(total(1,2,3,4))

Functions are Objects

Functions are first-class objects. You can assign them to variables, pass them as arguments, or store them in lists.
def double(x): return x * 2
fn = double
print(fn(5))

Calling with Unpacking

args = [1,2]; f(*args) unpacks a list as positional args. d = {'a':1}; f(**d) unpacks a dict as keyword args.
def add(a, b): return a + b
args = [3, 4]
print(add(*args))

Lambda Functions

lambda x: x*2 is an anonymous one-line function. Use for short callbacks; prefer named functions for anything complex.
double = lambda x: x * 2
print(double(5))
print(list(map(lambda x: x**2, [1,2,3])))

Nested Functions

Functions can be defined inside other functions. The inner function has access to the outer function's local variables (closure).
def outer(x):
    def inner(y):
        return x + y
    return inner
add5 = outer(5)
print(add5(3))

Quick Check

What does a function return if it has no explicit return statement?

Recap

Functions: def name(args): body. return sends values back. Multiple returns via tuple. Lambdas for simple one-liners. Functions are first-class objects.

Keep Going

Great work! Move on to the next lesson to keep progressing.

Frequently asked questions

Is the “Defining and Calling Functions” lesson free?

Yes — the full text of “Defining and Calling Functions” is free to read here on the web, and the Python Academy 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 Academy course, upgrade to CoddyKit PRO.

What will I learn in “Defining and Calling Functions”?

Create functions with def and call them with arguments. You practise Python Academy 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 Academy?

No prior experience is required. Python Academy 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 “Defining and Calling 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 Academy lesson?

Yes. Every Python Academy 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. Defining and Calling Functions
  2. Default and Keyword Arguments
  3. args and kwargs
  4. Return Values and Scope
← Back to Python Academy