Passing a List as an Argument to a Function in Python:-

As a program grows larger and more complex, it should be broken down into functions that each performs a specific task. This makes the program easier to understand and to maintain.

You can easily pass a list as an argument to a function. This gives you the ability to put many of the operations that you perform on a list in their own functions. When you need to call these functions, you can pass the list as an argument.

Program (specified below) shows an example of a program that uses such a function. The function in this program accepts a list as an argument and returns the total of the list's elements.

(total_function.py)

# This program uses a function to calculate the total of the values in a list #.

def main ( ):

# Create a list.

numbers = [2, 4, 6, 8, 10]

# Display the total of the list elements.

print 'The total is', get-total (numbers)

# The get-total function accepts a list as an argument returns the total of the values in the list #.

def get-total(va1ue-list):

# Create a variable to use as an accumulator.

total = 0

# Calculate the total of the list elements.

for num in value-list:

total += num

# Return the total

return total

Call the main function.

main ()

Output

The total is 30