Passing and returning values

We normally pass variables to a function, and we may refer to them as arguments. Try the example below, where I pass a string to the function.

def name_of_function(first_name):
    """
    Simple test function
    """
    print(f"Yoo hoo, hello {first_name}!")

name_of_function("JOR")

We normally use the keyword return to send the output of a function back to the main program as a variable.

I pass the value 5 to the function and then make the return value equal to a, finally I print a.

def calculate_circumference(radius):
    """
    Calculate the circumference of a circle based on its radius
    """
    return 2 * 3.142 * radius 

a = calculate_circumference(5)
print(a)

If I leave out the value when I call the function, I will get an error:

TypeError: calculate_circumference() missing 1 required positional argument: 'radius'

One way to avoid this would be to use a default value.

I can use the input statement to take a value from the operator. Unfortunately, the input keyword returns a string, you cannot add a string to an integer and a float, I also need to do a conversion from string to float.

There may be cases where you want to pass an unknown number of arguments to a function. We could use the asterisk symbol * for this.

In real coding, we can use the sum() function, we do not need to use a loop. This was a demonstration only! 

Last updated