How to Pass an Array to a Function in Python?

Here, in this article let us look at how to pass an array to a function in Python.

In Python, any type of data, such as a text, list, array, dictionary, and so on, can be given as an argument to a function. When we send a numeric array as an argument to a Python method or function, it is passed by reference.

Function in Python:

A function is a block of code that only executes when it is invoked. Data, known as parameters, can be passed into a function. As a result, a function can return data.

A function is a set of related statements that accomplishes a certain task.

Functions help in the division of our program into smaller, modular parts. As our program increases in size, functions help to keep it organized and manageable.

It also avoids repetition and makes the code reusable.

Passing an Array to a Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import all from array module using the import keyword
  • Create a function say printing_array() which accepts the given aray as an argument
  • Inside the function, loop in the above passed array using the for loop.
  • Print each element of the given array.
  • Pass ‘i’, some random list as arguments to the array() function to create an array.
  • Store it in a variable.
  • Here ‘i’ indicates the datatype of the given array elements is integer
  • Pass the given array as an argument to the above-created printing_array() function to print an array.
  • The Exit of the Program.

Below is the implementation:

# Import all from array module using the import keyword
from array import *
# Create a function say printing_array() which accepts the given aray as an argument
def printing_array(arry): 
    # Inside the function, loop in the above passed array using the for loop
    for k in arry:
        # Print each element of the given array. 
        print(k)

# Pass 'i', some random list as arguments to the array() function to create an array.
# Store it in a variable.
# Here 'i' indicates the datatype of the given array elements is integer
gvn_arry = array('i', [1, 5, 3, 8, 2])
# Pass the given array as an argument to the above created printing_array() function
# to print an array
printing_array(gvn_arry)

Output:

1
5
3
8
2

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import all from array module using the import keyword
  • Create a function say printing_array() which accepts the given aray as an argument
  • Inside the function, loop in the above passed array using the for loop.
  • Print each element of the given array.
  • Give the list as user input using the list(),map(),split(),int functions and store it in a variable.
  • Pass ‘i’, above given list as arguments to the array() function to create an array.
  • Store it in another variable.
  • Here ‘i’ indicates the datatype of the given array elements is integer
  • Pass the given array as an argument to the above-created printing_array() function to print an array.
  • The Exit of the Program.

Below is the implementation:

# Import all from array module using the import keyword
from array import *
# Create a function say printing_array() which accepts the given aray as an argument
def printing_array(arry): 
    # Inside the function, loop in the above passed array using the for loop
    for k in arry:
        # Print each element of the given array. 
        print(k)
        
# Give the list as user input using the list(),map(),split(),int 
# functions and store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass 'i', above given list as arguments to the array() function to create an array.
# Store it in another variable.
# Here 'i' indicates the datatype of the given array elements is integer
gvn_arry = array('i', gvn_lst)
# Pass the given array as an argument to the above created printing_array() function
# to print an array
printing_array(gvn_arry)

Output:

Enter some random List Elements separated by spaces = 11 3 6 2 4
11
3
6
2
4