Legacy - Writing Function Workout
- 01:37
Learn how to build your own functions in PyCharm.
Downloads
Writing Functions - Python Workout FilesTranscript
Let us now look at how to write our own Functions in Python. When writing our own functions, the key thing to follow is to start with a def give the function a name, include some parameters in the function name, and then call the function at the end. So imagine we want to write a function that calculates the square of any number. So we start with a DEF, def, and give the function a name. Because we are trying to calculate the square of any number, we call the function square, and give it a parameter s. Now we want the function to print the square of any number. So it's s star star two, because this is the first time we are running this code, we right click on the tab and press Run. Now, the key thing is we have to call the function, we say square of 2 say because you want to calculate the square of two, we can either use the shortcut which is Shift f10, or use the icon at the top of the screen. 4 Perfect, now, if we want to calculate the square of 45, say, we run the code perfect, it's working. And again, the square of 12. Perfect our function works. Now in case you want to calculate a really complex example, like the net present value of a cash flow. Again, it's really simple. It's just a different formula in the function