Home > Article > Backend Development > 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.
#include<stdio.h> main (){ void display (int, int); int a[5], i; clrscr(); printf (“enter 5 elements”); 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); }
Enter 5 elements 10 20 30 40 50 First element = 10 Last element = 50
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); }
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!