No history yet

Introduction to Python Functions

Organizing Code with Functions

Imagine you're writing instructions for a daily routine. You probably wouldn't write out every single step of "brush your teeth" each morning. Instead, you just think, "Time to brush my teeth." The detailed steps—like picking up the toothbrush, applying toothpaste, and so on—are bundled under that one simple command.

In programming, functions work the same way. They are named blocks of code that perform a specific task. You write the code once, give it a name, and then you can run that block of code whenever you want, simply by calling its name.

Functions in Python are reusable blocks of code that allow you to organise tasks efficiently.

Using functions makes your programs cleaner, easier to read, and less repetitive. If you need to change how a task is done, you only have to update the code in one place: inside the function.

Defining a Function

To create a function in Python, you use the def keyword, which is short for "define." You follow def with the name you want to give your function, a pair of parentheses (), and a colon :. The name should be descriptive of what the function does.

The actual code that the function will execute goes on the lines below the def statement. This block of code, called the function body, must be indented. Indentation is how Python knows which lines of code belong to the function.

def say_hello():
    # The next two lines are the function body
    # They are indented to show they are inside the function
    print("Hello from inside the function!")
    print("Isn't this neat?")

In this example, we've defined a function named say_hello. The body of the function contains two print statements. Notice the parentheses are empty for now; we'll explore what goes in them later on.

Calling a Function

Just defining a function doesn't make it run. It's like writing down a recipe and putting it in a book. The recipe just sits there until you decide to use it. To run the code inside a function, you need to call it.

Calling a function is simple: you just type its name followed by parentheses.

Function call syntax: function_name()

When the program encounters a function call, it pauses what it's doing, jumps to the function's definition, runs all the code inside the function's body, and then returns to the line right after the function call to continue.

# 1. First, we define the function
def say_hello():
    print("Hello from inside the function!")

# 2. This code runs before the function is called
print("This is the first line of the program.")

# 3. Now, we call the function
say_hello()

# 4. This code runs after the function has finished
print("This is the last line of the program.")

If you run the code above, the output will be:

This is the first line of the program. Hello from inside the function! This is the last line of the program.

This shows the flow of control. The program executes statements in order, jumping into the function when it's called, and then returning to where it left off.

Quiz Questions 1/5

What is the primary purpose of a function in programming?

Quiz Questions 2/5

Which keyword is used to start the definition of a function in Python?

Functions are a fundamental concept in programming. By grouping code into reusable blocks, you can write programs that are more organized and efficient.