# FUNCTION is Invoked in Expression and Returns a Value

### Functions can be called from anywhere in the script. They are locally encapsulated, or they can share some or all variables with other procedures.

• FUNCTION is the initial statement of a function procedure.
• a FUNCTION procedure is invoked in an expression and returns a single result to evaluate the expression.
• with procedure arguments and/or with USE a FUNCTION encapsulates its own variables (makes them local)
• the FUNCTION result must be defined in the FUNCTION body before the FUNCTION completes.
• in HicEst a FUNCTION result is always a numeric scalar.
• recursive calls are not possible
• ⇾ programming (procedures)
• The following are examples of functions and how to call them. The procedures differ in the scope of variables, either local or global
• ! the "main" program does not have an extra declaration
• REAL :: n=5, k=2 ! 2 global variables
• nplusk = SumGlobals() ! returns 7
• anotherGlobalVariable ! was created in SumGlobals and is 25
• binomial_coefficient = Binomial(n, k) ! returns 10
• inserted = FillString(' a b c') ! returns 166667
• blanks = count_blanks() ! returns 5E5
• longstring ! a numeric scalar of value 0 (default). The declaration in FillString is not known here
• END
• FUNCTION SumGlobals()
• ! without dummy arguments or USE all variables are global
• SumGlobals = n + k ! function result, evaluates to 7
• anotherGlobalVariable = n^k ! global variable, evaluates to 25
• END
• FUNCTION Binomial(n, k) ! calls the function Factorial
• Binomial = Factorial(n) / Factorial(k) / Factorial(n-k)
• END
• FUNCTION Factorial(n) ! all variables local to this function:
• ! numeric scalar dummies do not require a declaration
• Factorial = *( 1 : n )
• END
• FUNCTION FillString(text)
• ! with procedure arguments any variables remain local to this function
• CHARACTER text, longstring*1E6 ! a dummy and a 1-megabyte string
• FillString = EDIT(Text=longstring, End, Insert=text, DO=1E6)
• END
• FUNCTION count_blanks()
• USE FillString: longstring ! import a variable
• ! with USE any variables remain local to this function
• count_blanks = EDIT(Text=longstring, End, Count=' ')
• END

Support HicEst   ⇾ Impressum