Video Transcription
Hi, I'm Art, and I teach Python at Noble Desktop. Today, I'll walk you through creating your own custom function—one of the most fundamental skills for writing clean, maintainable code.
Functions are the building blocks of efficient programming. They represent reusable blocks of code that eliminate redundancy and make your programs more organized and scalable. Think of functions as specialized tools in your programming toolkit—once you build them, you can use them repeatedly throughout your projects. Let's build your first function from the ground up.
Every function begins with the keyword 'def' (short for 'define'), followed by a descriptive name that clearly indicates the function's purpose. For our example, I'll create a function called 'add'—a straightforward name that immediately tells other developers what this function accomplishes. This function accepts two parameters, 'a' and 'b', which act as placeholders for the values we'll pass in later. Inside the function, I'll set a = 6 and b = 7, then calculate their sum using the expression total = a + b.
Here's where functions differ from simple print statements. While print() displays output for human consumption, functions should return values that other parts of your program can use. The 'return' statement serves this crucial purpose—it sends the calculated result back to wherever the function was called. In our case, we're returning the 'total' variable, making this function's output available for further processing or storage.
Creating a function is only half the equation—you must call (or invoke) it to execute the code inside. When I call add(), the function runs and produces our expected result. This separation between definition and execution is what makes functions so powerful: you define the logic once, then call it whenever needed.
Two critical principles govern effective function design. First, 'return' must always be your function's final statement—Python ignores any code placed after a return statement, which can lead to subtle bugs. Second, to preserve your function's output for later use, assign the returned value to a variable. For instance, you might create variables like 'calculation_result' or 'sum_total' to store the function's output, making it available for subsequent operations in your program.
Understanding these variables is essential for building complex applications. When you assign a function's return value to a variable, you're essentially capturing the function's work product for use elsewhere in your code. This pattern becomes invaluable as you build larger programs with multiple interconnected functions.
In my upcoming video, I'll demonstrate how to call functions within other functions—a technique that opens the door to more sophisticated programming patterns and modular code architecture. Until then, practice creating and calling your own functions to solidify these foundational concepts.
Core Function Components
Function Definition
Start with the 'def' keyword followed by a descriptive function name that reflects its purpose. Include parameters in parentheses.
Function Body
Write the logic that processes the input parameters. Use meaningful variable names and clear operations.
Return Statement
Always end with a return statement to send results back. This must be the last statement in the function.
Creating Your First Python Function
Define the Function
Use 'def' keyword followed by function name 'add' and parameters (a, b). Choose names that clearly indicate the function's purpose.
Set Parameter Values
Assign values to parameters within the function body. In this example, a equals 6 and b equals 7.
Perform Calculations
Create a variable 'total' that stores the result of a plus b operation for later use.
Return the Result
Use the return statement to send the total back to whoever called the function. This must be the final statement.
Call the Function
Invoke the function by using its name followed by parentheses to execute the code and get results.
The return statement must always be the last statement within a function. No code can be executed after the return statement, as it immediately exits the function.
Print vs Return in Functions
| Feature | Print Statement | Return Statement |
|---|---|---|
| Purpose | Display output to console | Send value back to caller |
| Reusability | Limited - only shows output | High - value can be stored |
| Variable Assignment | Cannot assign result | Can assign to variables |
| Function Chaining | Not possible | Enables function composition |
Function Creation Best Practices
Names should clearly indicate what the function does, like 'add' for addition operations
Parameters should represent the data your function needs to operate effectively
Functions should return values to enable reusability and variable assignment
No code can execute after return, so it must be the final statement in your function
Verify your function works by calling it and checking the output or assigned variables
A function is a block of reusable code, something we can use over and over again.To capture and reuse function output, assign the function call to a variable. This allows you to store the returned value for later use in your program.