Basics of functions in Python 3

in #python5 years ago

How to define a function is what is covered in this post.

Let us start by using def for writing a function and remember to use indents and lower case. It is to prevent errors.

It is not enough to just define a function. Python will not execute the function unless it is called for. So, a print command alone is not enough.

How to call for is shown in the screenshot below.

Capture.JPG

As you can see, the say_hi is the part that calls for the print to be executed by Python.

Suppose you want to add more information and print it out at the same time. This is called for in the case of large data. '
So, it is just enough to add more information.
Please study the screenshot below.

parameter.JPG

Return statement

The return statement helps you to use Python to execute functions.
Look at how the cube of a number is printed out.

return.JPG

What if we enter result as code before the print statement, we get the same result anyways

resukt.JPG

The return statement sort of ends the code and any command written after it will not get executed. It means that the code is done.

return done.JPG

As you note, the print code was not executed.

All images are screenshots of google colab which is easier to use if you do not want to download Python.

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.031
BTC 63062.73
ETH 2687.13
USDT 1.00
SBD 2.54