Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)

9.5 Array Parameters

A function can define a parameter as an array. The array can then be passed as an argument when calling the function. A copy of the array is not actually passed as an argument, only a reference to the array is passed.

To define a function with an array as a parameter, the header of the function definition must indicate this with the parameters KJP keyword. The general KJP syntax for a function header that includes a parameter definition is:

description . . . */ function 〈 function_name 〉 parameters 〈 parameter_list 〉 is . . . endfun 〈 function_name 〉

The parameter_list part of the function header can include one or more array definitions. The array name is followed by an empty pair of brackets.

〈 array_name 〉 []

A function definition that finds the minimum value in an array of type float defines the array as a parameter and an integer parameter. This second parameter is the number of elements in the array. The function returns the minimum value. The complete definition of function minimum in KJP follows.

description This function calculates the minimum value of an array parameter tarray, it then returns the result. */ function minimum of type float parameters float tarray[], integer numel is variables real min // local variable integer j begin set min = tarray[0] for j = 1 to numel - 1 do if min < tarray[j] then set min = tarray[j] endif endfor return min endfun minimum

To call a function and pass an array as an argument, only the name of the array is used. For example, the following KJP code calls function minimum and pass array mtime.

constants integer KNUM = 100 variables float mtime array [KNUM] float mintime . . . set mintime = minimum using mtime, KNUM . . .

The previous code with a call statement is included in another function of the same class. The call to function minimum is carried out in an assignment statement, because the function returns a value that is assigned to variable mintime.

Категории