Home  >  Article  >  Backend Development  >  How to pass a single element from an array as a parameter to a function in C?

How to pass a single element from an array as a parameter to a function in C?

WBOY
WBOYforward
2023-09-01 22:53:06766browse

How to pass a single element from an array as a parameter to a function in C?

If a single element is to be passed as a parameter, the array element and its subscript must be given in the function call.

To receive these elements, use simple variables in the function definition.

Example 1

#include<stdio.h>
main (){
   void display (int, int);
   int a[5], i;
   clrscr();
   printf (&ldquo;enter 5 elements&rdquo;);
   for (i=0; i<5; i++)
      scanf("%d", &a[i]);
   display (a [0], a[4]); //calling function with individual arguments
   getch( );
}
void display (int a, int b){ //called function with individual arguments
   print f ("first element = %d",a);
   printf ("last element = %d",b);
}

Output

Enter 5 elements
   10    20    30    40    50
First element = 10
Last element = 50

Example 2

Consider another example of passing a single element as a parameter to a function.

#include<stdio.h>
main (){
   void arguments(int,int);
   int a[10], i;
   printf ("enter 6 elements:</p><p>");
   for (i=0; i<6; i++)
      scanf("%d", &a[i]);
   arguments(a[0],a[5]); //calling function with individual arguments
   getch( );
}
void arguments(int a, int b){ //called function with individual arguments
   printf ("first element = %d</p><p>",a);
   printf ("last element = %d</p><p>",b);
}

Output

enter 6 elements:
1
2
3
4
5
6
first element = 1
last element = 6

The above is the detailed content of How to pass a single element from an array as a parameter to a function in C?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete