Scope/Function names and labels: Difference between revisions

m
(→‎{{header|Phix}}: added a note re classes)
m (→‎{{header|Wren}}: Minor tidy)
 
(13 intermediate revisions by 10 users not shown)
Line 9:
* [[Scope modifiers]] for general scope modification facilities
<br><br>
=={{header|6502 Assembly}}==
 
6502 Assembly has no scope rules by default. The entire address space of the CPU is a valid target for the <code>JMP</code> command. The enforcement of scope is up to the assembler itself, which can allow the programmer to define local labels. Prior to conversion into an address, each local label with the same name is padded with a unique sequence of numbers not shown to the programmer, to fulfill the assembler's requirement that all code labels are unique. Assemblers that don't have local label features require that no label be used in more than one place.
=={{header|ALGOL 68}}==
Algol 68 follows the traditional block structure scoping rules introduced by Algol 60.
Line 18 ⟶ 19:
In addition, functions etc. declared between IF and THEN are in scope in the THEN and ELSE parts, but
declarations in the THEN part and not visible in the ELSE part (and vice versa), so the following is invalid:
<langsyntaxhighlight lang="algol68">IF PROC x = ...;
...
THEN
Line 29 ⟶ 30:
...
l1: ...
FI</langsyntaxhighlight>
Similarly, declarations between WHILE and DO are in scope between DO and OD and those declared between CASE and IN are visible in the IN and OUT parts of a CASE.
<br>
Line 40 ⟶ 41:
=={{header|AWK}}==
In awk, function names are always global and can be referenced in sections of code appearing before the definition:
<langsyntaxhighlight lang="awk"># This program outputs a greeting
BEGIN {
sayhello() # Call the function defined below
Line 48 ⟶ 49:
function sayhello {
print "Hello World!" # Outputs a message to the terminal
}</langsyntaxhighlight>
Note that the awk extraction and reporting language is data driven and does not support arbitary line labels.
 
Line 56 ⟶ 57:
=={{header|BASIC}}==
Line numbers are used instead of labels. These are also immediately accessible as soon as the line is entered, even if the program is not run:
<langsyntaxhighlight lang="basic">GOTO 50: REM THIS WILL WORK IMMEDIATELY</langsyntaxhighlight>
The visibility of functions depends on the implementation. Most versions of basic will allow a function to be referenced from a point in the code prior to its definition. However, other implementations may require the function definition to be run, before it can be used:
<langsyntaxhighlight lang="basic">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
Line 65 ⟶ 66:
60 END
9000 DEF FN C(A)=A*A*A
9999 RETURN</langsyntaxhighlight>
 
=={{header|bc}}==
Line 74 ⟶ 75:
There are no labels in bc.
 
<langsyntaxhighlight lang="bc">f(1) /* First output line */
define f(x) {
return(x)
Line 83 ⟶ 84:
return(x - 1)
}
f(3) /* Third output line */</langsyntaxhighlight>
 
{{Out}}
Line 93 ⟶ 94:
Demonstrating function scope as well as goto in a C program invariably leads to code like the one below. The [http://en.wikipedia.org/wiki/Scope_(computer_science)#C Wikipedia article] is a good starting point.
 
<syntaxhighlight lang="c">
<lang C>
#include <stdio.h>
 
Line 144 ⟶ 145:
printf("If you are trying to figure out what happened, you now understand goto.\n");
return 0;
}</langsyntaxhighlight>
 
{{out}} Example run:
Line 160 ⟶ 161:
If you are trying to figure out what happened, you now understand goto.
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
1. Delphi is a one pass compiler, so the names of procedures, functions, variables, constants and types are only visible to code that comes after the item was declared. Procedure and functions can be "Forward" declared, which allows them to be declared before they are defined.
 
2. All items inside an object are visible throughout the object, no matter the order they are defined. Outside an object, the object and items inside it are only visible to code that comes after it was declared.
 
3. Delphi prodedures and functions can be nested. Varibles inside a procedure or function are only visible from inside the same procedures or functions. Variable, procedures and functions at higher levels of nesting are visible to lower levels of nesting.
 
4. Items inside objects have controlled visibility to the outside world. Objects are divided in specific sections that control the visibility of items inside the section. Here is a list of sections:
 
Private: Items contained within the "Private" section of an object are only visible inside the object.
 
Protected: Item contained within the "Protected" section of an object are only visible inside the object or inside object that inherit from the object.
 
Public: Item contained within the "Public" section of an object are completely visible to the outside world.
 
Published: Item contained within the "Published" section of an object are visible to the outside world and can be manipulated by the IDE.
 
 
<syntaxhighlight lang="Delphi">
 
// Test1 is visible to Test2, but not vice versa.
// The local variables A, B, and C invisible to the outside world.
 
procedure Test1;
var A,B,C: integer;
begin
end;
 
procedure Test2;
var A,B,C: integer;
begin
end;
 
 
// Test1 is visible to all code that follows it.
// Test2 is invisible to the ouside world
 
procedure Test1;
var A,B,C: integer;
 
procedure Test2;
var A,B,C: integer;
begin
end;
 
begin
end;
 
 
// Item1 and Test1 are only visible inside the object
// Item2 and Test2 are additional to inhered objects
// Item3 and Test3 are visible to the outside world
// Item4 is visible to the outside world and the IDE
 
 
type TMyObject = class(TObject)
private
Item1: integer
procedure Test1;
protected
Item2: integer
procedure Test2;
public
Item3: integer
procedure Test3;
published
property Item4: integer read FItem4 write FItem4;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
 
</pre>
 
 
=={{header|Eiffel}}==
Line 166 ⟶ 247:
 
All features are assigned (at compile-time) to a particular scope defined by the most-recently preceding feature clause. Various feature clauses are given below, from least restrictive (most visible) to most restrictive (most hidden).
<langsyntaxhighlight Eiffellang="eiffel">--assume A, B and C to be valid classes
class X
feature -- alias for "feature {ANY}"
Line 186 ⟶ 267:
-- features following this clause are only visible to this particular instance of X
 
end</langsyntaxhighlight>
 
=={{header|Erlang}}==
Line 193 ⟶ 274:
There are no labels.
 
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( a_module ).
 
Line 201 ⟶ 282:
 
local_function() -> 2.
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
Each word belongs to a vocabulary. Vocabularies may be added to the vocabulary search path to put their words in scope. For example, to use the <code>+</code> word:
<langsyntaxhighlight lang="factor">USE: math
2 2 +</langsyntaxhighlight>
 
Words are visible in the vocabulary where they are defined. But only after the point where they have been defined.
<langsyntaxhighlight lang="factor">USE: io
IN: hello-vocab
 
hello ! error; hello hasn't been defined yet
: hello ( -- ) "Hello, world!" print ;
hello ! visible here</langsyntaxhighlight>
 
This restriction can be lifted through the use of <code>DEFER:</code>.
 
<langsyntaxhighlight lang="factor">USE: io
IN: hello-vocab
 
Line 224 ⟶ 305:
hello ! visible here
: hello ( -- ) "Hello, world!" print ;
hello ! visible here</langsyntaxhighlight>
 
 
=={{header|FreeBASIC}}==
'''PROCEDURES'''
 
A <code>'''function'''</code> defines a block of code which can be executed with a single statement (a function call), and provide a value back to the caller when finished (a return value):
There are several reasons to use functions
* Reduces redundancy in your program.
* Enables reuse of code in many programs.
* Improves readability of the program.
* Improves maintainability of the program.
* Makes it easy to extend your program.
 
 
A <code>'''subroutine'''</code> is a block of code which may be called at any time from a program.
This code may need to be executed multiple times, and subroutines provide an invaluable means to simplify code by replacing these blocks of code with a single subroutine call.
A subroutine also serves to allow a user to extend the FreeBASIC language to provide custom commands.
Many of the functions built into FreeBASIC are merely subroutines part of a "runtime library" linked to by default.
 
 
Scope (visibility) of a procedure through the different modules of a program.
A procedure is a '''subroutine''' (sub) or a '''function''' that can be called by code outside the procedure (or internal code in the case of a recursion).
A procedure consists of a sequence of instructions that form the body of the procedure.
It is possible to pass values or variables to a procedure, and a function may return a value or a reference.
Scopes of procedures in modules follows simple rules:
# '''Private scope''': procedure visible only in its own module (where it is defined).
# '''Public scope''': procedure visible from all modules constituting a compiled program (including static libraries).
# '''Export scope''': when defined in a DLL (dynamically linked library), procedure visible from an external program that has loaded it (statically or dynamically).
 
 
'''LABELS'''
 
A <code>'''label'''</code> defines a place in a program where Goto or GoSub can jump to.
A label can be a positive integer line number or a symbolname. In both cases, the label must start at the first column of line. A symbolname label must end with a colon (:) character.
Is available only in the "-lang qb" and "-lang fblite" dialect.
 
=={{header|Go}}==
Line 235 ⟶ 351:
A function definition, either a top-level declaration or a function literal, represents a function block.
 
<langsyntaxhighlight lang="go">package main
 
import (
Line 258 ⟶ 374:
 
// ex.x() non-exported function not visible here
}</langsyntaxhighlight>
<langsyntaxhighlight lang="go">package ex
 
import (
Line 275 ⟶ 391:
func x() { // not exported, x not upper case.
panic("top level x")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 283 ⟶ 399:
 
Labels can only be declared in function blocks. The scope is the function block where the label is declared excluding any nested function blocks.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 311 ⟶ 427:
}
 
// end: // labels not allowed outside function blocks</langsyntaxhighlight>
{{out}}
<pre>
Line 320 ⟶ 436:
Functions are considered global in haskell and can be referenced in the sections of code appearing before the function definition. The following code illustrates the same. add2 was declared after add3 and used in add3. The variables x,y and z are local to the function.
 
<langsyntaxhighlight lang="haskell">
add3 :: Int -> Int-> Int-> Int
add3 x y z = add2 x y + z
Line 328 ⟶ 444:
 
main :: putStrLn(show (add3 5 6 5))
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 337 ⟶ 453:
In the function below the functions g and h are local to the function getSquaredSum. They cannot be called from anywhere outside the function.
 
<langsyntaxhighlight lang="haskell">
getSquaredSum :: Int-> Int-> Int
getSquaredSum x y = g x + h y
Line 343 ⟶ 459:
g a = a*a
h b = b*b
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 371 ⟶ 487:
'''Local''' scope, names defined using =. will have local scope if a block scope exists.
 
<langsyntaxhighlight lang="j"> a=. 1</langsyntaxhighlight>
 
'''Locale''' scope, names defined using =: will have locale scope (and the '''base''' locale is used by default).
 
<langsyntaxhighlight lang="j"> b=: 2</langsyntaxhighlight>
 
Names may include a locale qualifier. A locative is a qualified name. Locale qualifiers contain two '''_''' characters, are a suffix on what would be the unqualified name. Locale qualifiers come in two forms: absolute and relative. Relative locale qualifiers use another name in the current locale to identify the target locale. Absolute locatives place the locale name between the two '''_''' characters, while relative locatives name the locale reference following the pair of '''_''' characters.
 
<langsyntaxhighlight lang="j"> c_thingy_=: 3
d=: <'test'
e__d=: 4
b + e_test_
6</langsyntaxhighlight>
 
If a local definition exists for a name, it is an error to use use =: to assign that name (use a locative instead of the unqualified name if you really need to do this).
 
<langsyntaxhighlight lang="j">verb define ''
f=. 6
g=: 7
Line 394 ⟶ 510:
)
|domain error
| g =:9</langsyntaxhighlight>
 
Meanwhile, there is a global current locale (default: '''base''') but each verb's definition is evaluated with the current locale being the locale where that verb was defined.
Line 408 ⟶ 524:
Labels are available in explicit definitions, and are names beginning with '''label_''' and ending with a '''.'''. You may use them with a goto which is a name beginning with '''goto_''' and ending with a '''.'''. Use of labels is restricted to the scope where they are defined, and they cannot be used to enter control structures (except in the sense of the normal entry point).
 
<labelsyntaxhighlight lang="j">example=:3 :0
if. y do.
echo 0
Line 427 ⟶ 543:
example 0
2
5</langsyntaxhighlight>
 
=={{header|jq}}==
Line 446 ⟶ 562:
A similar rule applies to two inner functions defined within the same enclosing function. Otherwise, inner functions of the same
name can co-exist. In particular, a function named NAME may define an inner function of the same name. For example:
<langsyntaxhighlight lang="jq">def NAME:
def NAME: 2;
1, NAME; # this calls the inner function, not the outer function
 
NAME # => 1, 2</langsyntaxhighlight>
 
'''Mutually Defined Functions'''
The "declare-before-use" rule means that two top-level functions cannot be defined in terms of each other. Consider the following example:
<langsyntaxhighlight lang="jq">def F(x): if x == 0 then M(x) else 1 end; # NOT POSSIBLE
def M(x): if x == 1 then F(x) else 2 end;</langsyntaxhighlight>
There are several possible workarounds using inner functions. For example, if both F and M must be top-level functions, we could define them as follows:
<langsyntaxhighlight lang="jq">def F(x):
def M(x): if x == 1 then F(x) else 2 end;
if x == 0 then M(x) else 1 end;
 
def M(x): if x == 1 then F(x) else 2 end;</langsyntaxhighlight>
If F and M are not required to be top-level functions, then both F and M could be defined as inner functions of the same enclosing function.
 
Line 516 ⟶ 632:
 
The following program illustrates some of these usages.
<langsyntaxhighlight lang="scala">// version 1.1.2
 
// top level function visible anywhere within the current module
Line 581 ⟶ 697:
l() // invokes lambda
println("Good-bye!") // will be executed
}</langsyntaxhighlight>
 
{{out}}
Line 597 ⟶ 713:
Good-bye!
</pre>
 
=={{header|Lua}}==
In Lua, variables are global by default, but can be modified to be local to the block in which they are declared.
<syntaxhighlight lang="lua">function foo() print("global") end -- global scope by default
foo()
local function foo() print("local module") end -- local to the current block (which is the module)
foo() -- local obscures the global
_G.foo() -- bug global still exists
do -- create a new block
foo() -- outer module-level scope still visible
local function foo() print("local block") end
foo() -- obscures outer module-level local
local function foo() -- redefine at local block level
print("local block redef")
local function foo() -- define again inside redef
print("local block redef inner")
end
foo() -- call block-level redef inner
end
foo() -- call block-level redef
end -- close the block (and thus its scope)
foo() -- module-level local still exists
_G.foo() -- global still exists</syntaxhighlight>
{{out}}
<pre>global
local module
global
local module
local block
local block redef
local block redef inner
local module
global</pre>
 
=={{header|M2000 Interpreter}}==
There are 5 structures for named routines.
Line 611 ⟶ 761:
There are statements Goto and Gosub as in Basic, including On Goto and On Gosub. We can make simple routines using Return to return from them.We can jump out from a block of code. We can't do the opposite, to enter in a block of code from outside. We can't jump out of Module or Function block, we can exit only using Exit or Break statements (Break exit on multiple blocks, Exit exit the current block). So Goto used to exit from a specific inner block to some block above it.
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Function Master {
Module Alfa {
Line 671 ⟶ 821:
\\ No variables exist after the return from Master()
Print Valid(M)=False
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
In Nim, procedures can be defined at the module level, which is the most frequent, or in any other scope, for instance in another procedure or even in a loop or an if statement. That means that, as regards scoping, procedures are managed as variables, types, etc.
 
When defined at the module level, a procedure is considered private to the module. To make it visible from other modules (provided they import the whole module or only the procedure), the procedure must be annotated with an “*”.
 
Labels are only used when defining a block which opens a new scope: <code>block outer:</code>. This label is only used to allow breaking from the block (useful when we want to exit from internal loops): <code>break outer</code>. The label is only visible in the block.
 
Note that blocks are allowed anywhere where code is allowed, and so are labels. This is perfectly valid:
 
<syntaxhighlight lang="nim"> const C = block useless: 3</syntaxhighlight>
 
=={{header|Objeck}}==
In Objeck function, names may be public or private. Access to the classes that contain functions can be explicit or controlled at the file level by "use" statements.
 
=={{header|Oforth}}==
Line 679 ⟶ 843:
=={{header|Perl}}==
Perl allows various ways to futz with scope, but keeping it simple: Routines are package-scoped, and in each package the final definition of the routine is the one that is used throughout. Labels are generally also package-scoped, except when it comes to <code>goto</code>; let's don't even go there.
<langsyntaxhighlight lang="perl">no warnings 'redefine';
 
sub logger { print shift . ": Dicitur clamantis in deserto." }; # discarded
Line 701 ⟶ 865:
sub logger {
print shift . ": This thought intentionally left blank.\n" # routine for 'main' package
};</langsyntaxhighlight>
{{out}}
<pre>A: This thought intentionally left blank.
Line 710 ⟶ 874:
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
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.<br>
Functions within classes are either private (can only be called from within the class definition) or public (can be called by anything that manages to get it's grubby little mitts on an instance of it). The containing classes themselves can be private or public as above, but if an instance of a private class is handed over, any recipient has full access to anything declared as public within it (but naturally they won't have the ability to create an instance of a private class).
 
There are no labels in Phix except for inline assembly code. Top-level #ilASM{} can use the special syntax :% (also :<, :>, and :!, see pops.e for details) to declare global labels, typically opcodes such as :%opAlloc, otherwise normal labels, declared with :: syntax, are [only] visible across all #ilASM{} in the same file or routine.
 
=={{header|PL/I}}==
<syntaxhighlight lang="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.
Line 722 ⟶ 889:
Functions can be compiled separately, and then linked with a program
in which case they are globally accessible.
</syntaxhighlight>
</lang>
 
=={{header|PowerShell}}==
Line 730 ⟶ 897:
 
You can specify the scope of a function. For example, the function is added to the global scope in the following example:
<syntaxhighlight lang="powershell">
<lang PowerShell>
function global:Get-DependentService
{
Get-Service | Where-Object {$_.DependentServices}
}
</syntaxhighlight>
</lang>
When a function is in the global scope, you can use the function in scripts, in functions, and at the command line.
 
Line 741 ⟶ 908:
 
For more information about scope in Windows PowerShell, see about_Scopes
<syntaxhighlight lang="powershell">
<lang PowerShell>
Get-Help about_Scopes
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
Line 760 ⟶ 927:
=={{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
<langsyntaxhighlight lang="racket">
(define (foo x)
(define (bar y) (+ x y))
Line 766 ⟶ 933:
(foo 1) ; => 3
(bar 1) ; => error
</syntaxhighlight>
</lang>
but that applies only to the *bindings* -- the actual function values (like other values) can be passed around freely:
<langsyntaxhighlight lang="racket">
(define (foo x)
(define (bar y) (+ x y))
Line 774 ⟶ 941:
(foo 1) ; => #<procedure:bar>
((foo 1) 2) ; => 3
</syntaxhighlight>
</lang>
 
But it should be noted that Racket is flexible enough to make it possible to implement other kinds of scope.
Line 800 ⟶ 967:
</pre>
 
<syntaxhighlight lang="raku" perl6line># call a routine before it has been defined
say log(); # prints: outer
 
Line 852 ⟶ 1,019:
# call the original overridden CORE routine
say &CORE::log(e); # prints: 1 ( natural log of e )
}</langsyntaxhighlight>
 
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.
Line 879 ⟶ 1,046:
::::: (optional blanks)
::::::: ────(a repeat of the above as many times as is possible on a line of code)────
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates the use of labels and also a CALL statement. */
blarney = -0 /*just a blarney & balderdash statement*/
signal do_add /*transfer program control to a label.*/
Line 893 ⟶ 1,060:
add.2.args: procedure; parse arg x,y; return x+y /*first come, first served ···*/
add.2.args: say 'Whoa Nelly!! Has the universe run amok?' /*didactic, but never executed*/
add.2.args: return arg(1) + arg(2) /*concise, " " " */</langsyntaxhighlight>
'''output'''
<pre>
Line 901 ⟶ 1,068:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Scope/Function names and labels
 
Line 910 ⟶ 1,077:
func welcome(name)
see "hello " + name + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 920 ⟶ 1,087:
=={{header|Ruby}}==
'def' starts the definition of a method, and 'end' ends it - no cute little curly braces.
<langsyntaxhighlight lang="ruby">
def welcome(name)
puts "hello #{name}"
Line 927 ⟶ 1,094:
$name = STDIN.gets
welcome($name)
return </langsyntaxhighlight>
'''output'''
<pre>
Line 936 ⟶ 1,103:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object ScopeFunction extends App {
val c = new C()
val d = new D()
Line 990 ⟶ 1,157:
println("Good-bye!") // will be executed
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
In Sidef, the same rule which is applied to variable scoping, is applied to functions and classes as well, which means that a function defined inside another function is not visible outside the current scope.
<langsyntaxhighlight lang="ruby"># Nested functions
func outer {
func inner {}; # not visible outside
Line 1,002 ⟶ 1,169:
class Outer {
class Inner {}; # not visisble outside
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
There are a number of different symbol types in Tcl, all of which are handled independently. Each namespace contains a mapping from (simple) command names to command implementations; when a command is looked up, the search is done by looking in the current namespace, then in the namespaces on that namespace's path (which is usually empty), and finally in the global namespace. There are ''no'' local commands (unlike with variables, though a lambda expression in a variable can act very similarly to a command). Commands only have a mapping after they have been created; the <code>proc</code> “declaration” is just a command that creates a procedure at the point where it is called.
<langsyntaxhighlight lang="tcl">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</langsyntaxhighlight>
Tcl does not support labels, either outside or inside procedures. (Other mechanisms are used for jumps and state machines.)
 
Line 1,018 ⟶ 1,185:
There is no lookahead in the shell, so functions cannot be called until their definition has been run:
 
<langsyntaxhighlight lang="sh">#!/bin/sh
multiply 3 4 # This will not work
echo $? # A bogus value was returned because multiply definition has not yet been run.
Line 1,027 ⟶ 1,194:
 
multiply 3 4 # Ok. It works now.
echo $? # This gives 12</langsyntaxhighlight>
 
The shell does not support the use of arbitrary line labels.
 
=={{header|V (Vlang)}}==
Vlang has both functions and labels.
 
A function definition, either a top level declaration or a function literal, represents a function block.
<syntaxhighlight lang="Vlang">
fn world() {
print("World!")
}
 
fn main() {
 
// anonymous function
f := fn() {
print("Hello ")
}
f() // "Hello
world() // World!"
 
// "Hello World!"
}
</syntaxhighlight>
Functions can be used before their declaration (below main), but can still be called from main.
 
This eliminates the need for header files or worrying about the order.
<syntaxhighlight lang="Vlang">
fn main() {
println(add(77, 33))
println(sub(100, 50))
}
 
fn add(x int, y int) int {
return x + y
}
 
fn sub(x int, y int) int {
return x - y
}
</syntaxhighlight>
Functions are private (not exported) by default. To allow other modules to use them, prepend pub
<syntaxhighlight lang="Vlang">
pub fn public_function() {
}
 
fn private_function() {
}
</syntaxhighlight>
Labelled break & continue:
 
You can also use break and continue followed by a label name to refer to an outer for loop.
<syntaxhighlight lang="Vlang">
outer: for i := 4; true; i++ {
println(i)
for {
if i < 7 {
continue outer
} else {
break outer
}
}
}
</syntaxhighlight>
Goto and labels:
 
1) The label name must be contained within the same function as the 'goto' statement.
 
2) Used only with 'unsafe' statements.
<syntaxhighlight lang="Vlang">
// Unsafe 'goto' pseudo example:
if x {
// ...
if y {
unsafe {
goto my_label
}
}
// ...
}
my_label:
</syntaxhighlight>
 
=={{header|Wren}}==
Line 1,039 ⟶ 1,286:
 
The following example illustrates these points.
<langsyntaxhighlight ecmascriptlang="wren">//func.call() /* invalid, can't call func before its declared */
 
var func = Fn.new { System.print("func has been called.") }
Line 1,052 ⟶ 1,299:
}
 
C.init() // fine</langsyntaxhighlight>
 
{{out}}
Line 1,061 ⟶ 1,308:
 
(with line 1 uncommented)
[./scope_function_namesScope_Function_names_and_labels line 3] Error at '}': Variable 'func' referenced before this definition (first use at line 1).
 
(with only line 7 uncommented)
func has been called.
Null does not implement 'init()'.
[./scope_function_namesScope_Function_names_and_labels line 7] in (script)
</pre>
 
=={{header|XPL0}}==
Function names follow the scoping rules of Algol. A function is a
procedure that returns a value.
 
A procedure name is visible from the point it's declared to the end of
the procedure in which its declaration appears. A complete program is a
procedure, informally called "main."
 
A procedure name is also visible in any sub-procedures that might be
nested in the procedure. If the same name is declared at different levels
in nested procedures, the most local declaration is used. Procedures can be nested up to eight levels deep.
 
There are no labels in XPL0 (except for inline assembly code).
 
=={{header|zkl}}==
Line 1,073 ⟶ 1,334:
 
Functions have two modifiers, public (the default) and private. Private is the same as public inside the compilation unit, outside that unit/file, a private function can only be accessed via reflection.
<langsyntaxhighlight lang="zkl">class C{ fcn [private] f{} }</langsyntaxhighlight>
 
=={{header|ZX Spectrum Basic}}==
Functions are visible as soon as they are entered, even if the program is not run. Line numbers are used instead of labels. These are also immediately accessible as soon as the line is entered, even if the program is not run:
<langsyntaxhighlight lang="zxbasic">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</langsyntaxhighlight>
 
{{omit from|GUISS|does not have functions or labels}}
9,476

edits