Call a function: Difference between revisions

Content deleted Content added
Latitude language added
→‎version 1: added more comments, changed some comments, added whitespace and indentation of boxed comments, created separate program elements, aligned structures and statements.
Line 4,369:
=={{header|REXX}}==
===version 1===
<lang rexx>/*REXX programpgms to demonstratedemonstrates various methods/approaches of invoking/calling a REXX function.*/
/*┌────────────────────────────────────────────────────────────────────┐
│ Calling a function that REQUIRES no arguments. │
│ │
│ In the REXX language, there is no way to require the caller to not │
│ pass arguments, but the programmer can check if any arguments were │
│ (or weren't) passed. │
└────────────────────────────────────────────────────────────────────┘*/
yr=yearFunc()
say 'year=' yr
exit
 
/*╔════════════════════════════════════════════════════════════════════╗
yearFunc: procedure
if arg()\==0 then call sayErr "SomeFunc ║ Calling a function won'tthat acceptREQUIRES no arguments."
║ ║
return left(date('Sorted'),3)
║ In the REXX language, there is no way to require the caller to not ║
/*┌────────────────────────────────────────────────────────────────────┐
║ pass arguments, but the programmer can check if any arguments were ║
│ Calling a function with a fixed number of arguments. │
(or weren't) passed.
╚════════════════════════════════════════════════════════════════════╝*/
│ I take this to mean that the function requires a fixed number of │
│ arguments. As above, REXX doesn't enforce calling (or invoking) │
│ a (any) function with a certain number of arguments, but the │
│ programmer can check if the correct number of arguments have been │
│ specified (or not). │
└────────────────────────────────────────────────────────────────────┘*/
ggg=FourFunc(12,abc,6+q,zz%2,'da 5th disagreement')
say 'ggg squared=' ggg**2
exit
 
yr= yearFunc() /*the function name is caseless if it isn't */
FourFunc: procedure; parse arg a1,a2,a3; a4=arg(4) /*another way get a4*/
/*enclosed in quotes (') or apostrophes (").*/
say 'year=' yr
exit /*stick a fork in it, we're all done. */
 
yearFunc: procedure /*function ARG returns the # of args.*/
if arg()\==4 then do
errmsg= '***error***' /*an error message eyecatcher string. */
call sayErr "FourFunc function requires 4 arguments,"
if arg() \== 0 then say errmsg call sayErr "butthe insteadYEARFUNC itfunction found"won't arg()accept 'arguments.'"
return left( date('Sorted'), exit 133)</lang>
end
return a1+a2+a3+a4
/*┌────────────────────────────────────────────────────────────────────┐
│ Calling a function with optional arguments. │
│ │
│ Note that not passing an argument isn't the same as passing a null │
│ argument (a REXX variable whose value is length zero). │
└────────────────────────────────────────────────────────────────────┘*/
x=12; w=x/2; y=x**2; z=x//7 /* z is x modulo seven.*/
say 'sum of w, x, y, & z=' SumIt(w,x,y,,z) /*pass 5 args, 4th is null*/
exit
 
SumIt: procedure; sum=0
 
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
do j=1 for arg()
║ Calling a function with a fixed number of arguments. ║
if arg(j,'E') then sum=sum+arg(j) /*the Jth arg may have been omitted*/
║ ║
end
║ I take this to mean that the function requires a fixed number of ║
║ arguments. As above, REXX doesn't enforce calling (or invoking) ║
║ a (any) function with a certain number of arguments, but the ║
║ programmer can check if the correct number of arguments have been ║
║ specified (or not). ║
║ In some languages, these are known as "generic" functions. ║
╚════════════════════════════════════════════════════════════════════╝*/
 
ggg= FourFunc(12, abc, 6+q, zz%2, 'da 5th disagreement')
return sum
say 'ggg squared=' ggg**2
/*┌────────────────────────────────────────────────────────────────────┐
exit /*stick a fork in it, we're all done. */
│ Calling a function with a variable number of arguments. │
 
│ │
FourFunc: procedure; parse arg a1,a2,a3 /*obtain the first three arguments. */
│ This situation isn't any different then the previous example. │
a4= arg(4) /*another way to obtain the 4th arg. */
│ It's up to the programmer to code how to utilize the arguments. │
errmsg= '***error***' /*an error message eyecatcher string. */
└────────────────────────────────────────────────────────────────────┘*/
if arg() \== 4 then do
/*┌────────────────────────────────────────────────────────────────────┐
Calling a function with named arguments. say err "FourFunc function requires 4 arguments,"
say err "but instead it found" arg() 'arguments.'
exit 13 /*exit function with a RC of 13*/
│ REXX allows almost anything to be passed, so the following is one │
way this can be accomplished. end
 
└────────────────────────────────────────────────────────────────────┘*/
return a1 + a2 + a3 + a4</lang>
what=parserFunc('name=Luna',"gravity=.1654",'moon=yes')
 
say 'name=' common.name
 
gr=common.gr
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
║ Calling a function with optional arguments. ║
║ ║
║ Note that not passing an argument isn't the same as passing a null ║
║ argument (a REXX variable whose value is length zero). ║
╚════════════════════════════════════════════════════════════════════╝*/
 
x= 12; w= x/2; y= x**2; z= x//7 /* z is x modulo seven. */
say 'sum of w, x, y, & z=' SumIt(w,x,y,,z) /*pass five args, the 4th arg is "null"*/
exit /*stick a fork in it, we're all done. */
 
SumIt: procedure
$= 0 /*initialize the sum to zero. */
do j=1 for arg() /*obtain the sum of a number of args. */
if arg(j,'E') then $= $ + arg(j) /*the Jth arg may have been omitted. */
end /*j*/
 
return $</lang>
 
 
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
║ Calling a function with a variable number of arguments. ║
║ ║
║ This situation isn't any different then the previous example. ║
║ It's up to the programmer to code how to utilize the arguments. ║
╚════════════════════════════════════════════════════════════════════╝*/
 
/*╔════════════════════════════════════════════════════════════════════╗
║ Calling a function with named arguments. ║
║ ║
║ REXX allows almost anything to be passed, so the following is one ║
║ way this can be accomplished. ║
╚════════════════════════════════════════════════════════════════════╝*/
 
what= parserFunc('name=Luna', "gravity=.1654", 'moon=yes')
say 'name=' common.name
gr= common.gr
say 'gravity=' gr
exit /*stick a fork in it, we're all done. */
exit
 
parseFunc: procedure expose common.
do j=1 for arg()
parse var arg(j) name '=' val
upper name /*uppercase it.*/
upper name
call value 'COMMON.'name,val
end
return arg()</lang>
 
/*┌────────────────────────────────────────────────────────────────────┐
 
│ Calling a function in statement context. │
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
│ │
║ Calling a function in statement context. ║
│ REXX allows functions to be called (invoked) two ways, the first │
║ ║
│ example (above) is calling a function in statement context. │
║ REXX allows functions to be called (invoked) two ways, the first ║
└────────────────────────────────────────────────────────────────────┘*/
║ example (above) is calling a function in statement context. ║
/*┌────────────────────────────────────────────────────────────────────┐
╚════════════════════════════════════════════════════════════════════╝*/
│ Calling a function in within an expression. │
 
│ │
/*╔════════════════════════════════════════════════════════════════════╗
│ This is a variant of the first example. │
║ Calling a function in within an expression. ║
└────────────────────────────────────────────────────────────────────┘*/
║ ║
yr=yearFunc()+20
║ This is a variant of the first example. ║
╚════════════════════════════════════════════════════════════════════╝*/
 
yr= yearFunc() + 20
say 'two decades from now, the year will be:' yr
exit /*stick a fork in it, we're all done. */</lang>
exit
 
/*┌────────────────────────────────────────────────────────────────────┐
 
│ Obtaining the return value of a function. │
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
│ │
There are two ways to get ║ Obtaining the (return) value of a function.
║ ║
└────────────────────────────────────────────────────────────────────┘*/
║ There are 2 ways to get the (return) value (RESULT) of a function. ║
currYear=yearFunc()
╚════════════════════════════════════════════════════════════════════╝*/
say 'the current year is' currYear
 
currYear= yearFunc()
say 'the current year is' currYear
 
call yearFunc
say 'the current year is' result /*result can be RESULT, it is caseless.*/</lang>
 
/*┌────────────────────────────────────────────────────────────────────┐
 
│ Distinguishing built-in functions and user-defined functions. │
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
│ │
║ Distinguishing built-in functions and user-defined functions. ║
│ One objective of the REXX language is to allow the user to use any │
║ ║
│ function (or subroutine) name whether or not there is a built-in │
║ One objective of the REXX language is to allow the user to use any ║
│ function with the same name (there isn't a penality for this). │
║ function (or subroutine) name whether or not there is a built-in ║
└────────────────────────────────────────────────────────────────────┘*/
qqq=date() function with the /*numbersame ofname real dates(there thatisn't Boba waspenality onfor this). */
╚════════════════════════════════════════════════════════════════════╝*/
say "Bob's been out" qqq 'times.'
 
www='DATE'('USA') /*returns date in format mm/dd/yyy */
exit /*anydate: function as in quotesgoing is external.out with someone. */
qqq= date() /*number of real dates that Bob was on.*/
/*hopefully, it accurately counts dates*/
say "Bob's been out" qqq 'times.'
www= 'DATE'("USA") /*returns date in format mm/dd/yyyy */
/*any function in quotes is external. */
exit /*stick a fork in it, we're all done. */
 
date: return 4 /*Bob only "went out" 4 times, no need */
/* to actually count, he quit after 4. */</lang>
 
 
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
║ Distinguishing subroutines and functions. ║
║ ║
║ There is no programmatic difference between subroutines and ║
║ functions if the subroutine returns a value (which effectively ║
║ makes it a function). REXX allows you to call a function as if ║
║ it were a subroutine. ║
╚════════════════════════════════════════════════════════════════════╝*/
 
/*╔════════════════════════════════════════════════════════════════════╗
║ In REXX, all arguments are passed by value, never by name, but it ║
║ is possible to accomplish this if the variable's name is passed ║
║ and the subroutine/function could use the built-in-function VALUE ║
║ to retrieve the variable's value. ║
╚════════════════════════════════════════════════════════════════════╝*/
 
/*╔════════════════════════════════════════════════════════════════════╗
date: return 4
║ In the REXX language, partial application is possible, depending ║
/*┌────────────────────────────────────────────────────────────────────┐
Distinguishing subroutines and functions. how partial application is defined; I prefer the 1st definition
(as per the "discussion" for "Partial Function Application" task:
║ 1. The "syntactic sugar" that allows one to write some examples ║
│ There is no programatic difference between subroutines and │
║ are: map (f 1 9) [1..9] ║
│ functions if the subroutine returns a value (which effectively │
║ or: map (f(1,_,9)) [1, ..., 9] ║
│ makes it a function). REXX allows you to call a function as if │
╚════════════════════════════════════════════════════════════════════╝*/</lang>
│ it were a subroutine. │
└────────────────────────────────────────────────────────────────────┘*/
/*┌────────────────────────────────────────────────────────────────────┐
│ In REXX, all arguments are passed by value, never by name, but it │
│ is possible to accomplish this if the variable's name is passed │
│ and the subroutine/function could use the built-in-function VALUE │
│ to retrieve the variable's value. │
└────────────────────────────────────────────────────────────────────┘*/
/*┌────────────────────────────────────────────────────────────────────┐
│ In the REXX language, partial application is possible, depending │
│ how partial application is defined; I prefer the 1st definition (as│
│ (as per the "discussion" for "Partial Function Application" task: │
│ 1. The "syntactic sugar" that allows one to write (some examples│
│ are: map (f 7 9) [1..9] │
│ or: map(f(7,_,9),{1,...,9}) │
└────────────────────────────────────────────────────────────────────┘*/</lang>
 
===version 2===