Register to get access to free programming courses with interactive exercises

Function parameters Python Basics

Function parameters

Functions can also accept parameters as well as return parameters. In this lesson, we'll learn how to create these functions.

Remember that we've already encountered function parameters:

# Accepts one parameter of any type as input
print('I am a parameter')
# It takes two string parameters as input
# The first one is what we look for
# The second one is what we replace it with
'google'.replace('go', 'mo')  # moogle
# It takes two numeric parameters as input
# The first one is the number we want to round
# The second one is the number of decimal places that we want to show
round(10.23456, 3)  # 10.235

Now imagine that we need to implement a function called get_last_char() that returns the last character in the string we passed to it as a parameter.

Here is what using this function would look like:

# Passing parameters directly without variables
get_last_char("Hexlet")  # t
# Passing parameters through variables
name1 = 'Hexlet'
get_last_char(name1)  # t
name2 = 'Goo'
get_last_char(name2)  # o

We can draw the following conclusions from this example:

  • We need to define the get_last_char() function
  • The function must accept one parameter, which needs to be a string, as input
  • The function must return a string

Defining the function:

def get_last_char(text):
  return text[-1]

The name of the text variable that serves as a parameter in parentheses. The parameter name can be anything. The main thing is that it reflects the meaning of the value it contains. For example:

def get_last_char(string):
  return string[-1]

The value of the parameter will depend on how we call the function:

# Inside this function, the string will be `'hexlet'`
get_last_char('hexlet')  # t

# Inside this function, the string will be `'code'`
get_last_char('code')  # e

# Inside this function, the string will be `'Winter is coming'`
# The variable name outside the function isn't related
# to the variable name in the function definition
text = 'Winter is coming'
get_last_char(text)  # g

The parameter must be specified. If you call the function without it, the interpreter will give an error:

get_last_char()  # This code is pointless

TypeError: get_last_char() missing 1 required positional argument: 'string'

Many functions work simultaneously with several parameters. For example, to round numbers, you need to specify not only the number itself but also the number of decimal places:

round(10.23456, 3)  # 10.235

The same works with methods. They can require any number of parameters to work:

# The first parameter is what we're looking for
# The second is what we're changing it to
'google'.replace('go', 'mo')  # moogle

To create such functions and methods, you need to specify the required number of parameters, separated by commas, in the definition. They also need to be given clear names.

Below is an example of the definition of a function called replace() that replaces one part of a string with another:

def replace(text, from, to):
  # Here would be the body of the function
  #  We omitted it to keep you focused

replace('google', 'go', 'mo')  # moogle

When there are two or more parameters. The order in which we pass the parameters is important for almost all functions. If you change it, the function will work differently:

# Nothing has been replaced,
# since there is no `mo` inside Google
replace('google', 'mo', 'go')  # google

You now know how to create functions that can take parameters as input.

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY