Keywords: function, call function, input parameters, arguments
By creating and call multiple small functions in Python, these can be used to create large applications. This makes it much easier to program because you do not have to code the same thing several times.
When you want to use a function, it’s called that you call that function. To call a function in Python:
Let’s start by creating a function that calculates the area of a circle.
import math def area_circle(radius): "Return area of a circle" return radius*radius*math.pi
We can test the function by calling it. Examples where the radius of the circle is 4 or 8:
print("The Area of the circle is ", area_circle(4)) print("The Area of the circle is ", area_circle(8))
Resulting in
The Area of the circle is 50.26548245743669 The Area of the circle is 201.06192982974676
Additionally, in a similar way we create a function that calculates the area of a square.
def area_square(x): "Returns the area of a square" return x*x
Furthermore, we can call the function when, for example, the width is 2, or 5:
print ("The area of the square is ", area square (2)) print ("The area of the square is ", area_square (5))
Resulting in
The area of the square is 4 The area of the square is 25
We will use the width of the square and the radius of the circle as inparameters
def largest_area(width, radius): "Returns if the square or circle has the largest area, given the width of the square and the radius of the circle" # Calls the function that calculates the area of the square square = area_square(width) # Calls the function that calculates the area of the circle circle = area_circle(radius) # Determines if the square or circle is largest if square == circle: print("The square and circle are the same size!") elif square > circle: print("The square has a larger area than the circle") else: print("The circle has a larger area than the square") print("The area of the square is", square, "and the area of the circle is", circle)
Note, the highlighted rows in the code above is where we call our previous created functions.
Okay, so let’s try our new function. Say that we call the function, for example, when the width of the square is 4 and the radius of the circle is 3:
largest_area(width=4, radius=3)
Resulting in:
The circle has a larger area than the square The area of the square is 16 and the area of the circle is 28.274333882308138
Now you can use your finished function as many times as you want, without having to program new code! Additionally, you can add new functions. For example, if you want to compare the perimeter
By creating multiple small functions in Python, these can be used to create large applications.
If desired, you can also define default parameters for a function.
def dummy_function(text="Programming in Python"): print("The text that is printed is:", text)
If no input parameter is specified in this case, the input parameter automatically becomes “Programming in Python”.
You call the function without and with the inparameter
dummy_function() dummy_function("New input parameter")
Resulting in
The text that is printed is: Programming in Python The text that is printed is: New input parameter
Please leave feedback and help us continue to make our site better.
How useful was this article?
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?