Breaking News
yeni film izle hd film seyret film izle

Passing Array to Function

Whenever we need to pass a list of elements as argument to the function, it is prefered to do so using an array. But how can we pass an array as argument ? Lets see how to do it.

Declaring Function with array in parameter list

There are two possible ways to do so, one will lead to call by value and the other is used to perform call be reference.

  • We can either have an array in parameter.
    int sum (int arr[]);
  • Or, we can have a pointer in the parameter list, to hold base address of array.
    int sum (int* ptr);

    We will study this in detail later when we will study pointers.

Returning Array from function

We dont’t return an array from functions, rather we return a pointer holding the base address of the array to be returned. But we must, make sure that the array exists after the function ends.

int* sum (int x[])
 return x ;

We will discuss about this when we will study pointers with arrays.

Example : Create a function to sort an Array of elements

void sorting(int x[],int y)
 int i, j, temp ;
 for(i=1; i<=y-1; i++)
  for(j=0; j< y-i; j++)
   if(x[j] > x[j+1])
    temp = x[j];
    x[j] = x[j+1];
    x[j+1] = temp;

In the above example,

  • return type is void, that means function does not return any thing.
  • Sorting is the function name.
  • int x[ ] and int y is the parameter list.
  • int i, j, temp inside curly braces are the local variable declaraction.

Check Also

Program to Remove duplicate Element in an Array

Program to Remove duplicate Element in an Array C program to delete duplicate elements from …