Previous Page | Next Page

The FCMP Procedure

FUNCTION Statement


Specifies a subroutine declaration for a routine that returns a value.
FUNCTION function-name(argument-1, ..., argument-n) <VARARGS> <$> <length> <KIND | GROUP='string'>;
... more-program-statements ...
RETURN (expression);
ENDSUB;

Arguments

function-name

specifies the name of the function.

argument

specifies one or more arguments for the function. You specify character arguments by placing a dollar sign ($) after the argument name. In the following example,

function myfunct(arg1, arg2 $, arg3, arg4 $);
arg1 and arg3 are numeric arguments, and arg2 and arg4 are character arguments.
VARARGS

specifies that the function supports a variable number of arguments. If you specify VARARGS, then the last argument in the function must be an array.

See: Using Variable Arguments with an Array
$

specifies that the function returns a character value. If $ is not specified, the function returns a numeric value.

length

specifies the length of a character value.

Default: 8
KIND | GROUP='string'

specifies a collection of items that have specific attributes.

expression

specifies the value that is returned from the function.


Details

The FUNCTION statement is a special case of the subroutine declaration that returns a value. You do not use a CALL statement to call a function. The definition of a function begins with the FUNCTION statement and ends with an ENDSUB statement.


Examples


Using Numeric Data in the FUNCTION Statement

The following example uses numeric data as input to the FUNCTION statement of PROC FCMP:

proc fcmp;
   function inverse(in);
      if in=0 then inv=.;
      else inv=1/in;
      return(inv);
   endsub;
run;


Using Character Data in the FUNCTION Statement

The following example uses character data as input to the FUNCTION statement of PROC FCMP. The output from FUNCTION test is assigned a length of 12 bytes.

options cmplib = work.funcs;

proc fcmp outlib=work.funcs.math;
   function test(x $) $ 12;
   if x = 'yes' then
      return('si si si');
      else
      return('no');
   endsub;
run;

data _null_;
   spanish=test('yes');
   put spanish=;
run;

SAS writes the following output to the log:

spanish=si si si


Using Variable Arguments with an Array

The following example shows an array that accepts variable arguments. The example implies that the summation function can be called as follows: sum = summation(1, 2, 3, 4, 5); .

options cmplib=sasuser.funcs;

proc fcmp outlib=sasuser.funcs.temp;
function summation (b[*]) varargs;
    total = 0;
    do i = 1 to dim(b);
        total = total + b[i];
    end;
return(total);
endsub;
sum=summation(1,2,3,4,5);
   put sum=;
run;

Previous Page | Next Page | Top of Page