Introduction to Functions

Let’s get started with an introduction to functions in Python! 🚀🐍

What is a Function? 🤔

A function is a named block of code that performs a specific task. You can use a function whenever you need to repeat the same action multiple times in your program, or to organize your code into smaller, manageable pieces. Functions help make your code reusable, modular, and easier to read!12.

Why Use Functions? 🎯

  • Avoid repetition: Write code once, use it many times.
  • Organize code: Break big problems into smaller, logical steps.
  • Make code reusable: Use the same function in different places.
  • Easier to test and debug: Fix issues in one place.

Defining a Function 🛠️

You define a function using the def keyword:

def greet():
    print("Hello, world!")
  • def: Tells Python you’re defining a function.
  • greet: The function’s name.
  • (): Parentheses (can hold parameters).
  • : (colon): Start of the function’s code block.
  • Indented code: The body of the function.

Calling (Using) a Function 📞

To use (call) the function, just write its name followed by parentheses:

greet()  # Output: Hello, world!

Functions with Parameters and Return Values 🎁

You can pass information to a function using parameters:

def greet(name):
    print("Hello,", name)

greet("Alice")  # Output: Hello, Alice

A function can also return a value:

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

result = add(3, 5)
print(result)  # Output: 8
  • return sends a value back to where the function was called.

Built-in vs. User-defined Functions 🏗️

  • Built-in functions: Already provided by Python (like print(), len(), sum())
  • User-defined functions: You create them for your own needs.

Anatomy of a Function 🧩

PartExampleDescription
Namedef greet():How you call the function
Parameters(name)Data you give to the function
Bodyprint("Hello")The code that runs when called
Return valuereturn xWhat the function gives back (optional)

Practice Questions & Solutions 📝

1️⃣ Write a function that prints “Python is fun!”

Show Solution
def say_fun():
    print("Python is fun!")

say_fun()

2️⃣ Write a function that takes a number and returns its square.

Show Solution
def square(x):
    return x * x

print(square(4))  # Output: 16

3️⃣ Write a function that takes two numbers and prints their sum.

Show Solution
def print_sum(a, b):
    print(a + b)

print_sum(3, 7)  # Output: 10

4️⃣ What happens if you call a function without parentheses?

Show Solution

You get a reference to the function object, not its result!
Example:

def greet():
    print("Hi!")
print(greet)  # Output: <function greet at ...>

5️⃣ Write a function that returns both the sum and product of two numbers.

Show Solution
def sum_and_product(a, b):
    return a + b, a * b

s, p = sum_and_product(2, 5)
print(s, p)  # Output: 7 10

Key Points with Emojis 🎯

  • Functions = reusable blocks of code 🧱
  • Use def to define, () to call
  • Can take parameters and return values
  • Make code cleaner, shorter, and easier to manage!

If you want to learn about default values, keyword arguments, recursion, or lambda (anonymous) functions, just ask! 😃

References: Functions are named blocks of code that can take parameters, may return values, and help organize, reuse, and structure your programs12. You define them with def, call them with (), and use them to make your code modular and efficient.


  1. OER-202301_Wang_2023-Introduction-to-Computer-Programming-with-Python.pdf ↩︎ ↩︎

  2. itpacs_cafiero.pdf ↩︎ ↩︎