Skip to content
PythonFunctionsbeginner18 min read

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

Define a function and call it
python
def greet():
print('Hello!')
 
greet()
Output
Hello!

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

A function with a clear name
python
def display_message():
print('Welcome to Python')
 
display_message()
Output
Welcome to Python

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

A function definition must be syntactically complete. For example, def greet(): followed by an unindented function body is invalid. The function body also cannot be omitted; use an appropriate statement such as pass when a temporary empty body is needed.

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

Pass a name to a function
python
def greet(name):
print(f'Hello, {name}!')
 
greet('Mina')
greet('Arun')
Output
Hello, Mina! Hello, Arun!

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

For the examples in this lesson, provide the required number of arguments. Calling greet() when greet(name) requires one argument raises TypeError because the required argument is missing. Supplying too many positional arguments can also raise TypeError.

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

Trace the parameter value
python
def show_number(number):
print('Number:', number)
 
print('Start')
show_number(7)
show_number(12)
print('End')
Output
Start Number: 7 Number: 12 End

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

The word parameter describes a name in the definition; argument describes the value supplied by a call. Keeping these terms separate makes function code easier to read and discuss.

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

Reuse a function for a calculation
python
def show_square(number):
print(number * number)
 
show_square(4)
show_square(6)
show_square(-3)
Output
16 36 9

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

Do not confuse defining a function with calling it. A correct definition can produce no visible output on its own. Also, do not confuse print() with return: print() displays information, while return passes a value back to the caller. The next lesson explores return values in detail.

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.

Incorrect Approach
script.py
python
Defining a function but never calling it.
Correct Solution
script.py
python
After defining the function, write a function call such as greet() wherever you want its body to execute.

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.

Incorrect Approach
script.py
python
Forgetting the colon after the function header.
Correct Solution
script.py
python
Write the header with a colon, for example def greet():.

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.

Incorrect Approach
script.py
python
Indenting the function body incorrectly.
Correct Solution
script.py
python
Indent the statements belonging to the function body consistently, using the standard four spaces.

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.

Incorrect Approach
script.py
python
Confusing parameters with arguments.
Correct Solution
script.py
python
A parameter is a name in the definition; an argument is a value supplied in the call.

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.

Incorrect Approach
script.py
python
Assuming that a function definition immediately displays its output.
Correct Solution
script.py
python
A definition establishes the function. Its body runs when the function is called.

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.

Incorrect Approach
script.py
python
Calling a function with the wrong number of required arguments.
Correct Solution
script.py
python
Check the parameters in the definition and provide the corresponding arguments in the call.

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.

Incorrect Approach
script.py
python
Writing the same task repeatedly instead of reusing a function.
Correct Solution
script.py
python
Define the task once and call the function with the appropriate arguments wherever the task is needed.

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.

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?
Answer:
A function is a named block of reusable code that performs a task. Functions reduce repetition, give tasks descriptive names, and help divide a program into smaller, easier-to-maintain parts.
Q2:How do you define and call a function?
Answer:
Use def, followed by the function name, parentheses, and a colon. Indent the function body underneath the header. To execute it, write a call using the function name and parentheses, supplying any required arguments. For example, def greet(): followed by an indented print('Hello') statement is called with greet().
Q3:What is the difference between a parameter and an argument?
Answer:
A parameter is a name declared in the function definition. An argument is a value or expression supplied in a function call. In def greet(name):, name is a parameter; in greet('Mina'), 'Mina' is an argument.
Q4:Does a function execute when Python encounters its definition?
Answer:
No. Python processes the def statement to create the function, but the function body does not run just because it was defined. The body executes when the function is called.
Q5:Can a function be called multiple times with different values?
Answer:
Yes. For example, greet('Mina') and greet('Arun') call the same greet(name) function with different arguments. Each call executes the body using the value supplied for that call.
Q6:What happens if a required argument is not supplied?
Answer:
Calling a function without a required argument raises TypeError. The same error category can occur if too many positional arguments are supplied. The exact message depends on the call and Python version.
Q7:Why is indentation important in a function definition?
Answer:
Indentation identifies the statements that belong to the function body. Incorrect indentation can cause an IndentationError or make statements belong to a different block than intended.
Q8:What is the difference between print() and return in a function?
Answer:
print() displays information to the output stream. return sends a value back to the caller and ends the current function execution. A function can print a result without returning it. The next lesson explores return values in more detail.

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.
Was this lesson helpful?
Found an error? Report an issue

Related Topics & Next Steps

4 Recommended Lessons

Strengthen your Python foundation with these complementary core lessons: