Skip to main content
Logo image

Section 19.10 Passing Arrays to Functions

In the following video you will learn how to pass arrays to functions so that within the function you can use as well as modify values stored in the array.

If you cannot see this codecast, please click here.

Check Your Understanding Check Your Understanding

1.

    Suppose an array of integers has been declared using the declaration
    int num[50];
    
    Next, x integers are stored in this array (where x is no larger than 50). The array is to be passed to a function sum which is to add up the x integers stored in the array. What should you be passing to the function sum?
  • The number x of integers stored in the array and a pointer to the first element of num.
  • Correct
  • A pointer to the first element of num.
  • Not quite. Try again!
  • num[0]
  • Not quite. Try again!
  • The 50 elements of num, stored as individual integers.
  • Not quite. Try again!

2.

    Suppose a function sum has been defined as follows:
    int sum(int n, int array[])
    {
               int i;
               int result = 0;
               for (i=0; i<n; i++)
                 result += array[i];
               return(result);
    }
    
    Suppose furthermore that in your main function an array num has been declared and initialized with the following:
    int num[]={4,2,7,5,8,9,4,2,4,6};
    
    What is the correct function call of sum(), using this array?
    Feel free to use the window below to try out some code. Be sure to work the correct answer out "by hand" first before verifying your answer using the code window.

    admin.....open in new window

  • sum(10, num);
    
  • Correct
  • sum(10, num[0]);
    
  • Not quite. Try again!
  • sum(10, array[0]);
    
  • Not quite. Try again!
  • sum(10, *num);
    
  • Not quite. Try again!
  • sum(10, array);
    
  • Not quite. Try again!