Top

Function

function is a basic code block, used to perform a task.

You can divide different functions by function, logically each function performs a specified task.

Z1h language standard library provides a variety of built-in functions available For example, the len () function can accept different types of parameters and return the length of the type. If we pass in a string, it returns the length of the string, if we pass in an array, it returns the number of elements contained in the array .

function definition

function is defined in the following format

function name parameter = => { function body }

or

function name = (parameter 1, parameter 2 ...) => { 
function body 
} 

or

func function name (parameter 1, parameter 2 ...) { 
function body 
} 

defined resolved

name description
func func function starting from Declaration
function name function name is the variable name
parameter list of this function parameter is like a placeholder, when the function is called, you can pass the value to the parameter, this value is called the actual parameter. The parameter list specifies It is the parameter type, order, and number of parameters. The parameter is optional, which means that the function can also contain no parameters .
Set of functions defined code function body.

Function call

When creating a function, you define what the function needs to do. Call the function to perform the specified task.

Call the function, pass parameters to the function, and return the value, for example:

func getDouble (num) { 
return num * 2; 
} 

getDouble (100); 
// output 200 

function returns multiple values

Z1h function can return multiple values, the value obtained from the call is an array, for example:

func nameAndSuffix (str) { 
var suffix = $ file.ext (str, ''); 
return (suffix? str [: suffix.length + 1]: str), suffix; 
} 

print (nameAndSuffix ("hello.z1h")); 
// output [ "Hello", ".z1h"] 

[foo, bar] = nameAndSuffix ( 'index.html'); 
Print ( `$ foo} and {bar {} $`) 

exemplary

the Add = e => { 
e + 1 
} 
print (add (3))



plus = (num1, num2) => { 
num1 + num2 
} 
print (plus (3, 7)) 



func minus (n1, n2) { 
n1-(n2 || 0) 
} 
minus (8, 7) 

Automatically closed objects

If an object contains the Close method (including custom structures / map / native objects), you can use this method to automatically close

  • This usage is very similar to Python * // os.Create returns * os.File the type comprising a Close method with Assert (os.Create ( `test.txt`)) AS F { Assert (f.write (" Hello ".bytes)) }

or

with { 
tag: 'Test', 
Close: e => {print (` Close: tag = $ {this.tag}, name = $ {this.name} `)}, 
} as obj { 
obj.name =" haha " 
}