120 likes | 226 Views
Let’s Look at Some Functions. Boolean Functions – functions that return ‘true’ or ‘false’ Other Functions – some with arguments, some with none. Arrays as Function Arguments.
E N D
Let’s Look at Some Functions Boolean Functions – functions that return ‘true’ or ‘false’ Other Functions – some with arguments, some with none
Arrays as Function Arguments If you have a function that requires an array as its argument, you must indicated to the function (and the compiler) that it is an array. void GetData(int Data[], intsize) { body of GetData } The [] indicates that an array is being passed to the function. When an array is passed to a function THE ADDRESS of the array is what is actually passed. Size is necessary so the function will know how many cells the array being passed contains.
Passing an Array to a Function When you pass an array to a function you include only the name of the array. The size of the array is usually passed as an additional argument. Example: constint CLASS_SIZE=30; int grades[CLASS_SIZE]; GetData(grades, CLASS_SIZE); Note: To pass only one element of an array you must specify the element. This is a pass by value just like any other variable. Example: DisplayHighest(grades[high_grade]);
Pass by Reference When the address of a variable is passed to a function instead of the value of the variable it is called Pass by Reference Functions can change the value of arguments that are passed by reference since the address of the variable is what is passed.
Why Array Size is Not Required in Function Definition(but is usually provided in a second argument) Note: Since arrays are ‘passed by reference’, only the address of the beginning of the array is sent to a function. Since the function knows its data type it can calculate the location of the individual cells. The function can merrily calculate off the end of data storage if the actual size is not passed (or is a global constant) and array bounds are not checked by the function.
A Visual Look at ‘Pass by Reference’ int numbers[3] = {10,30,20}; //sizeof(int) is 4 bytes Memory: x = Biggest(numbers, 3); ************************************************* int Biggest(intnums[ ], intsize) { intlargest; largest = nums[0]; for (int i=1; i < size; i++) if (nums[i] > largest) largest = nums[i] return largest; } What is passed? Biggest(100234, 3)
The Address of a Variable There is an operator is C++ that returns the address of its operand. This is only being introduced so that we can physically look at how memory storage works and better understand how arrays are passed to functions. Note: Your book refers to a section called Reference Variables which look VERY similar. It is not the same thing though it uses the same symbol (&). We will not be covering Reference Variables in this course nor will you be responsible for the ‘address-of’ operator, the ‘&’.
Summary: Arrays as Function Arguments • Are a pass by reference (rather than value). The physical memory address of the start of the array is passed rather than the entire array. • The function calculates the location of specific elements using the size of the data type of the array • You should usually pass the size of the array to the function as well and your function should enforce boundary conditions • Unlike other arguments we previously used, the function being passed the array can alter the contents of the array. • To pass the array simply use its name • To indicate that an argument in a function definition is an array you need to add [] after the argument name.
const Array Parameters (or Arguments) If a function merely needs to look at the elements in an array but its task does not require that it change the array you can declare the argument type as const. Like other constvariables, its value cannot be changed in execution and the function will be unable to change the contents of the array. Syntax: return_typefunction_name(consttypearg_name, … )
More Two-dimensional ArraysExample: int numbers[3][4]; Concept: Remember how 2-dimensional arrays are stored in memory. A two-dimensional array can be thought of as an array of arrays. We used this concept for arrays of C-strings. Array 0 Array 1 Array 2
Passing a Two-Dimensional Arrayto a Function When a two-dimensional array is used as a function argument you MUST specifically declare the number of columns in the definition so the compiler can calculate the start of each row. This can be done with a literal, named integer constant, or global constant. Since C/C++ does no bounds checking it does not need the number of rows (HOWEVER – your function will need it so it can perform the boundary conditions check.)
Example: Function with 2D Array Example function header: void MyFunction(float twoD_array[][20], int size) Note: The number of columns is declared explicitly ************** Example function prototype: void MyFunction(float [][20], int); Note: The number of columns must still be specified. ************** Example function call: MyFunction(my_array, SIZE); Note: Only the name is included in the call. Remember that the address of myarray is what is being passed.