Exercise: writing a function

Write a function named AVG that receives a list of numbers separated by blanks and computes their average. The final answer can be a decimal number. To call this function, you would use:
AVG(number1
number2 number3…)

Use the WORDS (see section WORDS ) and WORD (see section WORD ) built-in functions.

ANSWER
Figure 1. possible solution
/******************************* REXX
********************************/
/* This function receives a list of numbers, adds them, computes */
/* their average, and returns the average to the calling
program
. */
/*********************************************************************/

ARG numlist /* receive the numbers in a single variable */

sum = 0 /* initialize sum to zero */

DO n = 1 TO WORDS(numlist) /* Repeat for as many times as there */
/* are numbers */

number = WORD(numlist,n) /* Word #n goes to number */
sum = sum + number /* Sum increases by number */
END

average = sum / WORDS(numlist) /* Compute the average */

RETURN average


dfhrx00058.html | Timestamp icon Last updated: Monday, 5 June 2017