5.5 Data Transfer with Functions


5.5 Data Transfer with Functions

A more complex mechanism in calling a function involves data transfer between the calling and the called function. This data transfer is possible in two directions, from the calling function to the called function and from the called function to the calling function. The simplest data transfer involves value-returning functions in which the direction of the data transfer is from the called function to the calling function.

5.5.1 Value-Returning Functions

In value-returning functions, some value is calculated or assigned to a variable that is returned to the calling function. Note that the return value is a single value; no more than one value can be returned.

5.5.1.1 Defining Value-Returning Functions

A type is defined in the called function, which is the type of the return value. This return value is actually the result produced by the called function. After the called function completes, control is returned with a single value to the calling function. The general structure of a value-returning function in KJP is:

       description            .  .  .         */       function  function_name  of type  return_type  is            .  .  .             return  return_value        endfun  function_name  

The value in the return statement can be any valid expression, following the return keyword. The expression can include constants, variables, object references, or a combination of these.

For example, suppose a function with name get_val displays a message on the console asking for an integer value; this value is read from the console and is returned to the calling function. In the header of the function, the type of the value returned is indicated as integer. The KJP code for this function definition is:

       description          This function asks the user for an integer          value, then this value is returned. */       function get_val of type integer is       variables          integer local_var       begin          display "Please enter an integer value: "          read local_var     // read value from console          return local_var   // return value read       endfun get_val 

Note

One difference of the value-returning function with a simple (or void) function is that the type of the value returned is indicated in the function statement. Another difference is that a return statement is necessary with the value to return.

5.5.1.2 Invoking Value-Returning Functions

The calling function can call a value-returning function, and the value returned can be used in one of the following ways:

  • A simple assignment statement

  • An assignment with an arithmetic expression

In a simple assignment, the value returned by the called function is used by the calling function by assigning this returned value to another variable. For example, suppose function main calls function get_val of an object referenced by myobj. Suppose then that function main assigns the value returned to variable y. The KJP code statement for this call is:

       set y = call get_val of myobj 

This call statement is included in an assignment statement. When the call is executed, the sequence of activities that are carried out is:

  1. Function main calls function get_val in object referenced by myobj.

  2. Function get_val executes when called and returns the value of variable local_var.

  3. Function main receives the value returned from function get_val.

  4. Function main assigns this value to variable y.

Using the value returned in an assignment with an arithmetic expression is straightforward after calling the function. For example, after calling function get_val of the object referenced by myobj, the value returned is assigned to variable y. This variable is then used in an arithmetic expression that multiplies y by variable x and adds 3. The value that results from evaluating this expression is assigned to variable zz. This assignment statement is:

       set zz = x * y + 3 

5.5.2 Functions with Parameters

Most useful functions can receive data values when called from another function. These data values are treated as input values by the called function. The data definitions for these input values are called parameter declarations and are similar to local data declarations. Local data has a local scope and the persistence is for the duration of the function execution. If the called function returns a value, it can be of any legal type but not of type void.

5.5.2.1 Calling Functions with Parameters

The calling function supplies the data values to send to the called function. These values are known as arguments and can be actual values (constants) or names of data items. When there are two or more argument values in the function call, the argument list consists of the data items separated by commas. The KJP statement for a function call with arguments is:

       call  function_name  of  object_ref                  using  argument_list  

For example, consider a call to function min_1 that belongs to an object referenced by obj_a. The function calculates and prints the minimum value of the two given arguments x and y. This call statement in KJP is:

       call min_1 of obj_a using x, y 

Note

The difference with the previous function call is that the name of the object reference is followed by the keyword using and this is followed by the list of arguments, which are values or names of the data items.

5.5.2.2 Defining Functions with Parameters

The definition of the called function includes the declaration of the data items defined as parameters. For every parameter, the function declares a type and a name. The general structure of a function with parameter definition is:

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

The following example uses this syntax structure with a function named min_1. The complete definition of function min_1 in KJP is:

       description         This function calculates the minimum value of          parameters a and b, it then prints the result          on the screen.         */       function min_1 parameters real a, real b is       variables         real min          // local variable       begin         if a < b then           set min = a         else           set min = b         endif         display "Minimum value is: ", min         return endfun min_1 

The definition of function min_1 declares the parameters a and b. These parameters are used as placeholders for the corresponding argument values transferred from the calling function. Figure 5.5 illustrates the calling of function min_1 with the transfer of argument values from the calling function to the called function.

click to expand
Figure 5.5: Transferring arguments in a function call.

In the call to function min_1 shown in Figure 5.5, the value of argument x is assigned to parameter a, and the value of argument y is assigned to parameter b. The arguments and the parameters must correspond in type and meaning, so the order of arguments in the argument list depends on the parameter list definition. The general structure of a value-returning function with parameter definition is:

       description              .  .  .              */       function  function_name  of type  return_type                    parameters  parameter_list  is           .  .  .            return  value_expression        endfun  function_name  

For example, consider a function called min_2 that returns the minimum of two integer values. The KJP definition for this function is:

       description         This function calculates the minimum value of         parameters x and y, it then returns this value. */       function min_2 of type integer parameters integer x,                  integer y is       variables         integer min          // local variable       begin         if a < b then           set min = x         else           set min = y         endif         return min       endfun min_2 

The call to function min_2 should be in an assignment statement, for example, to call min_2 with two constant arguments, a and b, and assign the return value to variable y:

       set y = call min_2 of obj_a using a, b 




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net