C Programming

Passing array to function in C

An array is a data structure of identical data items kept in contiguous memory locations that may be accessed randomly utilizing array indices in C or another programming language. They could be used to hold a set of primitive data types of any type, including int, float, double, char, etc. Furthermore, a C array can hold derived data types like structures, pointers, and so on. If you need to keep comparable elements, a C array is useful.

There are a variety of general situations in C that need sending several variables of the identical type to a function. Assume a function that arranges the 30 elements in ascending order; the real parameters from its main function must be passed as 30 numbers to this function. Rather than defining 30 individual numbers and then sending them into the method, we may declare and initialize an array and afterward pass it into it. This removes all of the complications because the function now works with any amount of values.

Our program is simple and manageable due to its functions. Thousands of functions can be found in large applications. We can build functions that take an array as an input to reuse the array operation. We only have to specify the array name inside the function call to provide an array to a function. Arrays, like variables, can be provided as arguments for a function. Let’s begin with the techniques and examples of this tutorial.

Techniques for declaring a function using an array as an argument

There are three approaches to define a function that will take an array as an argument.

Method 1

The syntax of “Method 1” is:

# return_type func(type arr[])
  • arr: name of the array
  • func: name of the function
  • type: data type of array

Method 2

The syntax of “Method 2” is:

# return_type func(type arr[SIZE])
  • arr: name of the array
  • func: name of the function
  • type: data type of array
  • Size can also be specified in subscript format [].

Method 3

The syntax of “Method 3” is:

# return_type func(type *arr)
  • arr: name of the array
  • func: name of the function
  • type: data type of array
  • *: specify the pointer

Note: These examples are implemented on Windows 10 operating system. The execution tool is GCC compiler.

Example 1:

Now we can easily start our first example to get the practical concept of this article in the C programming language. Let’s initiate by generating a file in our GCC compiler. This program is all about finding a minimum number from an array. At the start, we have added a standard input-output library of the C programming language. After that, we have declared an array of int data types. Then we have a variable “j” to which we have assigned a “0” value. For loop has been used in this function. Then we applied another condition to get the minimum number. The function will be closed here.

Then we move towards our main function, where we have declared an array and added a few int-type values into it. Now we are passing an array with the size that we have discussed in method 2. And lastly, we have used the printf() statement to get the minimum value to be displayed in the output.

As soon as you get an understanding of this program, save and close this file. Now run and compile the created file in the GCC compiler. You will get an output alike to the one that is displayed in the screenshot.

Example 2

Now we can move towards our second illustration to get the real concept of this tutorial in the C programming language. Let’s begin by generating a file in our GCC compiler or using the previously created file. This program will calculate the sum of total numbers provided in an array. At the start, we have added a standard input-output library of the C programming language. After that, we have our main() function in which we have declared an array of float data types. You can view that the values are in decimal points.

Then we have a variable “result1” to store the sum of the numbers provided in an array. We have used the printf() statement to display the result of all numbers in the output screen. Now we have closed our main() function. After that, the “For” loop has been used with some conditions. Then we applied a formula to calculate the sum of all numbers.

As soon as you get an understanding of this program, save and close this file. Now run and compile the created file in the GCC compiler. You will get an output identical to the one that is presented in the beneath screenshot.

Conclusion

This article was all about the concept associated with passing an array to a function. We have elaborated a brief concept of arrays in C, and after that, we have listed three different methods to declare a function. We have implemented the two most important examples for a well understanding. You can utilize them in your work, too, by making little modifications.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content