Python Function With No Parameters, With Return

In this article, you will learn about functions that does not take any parameters, but does return a value. The returned value can be used for further computation.

Advertisements

Function That Returns Sum of a List of Numbers

In this example, you will create a list of numbers and then sum the numbers in the list and return the value to main program for outputting.

Advertisements
# Program that returns sum of a list of Numbers

my_list = [23,44,66,74,23]

# Function definition 
def add_list():
    sum = 0
    for list_item in my_list:
         sum = sum + list_item
    return sum
# Function call 
result = add_list()
print("The Sum is", result)

The output of the above program is given below. Note that the my_list is global variable, therefore, it is accessible by the function.

== RESTART: C:/Python_projects/Functions/No_Parameter_With_Return.py =====
The Sum is 230
>>> 

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.