You are on page 1of 6

Function

Passing Array to Function


• An array can be passed to a called function by just
giving a name of the parameter for that called
function.
• Example:
int n[10]; //array with 10 elements
int sum(n);
Here we only pass array name ‘n’ in sum function.
n means starting index is n[0].
Write a C program to enter a marks of n students and display
them.
#include<stdio.h> void enter(int a[],int n)
#include<conio.h> {
void enter(int a[], int n); int i;
void display(int a[], int n); for(i=0;i<n;i++)
void main()
{
{
int marks[50],n; printf(“\n Enter marks of roll
printf(“Enter the number of
no=%d”,i+1);
students:”); scanf(%d”,&a[i]);
scanf(“%d”,&n); }
enter(marks,n); }
display(marks,n);
getch();
}
void display(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf(“\n Roll no %d Marks=%d”,i+1,a[i]);
}
}
Write a C program to pass a single element of an array
to function.
#include<stdio.h>
#include<conio.h>
void display(int age)
{
printf(“%d”,age);
}
void main()
{
int agearray[]={2,3,4};
display(agearray[2]);
getch();
}

Output: 4
Write a C program to pass a elements in two
dimensional array.
#include<stdio.h> void displaynumbers(int num[2][2])
#include<conio.h> {
void displaynumbers(int num[2][2]) int I,j;
void main() printf(“ Matrix elements are:”);
{ for(i=0;i<2;i++)
int num[2][2],i,j; {
printf(“Enter 4 numbers of matrix:”); for(j=0;j<2;j++)
for(i=0;i<2;i++) printf(“%d”,num[i][j]);
{ printf(“\n”);
for(j=0;j<2;j++) }
scanf(“%d”,&num[i][j]);
}
displaynumbers(num);
getch();
}

You might also like