Register to get access to free programming courses with interactive exercises

Function Signature Python Basics

Today we will learn about functions that raise numbers to power and round them. We will also discuss what a signature is and what parameters are mandatory and optional.

The pow() function

The pow() function expands a number to a power. It takes two parameters:

  • Which number to raise
  • To what power to raise

If you call pow() without parameters, Python outputs the following:

"TypeError: pow expected at least 2 arguments, got 0"

The interpreter tells you that the function expects two parameters and that you called it without them.

The pow() function always has two mandatory parameters, so we cannot call it with any other number of parameters.

Moreover, we have to number the pow() parameters. For example, if you pass a couple of strings into it, it will result in the following error:

"TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'str'"

The result of the function call will also always be a number.

Other functions can have different amounts and types of parameters. For example, it may be a function that takes three parameters: a number, a string, and another number.

To know these details about a particular function, we look at its signature. It defines the input parameters and their types, as well as the output parameter and its type. You can read about the pow() function in the official Python documentation.

Usually, the documentation for functions looks like this:

pow(x, y[, z])

# It returns `x` to the power of `y`
# If `z` is present, returns `x` to the power of `y`, modulus `z`

The first line here is the function signature. The function has two mandatory parameters, x and y. The optional parameter z is given in square brackets.

Next, we see the explanation. The documentation lets you know how many arguments the function has and what type they have. It also describes the result and the type of the return value.

Next, let us look at another function that rounds a number.

The round() fuction

Let us look at the round() function, which rounds a number passed to it:

result = round(10.25, 0)  # 10.0

We passed two parameters to it:

  • The number to round
  • The rounding accuracy

The 0 value means we round the first parameter to an integer. More often than not, you need to round to an integer, not to two or three decimal places. Therefore, the creators of the round function made the second parameter optional and gave it a default value of 0. So, you can choose not to specify the second parameter. The result will be the same:

result = round(10.25)  # 10.0

And if you need a different precision, you can pass a parameter:

# Rounding to one decimal place
result = round(10.25, 1)  # 10.2

If a Python function accepts optional arguments, they are always after the mandatory ones. There can be any number of them. They always go next to and at the end of the list of arguments.

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