Calling a function

A user function is called or invoked by typing its name followed by zero or more arguments. If there are two or more arguments these should be separated by commas. The following trivial example illustrates a function call that correctly matches the function definition.


      # function definition
      function ols_ess (series y, list xvars)
        ols y 0 xvars --quiet
        scalar myess = $ess
        printf "ESS = %g\n", myess
        return scalar myess
      end function
      # main script
      open data4-1
      list xlist = 2 3 4
      # function call (the return value is ignored here)
      ols_ess price, xlist

The function call gives two arguments: the first is a data series specified by name and the second is a named list of regressors. Note that while the function offers the variable myess as a return value, it is ignored by the caller in this instance.

(As a side note here, if you want a function to calculate some value having to do with a regression, but are not interested in the full results of the regression, you may wish to use the --quiet flag with the estimation command as shown above.)

A second example shows how to write a function call that assigns return values to variables in the caller:


      # function definition
      function ess_uhat (series y, list xvars)
        ols y 0 xvars --quiet
        scalar myess = $ess
        printf "ESS = %g\n", myess
        series uh = $uhat
        return scalar myess, series uh
      end function
      # main script
      open data4-1
      list xlist = 2 3 4
      # function call
      (SSR, resids) = ess_uhat price, xlist