DEV Community

Cover image for Learn Python #5 - Do with functions...
Nitin Reddy
Nitin Reddy

Posted on • Edited on

3 1

Learn Python #5 - Do with functions...

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
Enter fullscreen mode Exit fullscreen mode
  • 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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️