Jump to content

Scope/Function names and labels: Difference between revisions

m
no edit summary
No edit summary
mNo edit summary
Line 1,197:
 
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}}==
291

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.