Calling a function
We will first discuss calling a function, which intuitively means using the function.
We have already done this with a few built-in functions.
In the example below, we are calling the function len()
, by passing the argument x
to the function. The function then performs some computation and returns 3 (which is an object), and we then assign 3 to the variable length.
x = [1,5,8]
length = len(x)
A function call can also occur inside another function. The statement below calls the function id()
with the argument x
, and the resulting object (the memory location) is then passed on as the argument to the function print()
. `
x = "composition"
print(id(x))
Note that print()
is a function that does not return anything (more precisely, it returns None
). Whatever it prints out is something it does within the function.
y = print("I return nothing!")
print(type(y))
More built-in functions
Let us call some built-in Mathematical functions: sum()
, min()
, max()
, abs()
, pow()
print(sum([2, 3]))
print(min([4, 1, 7]))
print(max([4, 1, 7]))
print(abs(-4)) # absolute value
print(pow(2, 4)) # 2 to the power of 4
Note that pow()
takes two arguments, the base
and the exponent
. Thus, you can have functions that take multiple input.
A complete list of built-in Python functions is available here