7.7 arrays as function arguments

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

int main() { --- const in ARRAY_SIZE = 5; --- int numbers[ARRAY_SIZE] = {1,2,3,4,5}; --- showValues(numbers, ARRAY_SIZE); } void showValues(int nums[ ], int size) { statements } what is this an example of?

1. A function is being called in main that sends an array called numbers, with a size of 5 to the function called showValues. 2. showValues accepts the array and the size. Remember nums can be named anything you want, but it is still the array from main.

What happens in the example of doubling an array value?

1. there are three calls to function is main. One to show the values of the array called set which is the orginial values. One call for the function that doubles the array. And one call to show the values of the double array values. 2. there are two array definitions. The first function definition shows the values of the original array. Then it is used again to show the values of the doubled array. The second function definition double the values of the array.

What happens when a single element of an array is passed to a function?

It is handled like any other variable.

How are arrays treated when passed to a function?

They are treated as reference variables. So whatever changes made to the array by the statement in your function, will be changed in the array in main.

void showValues(int nums[ ], int size) { ---- for (int index = 0; index < size; index++) --------cout << nums[index]; } What is this an example of?

This is a function that accepts an entire array. Notice how the parameter is set up. int nums[ ] indicates that the argument passed will be an array not a single value. the reason there is no size declarator inside the brackets is becasue nums is not actually an array. Its a special variable that can accept the address of an array.

Arrays as function arguments concept:

To pass an array as an argument to a function, pass the name of the array. Quite often, you'll want to write functions that process the data in arrays. for example, function could be written to put values in an array, display an array's contents on the screen, total all of an array's elements, or calculate their average. Usually, such functions accept an array as an argument.

Can we use one function to display 2 different arrays in main?

Yes simply pass each array to the function. the function should have a statement within it to display the arrays.

What happens when we send a whole array to a function?

the array address is used so what changes in the function change what is in the array.

Using const Array parameters What can we use to not allow a function not be able to change the original array?

use const in the parameters ex. void showValues(const int nums[ ], int size) { statements } We will get an error message if the function attempts to make changes to a const. Make sure to use this when we do not want to change our function. Think of the test scores example. They used const int in the function header and returned lowest score.


Set pelajaran terkait

chem 141 practice problems (23-30)

View Set

EXAM 1 | Renaissance through Modern Art NOTES

View Set

AWS Cloud Practitioner Test Questions

View Set

Acts chapters 1-10 all worksheet answers(not finished)

View Set

The Day of the Dead - Your answers (on test) will be in ENGLISH.

View Set

NSG411 Exam 3 Practice Questions

View Set

ATI Critical Care Dosage Calculations

View Set