Skip to main content

Command Palette

Search for a command to run...

Learn Python #5 - Do with functions...

Published
β€’2 min read
Learn Python #5 - Do with functions...
N

Seasoned JavaScript Developer who loves to explore other programming languages like Python. A problem solver, tech lover by heart. Loves reading booking, and cooking. πŸ€©πŸš€πŸ”¬

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.

functions

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 for define 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

More from this blog

N

nitinreddy3

43 posts

Seasoned JavaScript Developer who loves to explore other programming languages like Python. A problem solver, tech lover by heart. Loves reading booking, and cooking. πŸ€©πŸš€πŸ”¬