Example program passing array as function argument in C C++

Example program passing array as function argument in C C++

C Language में Pointer क्या प्रयोग करते हुए किसी Function को कैसे point किया जा सकता है जानने के लिए देखें—Pointer to function

C में फाईल क्या होता है इसे क्यो बनाया जाता है यह कितने प्रकार का होता है जानने के लिए देखें—Introduction to File

C-Language में function recursion किसे कहते है और इसकी क्या उपयोगिता है जानने के लिए देखें— Recursion in C

Example program passing array as function argument in C

#include<stdio.h>
void display(int[],int);
int main()
{
    int a[5],i=0;
    printf("Enter values: ");
    for(i=0;i<5;i++)
    scanf("%d",&a[i]);
    display(a,5);
    return 0;
}
void display(int a[], int n)
{
    int i=0,sum=0;
    for(i=0;i<n;i++)
    {
        printf("Element = %d\n",a[i]);
        sum=sum+a[i];
    }
    printf("Sum = %d",sum);
}

Example program passing array as function argument in C++

#include<iostream>
using namespace std;
void display(int[],int);
int main()
{
    int a[5],i=0;
    cout<<"Enter values: ";
    for(i=0;i<5;i++)
    cin>>a[i];
    display(a,5);
    return 0;
}
void display(int a[], int n)
{
    int i=0,sum=0;
    for(i=0;i<n;i++)
    {
        cout<<"Element = "<<a[i]<<endl;
        sum=sum+a[i];
    }
    cout<<"Sum = "<<sum;
}

Output of Example program passing array as function argument in C C++

Example program for passing array as function argument in C

Other function based example programs in C C++

  1. Sum of two numbers using function
  2. Average of three numbers using function
  3. Swapping using function call by value
  4. Swapping using function call by reference
  5. Passing structure as function argument
  6. Pointer to Function
Share it to:

Published by

admin

I am a computer teacher, programmer and web developer