Specifying array variables as parameters

  • Syntax to define an array parameter to a function:

      returnType  funcName ( ..  DataType  arrayName[ ],  ..)      
      {
          // function body
      } 

  • Example:   a function that prints the elements in an array

       void printArray( double x[ ], int n )
       {
          for ( int i = 0; i < n; i++ )
       	 printf( "x[%d] = %lf\n", i, x[i] );          
       } 

    Note:

    • Since array variables in C does not array size information, we must pass the array size n as an (extra) parameter

Invoking a function with array variables as parameters

  • Syntax to pass an array as a parameter of a function:

      funcName ( ..  arrayName,  ..)   

  • Example:   calling the printArray function with an array a

    #include <stdio.h>
    
    int main( int argc, char* argv[] )             
    {
        double a[10];
    
        for ( int i = 0; i < 10; i++ )
            a[i] = i;
        
        printArray( a, 10 );   // Array size
    } 

    Note:

    • we must pass the array size (=10) in as an extra parameter

Alternative way to define an array parameter

  • Alternative way to define an array parameter in functions:

      returnType  funcName ( ..  DataType  arrayName[SIZE],  ..)      
      {                                              ^^^^
          // function body               You may specify array size
      } 

    The C compiler will ignore array size when specified (i.e.: treat it like a comment)


  • Example:

       void printArray( double x[10], int n )
       {
          for ( int i = 0; i < n; i++ )
       	 printf( "x[%d] = %lf\n", i, x[i] );          
       } 

    Note: it's your responsibility to pass a correct array size value for n !!!

DEMO: demo/C/set1/array-param1.c

Declaring a function with array parameter

  • Function declaration is necessary when a function is defined after it was used:

    
    
    int main( int argc, char* argv[] )
    {
       double a[10];
    
       printArray( a, 10 );  // Use the printArray function
    }
    
    void printArray(double x[ ], int n) // Function defined after it was used
    {
       int i;
    
       for ( i = 0; i < n; i++ )
          printf( "x[%d] = %lf\n", i, x[i] );
    }

     

Declaring a function with array parameter

  • Easiest syntax of the function declaration with an array variable:

    void printArray( double x[ ], int n ); // Function declaration
    
    int main( int argc, char* argv[] )
    {
       double a[10];
    
       printArray( a, 10 );  // Use the printArray function
    }
    
    void printArray(double x[ ], int n) // Function defined after it was used
    {
       int i;
    
       for ( i = 0; i < n; i++ )
          printf( "x[%d] = %lf\n", i, x[i] );
    }

    (I just copy and paste the function definition and add a semi-colon (;))

DEMO: demo/C/set1/array-param1a.c

Arrays in C are "passed-by-reference"

  • Parameter passing mechanism in C:

      • C programs always use the pass-by-value parameter passing mechanism

  • However:

      • Some expressions in C returns a value of an address

        Example:

            arrayName = address of the array variable 
        

      • When the value of an address is passed (= copied), the effect is like pass-by-reference !

What happens when an array is passed as an argument in C

Consider the following program with a function call with an array and parameter:

 

 

What happens when an array is passed as an argument in C

The array definition will allocate memory to store the array elements:

 

The array name a is equal to the memory address (= reference) where the array was allocated !

What happens when an array is passed as an argument in C

The function call will pass the value of the identifier a - which is the reference to the array:

 

So the effect of passing an array is: pass-by-reference !

What happens when an array is passed as an argument in C

When the array is used inside the function, the expression x[i] will refer to the element a[i] in main( ):

 

So the effect of passing an array is: pass-by-reference !

Program that shows that array parameters are "pass-by-reference" (and can be modified)

   void func( double x[ ] )
   {
       x[0] = 4444;      // WIll change the ORIGINAL !!!
       x[5] = 4444;
   }
     
   int main( int argc, char* argv[] )
   {
      double a[10];
      int    i;
    
      for ( i = 0; i < 10; i++ )
   	 a[i] = i;
    
      printf( "Array before the function call:\n");
      for ( i = 0; i < 10; i++ )
   	 printf( "a[%d] = %lf\n", i, a[i] );
    
      func( a );    // Pass array
    
      printf( "\nArray AFTER the function call:\n");             
      for ( i = 0; i < 10; i++ )
   	 printf( "a[%d] = %lf\n", i, a[i] );
    
   }  

DEMO: demo/C/set1/array-param2.c