tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
5
Task/Scope-Function-names-and-labels/0DESCRIPTION
Normal file
5
Task/Scope-Function-names-and-labels/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
The task is to explain or demonstrate the levels of visibility of function names and labels within the language.
|
||||
|
||||
;See also
|
||||
* [[Variables]] for levels of scope relating to visibility of program variables
|
||||
* [[Scope modifiers]] for general scope modification facilities
|
||||
4
Task/Scope-Function-names-and-labels/1META.yaml
Normal file
4
Task/Scope-Function-names-and-labels/1META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Scope
|
||||
note: Basic language learning
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
# This program outputs a greeting
|
||||
BEGIN {
|
||||
sayhello() # Call the function defined below
|
||||
exit
|
||||
}
|
||||
|
||||
function sayhello {
|
||||
print "Hello World!" # Outputs a message to the terminal
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
GOTO 50: REM THIS WILL WORK IMMEDIATELY
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
10 DEF FN S(A)=A*A
|
||||
20 PRINT FN S(2): REM THIS WILL WORK
|
||||
30 PRINT FN C(2): REM CALLING A FUNCTION PRIOR TO DEFINITION MAY NOT WORK
|
||||
40 GOSUB 9000
|
||||
50 PRINT FN C(2): REM THIS WILL WORK
|
||||
60 END
|
||||
9000 DEF FN C(A)=A*A*A
|
||||
9999 RETURN
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
# call a routine before it has been defined
|
||||
say log(); # prints: outer
|
||||
|
||||
# define a subroutine that overrides a CORE function
|
||||
sub log { 'outer' };
|
||||
{
|
||||
# redefine the subroutine in this block
|
||||
sub log { 'inner' };
|
||||
{
|
||||
# redefine the subroutine yet again
|
||||
sub log { 'way down inside' };
|
||||
|
||||
# call it within this block
|
||||
say log(); # prints: way down inside
|
||||
|
||||
# call it from the block one level out
|
||||
say &OUTER::log(); # prints: inner
|
||||
|
||||
# call it from the block two levels out
|
||||
say &OUTER::OUTER::log(); # prints: outer
|
||||
|
||||
# call it from the outermost block
|
||||
say &UNIT::log(); # prints: outer
|
||||
|
||||
# call a subroutine that is post declared in outermost scope
|
||||
outersub()
|
||||
}
|
||||
|
||||
{
|
||||
# subroutine in an inner block that doesn't redefine it
|
||||
# uses definition from nearest enclosing block
|
||||
say log(); # prints: inner
|
||||
}
|
||||
# call it within this block
|
||||
say log(); # prints: inner
|
||||
|
||||
# call it from the block one level out
|
||||
say &OUTER::log(); # prints: outer
|
||||
}
|
||||
|
||||
sub outersub{
|
||||
# call subroutine within this block - gets outer sub
|
||||
say log(); # prints: outer
|
||||
|
||||
# call subroutine from the scope of the callers block
|
||||
say &CALLER::log(); # prints: way down inside
|
||||
|
||||
# call subroutine from the outer scope of the callers block
|
||||
say &CALLER::OUTER::log(); # prints: inner
|
||||
|
||||
# call the original overridden CORE routine
|
||||
say &CORE::log(e); # prints: 1 ( natural log of e )
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
/*REXX program demonstrates use of labels and a CALL. */
|
||||
zz=4
|
||||
signal do_add
|
||||
ttt=sinD(30) /*this REXX statement is never executed.*/
|
||||
|
||||
do_add: /*coming here from the SIGNAL statement.*/
|
||||
|
||||
say 'calling the sub add.2.args'
|
||||
call add.2.args 1,7
|
||||
say 'sum=' result
|
||||
exit /*stick a fork in it, 'cause we're done.*/
|
||||
|
||||
add.2.args: procedure; parse arg x,y; return x+y
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
doFoo 1 2 3; # Will produce an error
|
||||
|
||||
proc doFoo {a b c} {
|
||||
puts [expr {$a + $b*$c}]
|
||||
}
|
||||
doFoo 1 2 3; # Will now print 7 (and will continue to do so until doFoo is renamed or deleted
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
#!/bin/sh
|
||||
multiply 3 4 # This will not work
|
||||
echo $? # A bogus value was returned because multiply definition has not yet been run.
|
||||
|
||||
multiply() {
|
||||
return `expr $1 \* $2` # The backslash is required to suppress interpolation
|
||||
}
|
||||
|
||||
multiply 3 4 # Ok. It works now.
|
||||
echo $? # This gives 12
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
9000 REM The function is immediately visible and usable
|
||||
9010 DEF FN s(x)=x*x
|
||||
|
||||
PRINT FN s(5): REM This will work immediately
|
||||
GO TO 50: REM This will work immediately
|
||||
Loading…
Add table
Add a link
Reference in a new issue