How to: A simple Python Function example

0

Functions in Python can be simple or extraordinarily complex. This will teach you how to write a simple Python Function example. From there you can go on to build much more complex functions.

Here is an example of a Python function that multiplies an number by itself.

def double(number):
dbl_num = number * number
return dbl_num

def is define. So we are defining our number in Python for this function. The first line stores the number and in the second line we store the number as a new variable called dbl_num. The above function in Python will not do anything by itself. You need an input either defined in your function or asked as an input by the user.

Here is a function you can run and test:

def double(number):
dbl_num = number * number
return dbl_num

number_in = 10
number_out = double(number_in)
print(number_out)

Here is what we just did. This example of a Python function is the same as above what changed is we now have a number to multiple. The number_in is 10. We needed to take that number and put it into our function, thus, double(number_in). We made that equal to a new variable of number_out. Then we printed the number_out which results in 100.

LEAVE A REPLY

Please enter your comment!
Please enter your name here