lect6_4

lect6_4 - Arrays as Arguments in Functions 1 Arrays as...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Arrays as Arguments in Functions 1 Arrays as Arguments in Functions Any individual element (indexed variable) or the full array itself can be used as an argument in function calls. Array elements as arguments Any array element, such as score[2] , can be an argument to a function in exactly the same way that any variable can be an argument. For example, if void exampleFunction(int); // function prototype is a void function that takes an int argument, then any element of an int type array can be used as an argument in the function call as follows. exampleFunction(score[2]); // function call In general, if i is a valid index (that is, within the array bounds), we can make a function call like this. exampleFunction(score[i]); // function call Depending on the value of i , the corresponding array element score[i] is taken as the argument. Arrays as Arguments in Functions 2 To illustrate this, let us consider the following program. This program uses an array of char type in the main function. The letters in the array are converted to uppercase, one character at a time, using the function changeLetterToUpper . #include<iostream> #include<cctype> using namespace std; void changeLetterToUpper( char &); // function declaration // converts a given character to uppercase void main() { char message[5] = {h, E, l, L, o}; int i; cout << "Original message in the character array = "; for (i = 0; i < 5; i++) { cout << message[i]; } for (i = 0; i < 5; i++) { changeLetterToUpper( message[i] ); // function call } cout << endl << "Changed message in the character array = "; for (i = 0; i < 5; i++) { cout << message[i]; } cout << endl; } // end main function // function definition void changeLetterToUpper( char &ch) { ch = toupper(ch); } // end function changeLetterToUpper Output of the above program. Original message in the character array = hElLo Changed message in the character array = HELLO Arrays as Arguments in Functions 3 Though the program is very simple, it does illustrate how array elements are used as arguments to functions. Note that we supplied one array element at a time to the function changeLetterToUpper . Moreover, we did use the pass by reference method to do so, because we want to change the argument that is passed to the function. We can also pass array elements to functions using the pass by value method exactly the same way. Full array as an argument A functions formal parameter can be an array. As an example, we rewrite the changeLetterToUpper of our previos program, so that the function takes the array as an argument instead of a single element. The declaration for this function is as follows....
View Full Document

Page1 / 9

lect6_4 - Arrays as Arguments in Functions 1 Arrays as...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online