Labs ICT
โญ Pro Login

Functions

What is a Function?

A function is a reusable block of code that performs a specific task. You define it once with def and call it whenever you need it.

Basic Function

def greet(name):
    return "Hello, " + name + "!"

print(greet("Alice"))
Try it Yourself โ†’

Function with Return Value

Use return to send a result back to the caller.

def add(a, b):
    return a + b

result = add(5, 3)
print(result)
Try it Yourself โ†’

Docstrings

A docstring describes what the function does. It goes right after the def line.

def square(n):
    """Return the square of a number."""
    return n * n

print(square(4))
print(square.__doc__)
Try it Yourself โ†’

๐Ÿงช Quick Quiz

What keyword defines a function in Python?