Skip to main content
March 23, 2026Noble Desktop/3 min read

Composing a Function in Python

Master Python Function Fundamentals Through Hands-On Practice

Learning Objective

This tutorial covers the fundamental concepts of creating, defining, and calling custom functions in Python, including proper syntax, return statements, and variable assignment.

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

1

Define the Function

Use 'def' keyword followed by function name 'add' and parameters (a, b). Choose names that clearly indicate the function's purpose.

2

Set Parameter Values

Assign values to parameters within the function body. In this example, a equals 6 and b equals 7.

3

Perform Calculations

Create a variable 'total' that stores the result of a plus b operation for later use.

4

Return the Result

Use the return statement to send the total back to whoever called the function. This must be the final statement.

5

Call the Function

Invoke the function by using its name followed by parentheses to execute the code and get results.

Critical Rule About Return Statements

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

FeaturePrint StatementReturn Statement
PurposeDisplay output to consoleSend value back to caller
ReusabilityLimited - only shows outputHigh - value can be stored
Variable AssignmentCannot assign resultCan assign to variables
Function ChainingNot possibleEnables function composition
Recommended: Use return statements in functions to enable variable assignment and function reusability rather than just printing output.

Function Creation Best Practices

0/5
A function is a block of reusable code, something we can use over and over again.
This fundamental definition emphasizes the primary benefit of functions in programming - code reusability and modularity.
Saving Function Results

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.

Key Takeaways

1Functions are reusable blocks of code that can be called multiple times throughout a program
2Use the 'def' keyword to define functions, followed by a descriptive name that indicates the function's purpose
3Function parameters allow you to pass data into the function for processing and manipulation
4Return statements must be the last line in a function and send results back to the calling code
5Functions must be called or invoked by name to execute their code and produce results
6Assign function results to variables to capture and reuse the returned values in your program
7Use return statements instead of print statements in functions to enable proper data flow and reusability
8Functions enable code modularity and can be composed together to build more complex functionality

RELATED ARTICLES