Before you do anything else, you have to define your function.
The def
keyword tells Python that you are defining a function.
For instance, def func_name(argument1):
tells Python that you are making a function with name func_name
and argument(s) argument1
.
def square(number):
# to print the square of the number, use number**2
print(number**2)
There is another use of function called return
.
def square(number):
# instead, we return the number squared
return number**2
The keyword return
tells Python that this is the value this function gives.
The second one is just like a function in maths, except the square
might be just f
instead:
It is equivalent to $f(x)=x^2$.
Multiple arguments are seperated by a comma, the ()
must be included even if there are no parameters.
You call a function the same way you define it.
Take the square code:
You used square(number)
, so when you call it, use square(5)
to get the square of a number (in this case, 5).
square(37)
Now you are done!!!