How to Call a Function in Python

What you’ll build or solve

You’ll call Python functions with the right arguments and use their return values without errors.

When this approach works best

This approach works best when you:

  • Wrote a function with def and want to run it with different inputs.
  • Need to pass data into a function and use the value it returns later in your program.
  • Are using a library function and keep getting errors about missing or unexpected arguments.

Avoid this approach when:

  • You are trying to “call” a variable or value that is not a function. Fix the type first.

Prerequisites

  • Python installed
  • Basic comfort running a Python file or using the Python REPL

Step-by-step instructions

1) Call a function by adding parentheses

A function call uses the function name followed by parentheses. If the function needs inputs, you put them inside the parentheses.

print("Hello")

If the function takes no arguments, the parentheses are still required:

importrandom

value=random.random()
print(value)

What to look for: if you forget the parentheses, you get a function object, not the result.


2) Pass positional arguments in the correct order

Positional arguments are matched by position. The first value goes to the first parameter, and so on.

defadd(a,b):
returna+b

total=add(2,3)
print(total)

If you swap the order, you change what the function receives:

defformat_name(first,last):
returnf"{last},{first}"

print(format_name("Ada","Lovelace"))

3) Use keyword arguments for clarity

Keyword arguments match by parameter name, so order matters less and calls are easier to read.

defsend_email(to,subject,body):
returnf"To:{to}\nSubject:{subject}\n\n{body}"

message=send_email(
to="sam@example.com",
subject="Welcome",
body="Thanks for signing up."
)
print(message)

What to look for: keyword arguments must use valid parameter names. Misspelling a name raises a TypeError.


4) Call a function with default values

A function can define default parameter values. You can omit those arguments when calling.

defgreet(name,punctuation="!"):
returnf"Hi,{name}{punctuation}"

print(greet("Mina"))
print(greet("Mina",punctuation=" :)"))

5) Capture and use the return value

Some functions return values, and some only produce side effects like printing. Store return values in a variable when you need them later.

defarea(width,height):
returnwidth*height

result=area(5,4)
print(result)

If a function does not return anything, it returns None:

deflog_message(text):
print(f"[LOG]{text}")

value=log_message("Started")
print(value)# None

What to look for: if you see None where you expected a result, check whether the function has a return.


6) Call methods on objects

Methods are functions that belong to objects. You call them with a dot, then parentheses.

name="ada lovelace"
fixed=name.title()
print(fixed)

Some methods return a new value, but others change the object in place.

numbers= [3,1,2]
numbers.sort()
print(numbers)

What to look for: list.sort() returns None. Use sorted(numbers) if you want a new list instead.


7) Call a function in the right scope

A function must be defined or imported before you call it. In a script, top-to-bottom order matters.

defsay_hi():
return"Hi"

print(say_hi())

If you call it before the def, Python raises a NameError.


Examples you can copy

Example 1: Call a function and print the result

defcelsius_to_fahrenheit(c):
return (c*9/5)+32

print(celsius_to_fahrenheit(20))

Example 2: Use keyword arguments to avoid mistakes

defschedule_meeting(day,time,location="Online"):
returnf"{day} at{time},{location}"

invite=schedule_meeting(time="14:00",day="Friday",location="Room 2B")
print(invite)

Example 3: Call a method, then pass its result into another function

text="  hello world  "
clean=text.strip().title()
print(clean)

parts=clean.split()
print(len(parts))

Example 4: Unpack arguments with * and **

defmultiply(a,b,c):
returna*b*c

values= (2,3,4)
print(multiply(*values))

options= {"b":5,"c":6}
print(multiply(2,**options))

Common mistakes and how to fix them

Mistake 1: Forgetting parentheses

You might write the function name without ():

defdouble(x):
returnx*2

result=double
print(result)

Why it breaks: double is a function object. You did not call it, so the code inside never ran.

Correct approach:

result=double(5)
print(result)

Mistake 2: Passing the wrong arguments

You might pass too many, too few, or the wrong names:

deffull_name(first,last):
returnf"{first}{last}"

print(full_name("Ada"))

Why it breaks: the function expects two arguments, but you only provided one.

Correct approach:

print(full_name("Ada","Lovelace"))

Or modify the function to accept optional parameters:

deffull_name(first,last=""):
returnf"{first}{last}".strip()

print(full_name("Ada"))

Troubleshooting

  • If you see TypeError: ... missing required positional argument, check the function definition and pass all required parameters.
  • If you see TypeError: ... got an unexpected keyword argument, check the parameter name spelling and the function signature.
  • If you see NameError: name 'x' is not defined, define or import the function before calling it, and check scope.
  • If you see None but expected a value, add a return statement, or call a function that returns a value.
  • If you see TypeError: 'str' object is not callable, you overwrote a function name with a variable. Rename the variable.

Quick recap

  • Call a function with name() and pass arguments inside the parentheses.
  • Use positional arguments when order is clear, keyword arguments when readability matters.
  • Store return values in variables, and remember that some functions return None.
  • Methods are functions on objects, called with object.method().
  • Define or import the function before calling it.
  • Use error messages to spot missing arguments, wrong names, or calling something that isn’t callable.