Defining and Calling Functions
Learn to define functions with def, call them, pass arguments to parameters, and reuse code through clear, traceable examples.
In this lesson, you will learn
- ✓Explain why functions are useful for organizing and reusing code.
- ✓Define a function using def and correctly indent its body.
- ✓Distinguish a parameter in a function definition from an argument supplied in a function call.
- ✓Trace function definitions and calls in execution order.
- ✓Call a function multiple times with different arguments.
- ✓Write, test, and explain a simple function.
1. What is a function, and why do we use one?
Imagine that a program needs to display the same greeting several times. You could write the print() statement repeatedly, but that duplicates code. A function lets you give a task a name, define its instructions once, and execute those instructions whenever you call the function.
Functions help reduce repetition, give meaningful names to tasks, and divide a larger program into smaller, easier-to-understand parts. A function can perform an action, calculate a result, or do both.
Python provides built-in functions such as print() and len(). You can also define your own functions using def. This lesson focuses on functions that you create yourself.
Define a function and call it
The line def greet(): defines a function named greet. The indented print('Hello!') statement is the function body: the instructions that execute when the function is called. The line greet() calls the function. The parentheses are part of the call syntax. Python defines the function before reaching the call, so the call can execute its body. The print() statement displays Hello! followed by a newline.
2. Function syntax, indentation, and naming
A basic function definition follows this structure: def function_name(): followed by an indented body. The colon ends the function header and introduces the block of statements belonging to the function.
The function name should describe the task. By convention, Python function names usually use lowercase words separated by underscores, such as calculate_total() or display_message().
The function body must be indented consistently. Four spaces are the standard indentation style in Python. A missing or inconsistent indentation can cause an IndentationError.
A function definition may contain parameters inside its parentheses. If a function needs no parameters, empty parentheses are used in both the definition and the call.
A function with a clear name
def begins the function definition. display_message is the function name. The parentheses are empty because this function does not require any parameters. The colon introduces the function body, and the indented print() statement belongs to that body. display_message() calls the function and executes the print() statement.
Note
3. Parameters and arguments
A parameter is a name written in the function definition. It acts as a local name that receives a value when the function is called.
An argument is the value or expression supplied to a function call. For example, in greet('Mina'), the string 'Mina' is an argument. If the definition is def greet(name):, name is the parameter.
When a function is called with a matching positional argument, Python binds that argument's value to the corresponding parameter. The function body can then use the parameter just like another local variable.
Parameters make functions flexible. You can define a task once and supply different arguments on different calls.
Pass a name to a function
name is a parameter because it appears in the function definition. In greet('Mina'), the string 'Mina' is the argument and is bound to name for that call. The f-string inserts the current value of name into the greeting. The second call supplies 'Arun', so the same function displays a different greeting. The function definition is reused; the code does not need to be duplicated for each name.
Note
4. Trace a function call step by step
To understand a function, separate the definition from the call. Python first processes the definition. When it reaches a call, it evaluates the supplied arguments, binds their values to the parameters, and executes the function body.
When the body finishes, execution continues after the function call. If the body contains print(), the text is displayed. A function does not need to return a value explicitly to perform an action.
A useful debugging habit is to track the parameter values for each call and write down which statement executes next. This prevents you from treating the function definition as if it were an immediate function call.
Trace the parameter value
Python defines show_number without displaying anything. The first print() displays Start. The first call binds 7 to number and displays Number: 7. The second call binds 12 to number and displays Number: 12. After both calls finish, the final print() displays End. The parameter name is the same in both calls, but its value is determined separately for each call.
Note
5. Practical use and common edge cases
Use a function when a task has a clear purpose, when the same operation is needed in several places, or when a larger program can be made easier to understand by naming a smaller task.
A function should generally have a descriptive name and a focused responsibility. For example, display_receipt() communicates its purpose more clearly than do_stuff().
Check the arguments expected by a function. A function that requires two parameters needs two corresponding arguments unless its definition supports another calling pattern. Incorrect calls may fail with TypeError.
Remember that a function can be called more than once. Each call executes its body again, using the arguments supplied for that call.
This lesson uses print() to make function behavior visible. Printing a value and returning a value are different operations. Returning values is the focus of the next lesson.
Reuse a function for a calculation
show_square has one parameter named number. For the first call, Python calculates 4 * 4 and prints 16. For the second call, it calculates 6 * 6 and prints 36. For the third call, it calculates -3 * -3 and prints 9. The same function handles positive and negative integers without needing separate copies of the code.
Caution
Common pitfalls to avoid
Real debugging lessons and exact syntax contrasts to avoid runtime bugs
#1 Defining a function but never calling it.
Why it happens: A def statement creates a function object but does not execute its body; call the function by writing its name followed by parentheses.
Key takeaway: After defining the function, write a function call such as greet() wherever you want its body to execute.
#2 Forgetting the colon after the function header.
Why it happens: The colon marks the start of the function suite, so omitting it causes a syntax error.
Key takeaway: Write the header with a colon, for example def greet():.
#3 Indenting the function body incorrectly.
Why it happens: Python uses indentation to define the function body, and statements at the wrong indentation level may fall outside the function or trigger an error.
Key takeaway: Indent the statements belonging to the function body consistently, using the standard four spaces.
#4 Confusing parameters with arguments.
Why it happens: Parameters are names listed in a function definition; arguments are the actual values supplied when the function is called.
Key takeaway: A parameter is a name in the definition; an argument is a value supplied in the call.
#5 Assuming that a function definition immediately displays its output.
Why it happens: Defining a function stores reusable instructions but does not display output until the function is called and its body performs an output operation.
Key takeaway: A definition establishes the function. Its body runs when the function is called.
#6 Calling a function with the wrong number of required arguments.
Why it happens: Required parameters must receive matching arguments unless the function definition supplies defaults or accepts variable-length arguments.
Key takeaway: Check the parameters in the definition and provide the corresponding arguments in the call.
#7 Writing the same task repeatedly instead of reusing a function.
Why it happens: Repeated code is harder to maintain and can drift out of sync; a function lets one implementation be reused for multiple inputs.
Key takeaway: Define the task once and call the function with the appropriate arguments wherever the task is needed.
Ready for hands-on challenges?
Test your skills with shuffled coding challenges in the Practice Arena.
Technical and placement interview questions
Frequently tested interview questions with detailed, full answers (click to expand)
Q1:What is a function in Python, and why is it useful?
Q2:How do you define and call a function?
Q3:What is the difference between a parameter and an argument?
Q4:Does a function execute when Python encounters its definition?
Q5:Can a function be called multiple times with different values?
Q6:What happens if a required argument is not supplied?
Q7:Why is indentation important in a function definition?
Q8:What is the difference between print() and return in a function?
Key takeaways
- Use def to define a function and a function call to execute its body.
- A function definition does not execute its body immediately.
- The function body is identified by indentation; four spaces per level is the standard style.
- A parameter is a name in a function definition, while an argument is a value supplied in a call.
- Functions can be reused with different arguments without duplicating their code.
- A call must supply the required arguments unless the function definition allows them to be omitted.
- print() displays information; return passes a value back to the caller and is covered in the next lesson.
Related Topics & Next Steps
4 Recommended LessonsStrengthen your Python foundation with these complementary core lessons: