Home > Backend Development > C++ > 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
Release: 2023-09-01 22:53:06
forward
823 people have browsed it

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);
}
Copy after login

Output

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

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);
}
Copy after login

Output

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template