Learn Python #5 - Do with functions...
Nitin Reddy
Posted on July 24, 2022
What functions are?
Functions in Python are meant to isolate the different parts of the program in smaller chunks.
Functions shall be easy to read and understand.
Let us look at an example
Let us say we want to find the volume of a cube so we write
def volume(side):
return side*side*side
print(f"The volume of a cube is {volume(3)} cubic cm")
//The volume of a cube is 27 cubic cm
- Here we say that we want to define a function with the keyword
def
which stands fordefine
in python. -
volume
is a name given to that function. - In parenthesis
()
we are passing an argument which is of type number. - And we end the
()
with:
so that the indentation (4 spaces) will begin from the next line. - return the result with the
return
keyword. - We are calling this method inside a
print
statement where we interpolate the value returned by the function.
Quick gotchas when you are defining a function
- Function name cannot start with numbers or any special characters.
Conclusion
There are more posts on the way related to the functions...So keep reading.
Thanks in advance for reading this article...🚀
More than happy to connect with you on
You can also find me on
💖 💪 🙅 🚩
Nitin Reddy
Posted on July 24, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
tutorial 💡Never forget again: Build a scheduled reminder app in <50 lines of Python using REST and Postgres
October 28, 2024