Call a function: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: removed some blank lines. -- ~~~~)
Line 624: Line 624:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program to demonstrate various methods of calling a REXX function*/
<lang rexx>
/*REXX program to demonstrate various methods of calling a REXX function*/

/*┌────────────────────────────────────────────────────────────────────┐
/*┌────────────────────────────────────────────────────────────────────┐
│ Calling a function that REQUIRES no arguments. │
│ Calling a function that REQUIRES no arguments. │
Line 634: Line 632:
│ (or weren't) passed. │
│ (or weren't) passed. │
└────────────────────────────────────────────────────────────────────┘*/
└────────────────────────────────────────────────────────────────────┘*/

yr=yearFunc()
yr=yearFunc()
say 'year=' yr
say 'year=' yr
Line 642: Line 639:
if arg()\==0 then call sayErr "SomeFunc function won't accept arguments."
if arg()\==0 then call sayErr "SomeFunc function won't accept arguments."
return left(date('Sorted'),3)
return left(date('Sorted'),3)

/*┌────────────────────────────────────────────────────────────────────┐
/*┌────────────────────────────────────────────────────────────────────┐
│ Calling a function with a fixed number of arguments. │
│ Calling a function with a fixed number of arguments. │
Line 652: Line 648:
│ specified (or not). │
│ specified (or not). │
└────────────────────────────────────────────────────────────────────┘*/
└────────────────────────────────────────────────────────────────────┘*/

ggg=FourFunc(12,abc,6+q,zz%2,'da 5th disagreement')
ggg=FourFunc(12,abc,6+q,zz%2,'da 5th disagreement')
say 'ggg squared=' ggg**2
say 'ggg squared=' ggg**2
Line 665: Line 660:
end
end
return a1+a2+a3+a4
return a1+a2+a3+a4

/*┌────────────────────────────────────────────────────────────────────┐
/*┌────────────────────────────────────────────────────────────────────┐
│ Calling a function with optional arguments. │
│ Calling a function with optional arguments. │
Line 672: Line 666:
│ argument (a REXX variable whose value is length zero). │
│ 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.*/
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*/
say 'sum of w, x, y, & z=' SumIt(w,x,y,,z) /*pass 5 args, 4th is null*/
Line 684: Line 677:


return sum
return sum

/*┌────────────────────────────────────────────────────────────────────┐
/*┌────────────────────────────────────────────────────────────────────┐
│ Calling a function with a variable number of arguments. │
│ Calling a function with a variable number of arguments. │
Line 691: Line 683:
│ It's up to the programmer to code how to utilize the arguments. │
│ It's up to the programmer to code how to utilize the arguments. │
└────────────────────────────────────────────────────────────────────┘*/
└────────────────────────────────────────────────────────────────────┘*/

/*┌────────────────────────────────────────────────────────────────────┐
/*┌────────────────────────────────────────────────────────────────────┐
│ Calling a function with named arguments. │
│ Calling a function with named arguments. │
Line 698: Line 689:
│ way this can be accomplished. │
│ way this can be accomplished. │
└────────────────────────────────────────────────────────────────────┘*/
└────────────────────────────────────────────────────────────────────┘*/

what=parserFunc('name=Luna',"gravity=.1654",'moon=yes')
what=parserFunc('name=Luna',"gravity=.1654",'moon=yes')
say 'name=' common.name
say 'name=' common.name
Line 712: Line 702:
end
end
return arg()
return arg()

/*┌────────────────────────────────────────────────────────────────────┐
/*┌────────────────────────────────────────────────────────────────────┐
│ Calling a function in statement context. │
│ Calling a function in statement context. │
Line 719: Line 708:
│ example (above) is calling a function in statement context. │
│ example (above) is calling a function in statement context. │
└────────────────────────────────────────────────────────────────────┘*/
└────────────────────────────────────────────────────────────────────┘*/

/*┌────────────────────────────────────────────────────────────────────┐
/*┌────────────────────────────────────────────────────────────────────┐
│ Calling a function in within an expression. │
│ Calling a function in within an expression. │
Line 725: Line 713:
│ This is a variant of the first example. │
│ This is a variant of the first example. │
└────────────────────────────────────────────────────────────────────┘*/
└────────────────────────────────────────────────────────────────────┘*/

yr=yearFunc()+20
yr=yearFunc()+20
say 'two decades from now, the year will be:' yr
say 'two decades from now, the year will be:' yr
exit
exit

/*┌────────────────────────────────────────────────────────────────────┐
/*┌────────────────────────────────────────────────────────────────────┐
│ Obtaining the return value of a function. │
│ Obtaining the return value of a function. │
Line 735: Line 721:
│ There are two ways to get the (return) value of a function. │
│ There are two ways to get the (return) value of a function. │
└────────────────────────────────────────────────────────────────────┘*/
└────────────────────────────────────────────────────────────────────┘*/

currYear=yearFunc()
currYear=yearFunc()
say 'the current year is' currYear
say 'the current year is' currYear
Line 741: Line 726:
call yearFunc
call yearFunc
say 'the current year is' result
say 'the current year is' result

/*┌────────────────────────────────────────────────────────────────────┐
/*┌────────────────────────────────────────────────────────────────────┐
│ Distinguishing built-in functions and user-defined functions. │
│ Distinguishing built-in functions and user-defined functions. │
Line 749: Line 733:
│ function with the same name (there isn't a penality for this). │
│ function with the same name (there isn't a penality for this). │
└────────────────────────────────────────────────────────────────────┘*/
└────────────────────────────────────────────────────────────────────┘*/

qqq=date() /*number of real dates that Bob was on. */
qqq=date() /*number of real dates that Bob was on. */
say "Bob's been out" qqq 'times.'
say "Bob's been out" qqq 'times.'
Line 756: Line 739:


date: return 4
date: return 4

/*┌────────────────────────────────────────────────────────────────────┐
/*┌────────────────────────────────────────────────────────────────────┐
│ Distinguishing subroutines and functions. │
│ Distinguishing subroutines and functions. │
Line 765: Line 747:
│ it were a subroutine. │
│ it were a subroutine. │
└────────────────────────────────────────────────────────────────────┘*/
└────────────────────────────────────────────────────────────────────┘*/

/*┌────────────────────────────────────────────────────────────────────┐
/*┌────────────────────────────────────────────────────────────────────┐
│ In REXX, all arguments are passed by value, never by name, but it │
│ In REXX, all arguments are passed by value, never by name, but it │
Line 772: Line 753:
│ to retrieve the variable's value. │
│ to retrieve the variable's value. │
└────────────────────────────────────────────────────────────────────┘*/
└────────────────────────────────────────────────────────────────────┘*/

/*┌────────────────────────────────────────────────────────────────────┐
/*┌────────────────────────────────────────────────────────────────────┐
│ In the REXX language, partial application is possible, depending │
│ In the REXX language, partial application is possible, depending │
Line 780: Line 760:
│ are: map (f 7 9) [1..9] │
│ are: map (f 7 9) [1..9] │
│ or: map(f(7,_,9),{1,...,9}) │
│ or: map(f(7,_,9),{1,...,9}) │
└────────────────────────────────────────────────────────────────────┘*/
└────────────────────────────────────────────────────────────────────┘*/</lang>
</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==