Passing Array as Function Arguments in C | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Introduction

If you want to pass a single-dimension array as an argument in a function, you would have to declare a formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received. Similarly, you can pass multi-dimensional arrays as formal parameters.

Way-1

Formal parameters as a pointer −

void myFunction(int *param) {

   .

   .

   .

}

Way-2

Formal parameters as a sized array −

void myFunction(int param[10]) {

   .

   .

   .

}

Way-3

Formal parameters as an unsized array −

void myFunction(int param[]) {

   .

   .

   .

}

Example
Now, consider the following function, which takes an array as an argument along with another argument and based on the passed arguments, it returns the average of the numbers passed through the array as follows −

double getAverage(int arr[], int size) {

   int i;

   double avg;

   double sum = 0;


   for (i = 0; i < size; ++i) {

      sum += arr[i];

   }

   avg = sum / size;

   return avg;

}

Now, let us call the above function as follows −

#include <stdio.h>

/* function declaration */

double getAverage(int arr[], int size);

int main () {

   /* an int array with 5 elements */

   int balance[5] = {1000, 2, 3, 17, 50};

   double avg;

   /* pass pointer to the array as an argument */

   avg = getAverage( balance, 5 ) ;

   /* output the returned value */

   printf( "Average value is: %f ", avg );

   return 0;

}

When the above code is compiled together and executed, it produces the following result −

Average value is: 214.400000

As you can see, the length of the array doesn't matter as far as the function is concerned because C performs no bounds checking for formal parameters.

The document Passing Array as Function Arguments in C | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Exam

,

study material

,

Passing Array as Function Arguments in C | Programming and Data Structures - Computer Science Engineering (CSE)

,

Objective type Questions

,

ppt

,

Viva Questions

,

practice quizzes

,

pdf

,

Summary

,

shortcuts and tricks

,

Passing Array as Function Arguments in C | Programming and Data Structures - Computer Science Engineering (CSE)

,

Free

,

Passing Array as Function Arguments in C | Programming and Data Structures - Computer Science Engineering (CSE)

,

MCQs

,

mock tests for examination

,

Important questions

,

Semester Notes

,

Previous Year Questions with Solutions

,

Sample Paper

,

Extra Questions

,

past year papers

,

video lectures

;