Demonstrates passing multiple variables to a function. - C Function

C examples for Function:Function Parameter

Description

Demonstrates passing multiple variables to a function.

Demo Code

#include <stdio.h>

void changeSome(int i, float *newX, int iAry[5]);

int main()//from w  w  w  .j a  v a  2  s  .com
{
   int i = 10;
   float x = 20.5;
   int iAry[] = { 10, 20, 30, 40, 50 };

   printf("i is %d\n", i);
   printf("x is %.1f\n", x);

   for (int i = 0; i < 5; i++) {
      printf("iAry[%d] is %d\n", i, iAry[i]);
   }

   // passing the value of i and the address of x (hence, the &)

   changeSome(i, &x, iAry);

   printf("i is %d\n", i);
   printf("x is %.1f\n", x);

   for (int i = 0; i < 5; i++) {
      printf("iAry[%d] is %d\n", i, iAry[i]);
   }

   return(0);
}

void changeSome(int i, float *newX, int iAry[5]) {
   i = 42;
   *newX = 92.6; // Same location as x in main

   for (int j = 0; j < 5; j++) {
      iAry[j] = 100 + 100 * j;
   }
   return;
}

Related Tutorials