[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Call a Function in Python

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1571670360614{background-color: #f6f6f6 !important;}”]

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.

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]We will now create several different functions and then call (use) them. We will code a program that determines what has the largest area of a circle or square. The functions will be independent of each other, making it easy to call the functions when you need them.[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]

By creating multiple small functions in Python, these can be used to create large applications.

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]

Example on how to Create and Call Functions in Python

 

Let’s start by creating a function that calculates the area of a circle.

  • The only input parameter that the function will have is the radius.
  • So we send in the radius and will receive the calculated area back.

 

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.

  • Input is the width of the square
  • Get back the area of the 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

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]Let’s use these two functions in a third function that determines if the square or circle has the largest area.

  • 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[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal”][vc_empty_space height=”10px”][vc_column_text]

DEFINE STANDARD PARAMETERS for functions in Python

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

[/vc_column_text][vc_empty_space height=”30px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]