Array to Function

  • In this module we will learn how to pass array to a function.
  • An entire array can be passed to function as an argument.
  • To pass an array to a function, the array must appear by itself, without brackets and subscript as an actual argument within the Function call.
  • The corresponding formal argument's are written in the same manner though it must be declared as an array within the formal argument declaration.
  • Only passing name means passing the base address of the array.
  • Passing just an array name is same as passing &array_name[0].
We Will Understand the passing of array concept by examples.

Q:define a function 'modify' which will take an array and number of elements in an array as an argument and it will just change the all element to array to -5

definition:
#include<stdio.h>
void modify(int arr[],int n);  //decleration of function.

void modify(int arr[],int n) //definition of function.
{
    for(int i=0;i<i++)
    {//modifying each value of array to -5.
        arr[i] = -5;
    }

}

Using above define Function:
void main()
{
  int arr[10] = {1,2,3,4,5};

  printf("value before calling modify function:\n");
  for(int i=0;i<5;i++)
  {
      printf("%d\t",arr[i]);
  }
      printf("\n");

        //calling function 'modify()'.
modify(arr,5); //Actual parameters.

         printf("values After calling modify function:\n");
  for(int i=0;i<5;i++)
  {
      printf("%d\t",arr[i]);
  }

}

Ouput:
value before calling modify function:
1    2    3    4    5
values After calling modify function:
-5    -5    -5    -5    -5

Q:Define function which will take array and number of element in an array as an argument and it should return the sum of array elements.

definition:
 int sum_array(int arr[],int n); //declaration

 //definition.
 int sum_array(int arr[] , int n) //formal parameters.
 {
     int i,sum = 0;
     for(i=0;i<i++)
     {
         sum = sum + arr[i];
     }
     return sum;
 }

Using above define Function:
 void main()
{
  int arr[10] = {1,2,3,4,5};
  int result;
   
   //Calling function 'sum_array()'
  result = sum_array(arr,5);//formal parameters.
  printf("\nSum of array is %d",result);


}

Ouput:
Sum of array is 15


Q:define a function which will take an string as argument and it should perform operation of reversing the string.

definition:
#include<stdio,h>

void reverse_string(char s[])//declaration

void reverse_string(char s[])//definition
{
    int i,j,len=0;
 char ch;
  for(i=0;s[i]!='\0';i++)
  { //calculating the length.
      len++;
  }
  for(j=0;j<i/2;j++)
  {
      ch = s[j];
      s[j]  = s[(len-1)-j];
      s[(len-1)-j] = ch;
  }
}

Using above define Function:
void main()
{
  char s[30] = "Programmers";
  //calling function 'reverse_string'
  reverse_string(s);//actual parameter.
  printf("reverse string is:");
  puts(s);

}

Ouput:
reverse string is: sremmargorP


Q:define a function which will take an string and a character as an argument and it should perform operation of finding the character 'c' in string 's',return the position of the characters if character is found otherwise return 0.

definition:
int find_char(char s[] ,char c)
{
    int i,result;
    for(i=0; s[i]!='\0'; i++)
    {
        if(s[i] == c)
        {
            result = (i+1);
            break;//breaking loop when character is got.
        }
        else
            result = 0;

    }
    return result;
}

Using above define Function:
 void main()
{  int Position;
  char s[30] = "Programmers are great";

  //calling function 'find_char'
Position = find_char(s,'o');//actual parameters.

 if(Position == 0)
    printf("\nCharacter Not Found");
 else
    printf("\nCharacter was found at %d position.",Position);
}

Ouput:
Character was found at 3 position.






Further Concepts:


People Also Searched: