Scope/Function names and labels: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Added Perl example)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 187:
 
end</lang>
 
 
=={{header|Erlang}}==
Line 330 ⟶ 329:
Variable not in scope: h :: Integer -> t
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
 
Line 611:
E: This thought intentionally left blank.</pre>
 
=={{header|Perl 6Phix}}==
Functions are private (restricted to a single file) by default, or can be made global by prefixing the definition with the global keyword to make it visible everywhere.
 
=={{header|PL/I}}==
<lang PL/I>
Functions are normally internal to a program. If they are at the nesting level
immediately within the program, they are accessible from anywhere in the program.
 
Functions can also be encapsuled in a package, and the function name exported.
 
Functions can be compiled separately, and then linked with a program
in which case they are globally accessible.
</lang>
 
=={{header|PowerShell}}==
A function exists in the scope in which it was created.
 
If a function is part of a script, the function is available to statements within that script. By default, a function in a script is not available at the command prompt.
 
You can specify the scope of a function. For example, the function is added to the global scope in the following example:
<lang PowerShell>
function global:Get-DependentService
{
Get-Service | Where-Object {$_.DependentServices}
}
</lang>
When a function is in the global scope, you can use the function in scripts, in functions, and at the command line.
 
Functions normally create a scope. The items created in a function, such as variables, exist only in the function scope.
 
For more information about scope in Windows PowerShell, see about_Scopes
<lang PowerShell>
Get-Help about_Scopes
</lang>
 
=={{header|Python}}==
:In Python; our chief creator of new scopes is a function definition ... functions and classes... classes and functions... Our ''two'' creators are functions and classes... and files... Our ''three'' creators are ... I'll come in again.
 
Some (rather dry) rules are:
# All names, (of functions, classes, as well as variables), are scoped in the same way.
# A names scope is its closest enclosing file, function, or class.
# Names belong to the scope where they are assigned-to (or bound)
 
;Cf.:
* Ka-Ping Yee has a tutorial on the above [http://www-inst.eecs.berkeley.edu/~selfpace/cs9honline/Q2/scope.html here].
* This Python Enhancement Proposal: [http://www.python.org/dev/peps/pep-3104/ PEP 3104] introduces the non-local keyword of Python 3.
* And of course, [http://www.youtube.com/watch?v=vt0Y39eMvpI this]!
 
=={{header|Racket}}==
Racket inherits the strict lexical-scopedness of Scheme, so function bindings (like any other bindings) are visible only within their scope. For example
<lang racket>
(define (foo x)
(define (bar y) (+ x y))
(bar 2))
(foo 1) ; => 3
(bar 1) ; => error
</lang>
but that applies only to the *bindings* -- the actual function values (like other values) can be passed around freely:
<lang racket>
(define (foo x)
(define (bar y) (+ x y))
bar)
(foo 1) ; => #<procedure:bar>
((foo 1) 2) ; => 3
</lang>
 
But it should be noted that Racket is flexible enough to make it possible to implement other kinds of scope.
 
=={{header|Raku}}==
(formerly Perl 6)
First a little hand-wavey exposition. The lines are rather blurry in Perl 6 between subroutines, methods, operators and functions. Methods are associated with an object and are inheritable. Subroutines and operators are not. Other than that though there is a lot of overlap. "A function" doesn't really have a specific definition, but is more of a generic term used when talking about code reference type of things.
 
Line 686 ⟶ 755:
 
Labels are less interesting and are typically useful only for control flow in looping constructs. They generally follow the same scoping rules as "my" variables. There is nearly always easier ways to do control flow though, so they aren't heavily used.
 
=={{header|Phix}}==
Functions are private (restricted to a single file) by default, or can be made global by prefixing the definition with the global keyword to make it visible everywhere.
 
=={{header|PL/I}}==
<lang PL/I>
Functions are normally internal to a program. If they are at the nesting level
immediately within the program, they are accessible from anywhere in the program.
 
Functions can also be encapsuled in a package, and the function name exported.
 
Functions can be compiled separately, and then linked with a program
in which case they are globally accessible.
</lang>
 
=={{header|PowerShell}}==
A function exists in the scope in which it was created.
 
If a function is part of a script, the function is available to statements within that script. By default, a function in a script is not available at the command prompt.
 
You can specify the scope of a function. For example, the function is added to the global scope in the following example:
<lang PowerShell>
function global:Get-DependentService
{
Get-Service | Where-Object {$_.DependentServices}
}
</lang>
When a function is in the global scope, you can use the function in scripts, in functions, and at the command line.
 
Functions normally create a scope. The items created in a function, such as variables, exist only in the function scope.
 
For more information about scope in Windows PowerShell, see about_Scopes
<lang PowerShell>
Get-Help about_Scopes
</lang>
 
=={{header|Python}}==
:In Python; our chief creator of new scopes is a function definition ... functions and classes... classes and functions... Our ''two'' creators are functions and classes... and files... Our ''three'' creators are ... I'll come in again.
 
Some (rather dry) rules are:
# All names, (of functions, classes, as well as variables), are scoped in the same way.
# A names scope is its closest enclosing file, function, or class.
# Names belong to the scope where they are assigned-to (or bound)
 
;Cf.:
* Ka-Ping Yee has a tutorial on the above [http://www-inst.eecs.berkeley.edu/~selfpace/cs9honline/Q2/scope.html here].
* This Python Enhancement Proposal: [http://www.python.org/dev/peps/pep-3104/ PEP 3104] introduces the non-local keyword of Python 3.
* And of course, [http://www.youtube.com/watch?v=vt0Y39eMvpI this]!
 
=={{header|Racket}}==
Racket inherits the strict lexical-scopedness of Scheme, so function bindings (like any other bindings) are visible only within their scope. For example
<lang racket>
(define (foo x)
(define (bar y) (+ x y))
(bar 2))
(foo 1) ; => 3
(bar 1) ; => error
</lang>
but that applies only to the *bindings* -- the actual function values (like other values) can be passed around freely:
<lang racket>
(define (foo x)
(define (bar y) (+ x y))
bar)
(foo 1) ; => #<procedure:bar>
((foo 1) 2) ; => 3
</lang>
 
But it should be noted that Racket is flexible enough to make it possible to implement other kinds of scope.
 
=={{header|REXX}}==
Line 833 ⟶ 834:
hello xyz
</pre>
 
=={{header|Scala}}==
<lang Scala>object ScopeFunction extends App {
10,327

edits