Fruitful Functions of Python: Return Values and Incremental Development
Python is known as one of the simplest computer languages to learn, it's because of some fruitful functions involved which make it easier to code.
Today we are going to learn 2 of such functions, Return Values and Incremental Development.
We may have heard about built-in functions for example math functions, which produce results. When we call the function, it generates a value that is assigned to a variable.
e = math.exp(1.0)
height = radius * math.sin(radians
In the above code we have used the math library function but no return values, which results in nothing or void.
Now we introduce return for a code to which shows the result of the area of the circle with a given radius:
Here the return statement includes as an expression. Line number 5 means it will immediately return the result after the functions and use the expression as a return value.
The result variable sometimes makes it more complicated, so now we write this function more concisely…..
Now let's see how to code multiple return statements:
The above code snippet will have only one return statement executed because of the alternative condition.
And this is how the return function works and makes our code more sorted and easier to understand.
As we get smoother in Python the user has to learn more complex problems. Here we learn something known as Incremental Development. Its goal is to avoid long debugging by testing small parts of the code.
Let's take an example of a complex problem, lets find the distance between two points, if co-ordinates given are (x1, y1) and (x2, y2) then using the Pythagorean theorem, the distance is:
To design a python program for this example, we should first think about how the distance function will look like, what input the user will have to give and what are outputs we should expect. Here, the inputs are 2 coordinates and the output or the return value is the distance.
Now let’s create a step-by-step program to design a code for this,
Here line no. 1 has a function named distance and the parameters passes are the coordinates. Thinking of the next step, we will now check the formula. Now we will perform x2 — x1 and y2-y1.
Enter any 2 coordinates and compare them with the expected output.
Later we compute the squares of dx and dy:
Again, we compare with the expected output and test this block.
And now the only thing left is the square root.
Here to calculate the square root we use import math so that all functionalities in math can b easily used here. The math. sqrt is used to calculate the square root for the formula.
In the end, we finally get the answer as expected, and the formula is complete. Line by line we designed and also checked every block and moved forward. This has also prevented long debugging processes.
Some key terms to keep in mind while performing incremental development:
1. Start with a working program and make small changes. At any point, the result doesn’t matches with the output then try to debug it.
2. Use temporary variables that hold the values, here the user can check them and display them.
3. Once the program is completed and runs successfully, the coder should remove all the extras or consolidate multiple statements. But If the coder does not it makes the code difficult to read and understand.