Module 9 · Functions — topic 1 of 19
Functions
What a function is, why programs are built from them, the difference between built-in and user-defined functions, and how a function call transfers control and returns a result.
Learning objectives
- Explain what a function is and why it is useful
- Distinguish built-in functions from functions you write
- Describe what happens when a function is called
Definition
A function is a named, reusable block of code that performs one task. It may receive input values (arguments), and it can send a result back to the code that called it with return.
Introduction
You have already used many functions: print(), len(), input(), round(), sorted(). Each one hides a piece of work behind a name. You call it, give it what it needs, and get a result back, without caring how it works inside.
Python lets you write your own functions in exactly the same way. Once a calculation or task has a name, you can reuse it anywhere, test it on its own and change it in one place.
Why it matters
Real programs are thousands of lines long. Without functions they would be one enormous script that nobody could understand. Functions break a problem into small named pieces, each easy to read and to check.
Functions also remove duplication. If the same five lines appear in three places, a bug must be fixed three times; in a function it is fixed once.
Detailed explanation
Built-in functions come with Python and are always available. User-defined functions are the ones you write with the def keyword. Library functions come from modules you import, such as math.sqrt.
Calling a function means writing its name followed by parentheses, with any arguments inside: area(3, 4). Python pauses the current code, runs the function body with those values, and then continues from where it left off.
A function can return a value with return; the call expression then evaluates to that value, so you can store it, print it or pass it to another function. A function without a return statement returns None.
Good functions do one thing, have a descriptive name (usually a verb such as calculate_total or is_valid_email), and depend only on their inputs whenever possible. Such functions are easy to test and reuse.
Thinking in functions is a form of abstraction: once average() works, you can use it while thinking only about what it does, not how.
Syntax
- def starts a function definition.
- The body is indented.
- Calling the function runs the body.
Code examples
Built-in functions
3 95 225 75.0
Your first function
Hello, Asha! Hello, Ravi!
More examples
Replacing repeated code with a function
Asha: average 91.7, pass Ravi: average 59.0, fail Meera: average 84.7, pass
Step-by-step explanation
- def report(name, marks): defines a function with two parameters.
- Inside, average and status are computed from the parameters.
- return sends a formatted string back to the caller.
- Each print(report(...)) call runs the body with different arguments and prints the returned string.
Expected output
Running “Replacing repeated code with a function” prints exactly:
Practical applications
- A web app has functions such as login(), add_to_cart() and send_invoice().
- Data scripts define clean_row() and summarise() and apply them to every record.
- Libraries like math and statistics are collections of well-tested functions.
Best practices
- Give each function one clear job and a verb name.
- Prefer returning results over printing inside functions.
- Keep functions short enough to read at a glance.
Common mistakes
Defining but never calling
Avoid
Prefer
A def only creates the function. The body runs when you call it.
Printing instead of returning
Avoid
Prefer
print shows a value but the function still returns None; return makes the value available to the caller.
Important notes
- Functions are objects in Python: you can store them in variables and pass them to other functions (see First-class functions).
Key takeaways
- A function is a named, reusable block of code.
- Calling it runs the body with the given arguments.
- return sends a value back; without it the result is None.
Try it yourself
- 1Write a function celsius_to_fahrenheit(c) and print the result for 0, 37 and 100.
- 2Write is_even(n) that returns True or False, and use it in a loop over 1 to 10.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
15 questions cover the “Functions” module. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?