0
0
PythonHow-ToBeginner · 3 min read

How to Call a Function in Python: Simple Guide

To call a function in Python, write the function's name followed by parentheses (). If the function needs inputs, put the values inside the parentheses, like function_name(argument). This runs the code inside the function and can return a result.
📐

Syntax

Calling a function means running the code inside it. The basic syntax is:

  • function_name(): Calls a function without inputs.
  • function_name(argument1, argument2): Calls a function with inputs called arguments.

The parentheses () are required to run the function.

python
function_name()
function_name(argument1, argument2)
💻

Example

This example shows how to define a function and call it with and without arguments.

python
def greet():
    print("Hello!")

def add(a, b):
    return a + b

# Call greet function
print("Calling greet():")
greet()

# Call add function with arguments
result = add(3, 5)
print("Result of add(3, 5):", result)
Output
Calling greet(): Hello! Result of add(3, 5): 8
⚠️

Common Pitfalls

Common mistakes when calling functions include:

  • Forgetting parentheses () means the function does not run, you just get the function itself.
  • Passing the wrong number of arguments causes errors.
  • Using parentheses but misspelling the function name causes a NameError.
python
def say_hello():
    print("Hi!")

# Wrong: missing parentheses, function not called
print(say_hello)  # Prints function info, not "Hi!"

# Correct: call the function
say_hello()
Output
<function say_hello at 0x7f8c2c3d1d30> Hi!
📊

Quick Reference

Remember these tips when calling functions:

  • Always use parentheses () to call a function.
  • Put arguments inside parentheses if the function needs them.
  • Check the function name spelling carefully.
  • Functions can return values; use return to get results.

Key Takeaways

Use the function name followed by parentheses to call a function in Python.
Include arguments inside parentheses if the function requires inputs.
Forgetting parentheses means the function won't run, just referenced.
Passing wrong arguments or misspelling names causes errors.
Functions can return values; capture them with variables.