Read the Functions section of A Guide to GoScript (along with the rest of that guide) before reading this article.
Defining and Using a Custom Function
You can define your own functions by giving one a unique name:
def myCustomFunction
# contents of myCustomFunction
You can then call your custom function simply by using its name within another function:
def anotherFunction go: https://example.com myCustomFunction
Arguments
An argument is a variable (see GoScript Variables) that you send to a function when you use it. To define a function that accepts an argument, list the argument(s) after the function name seperated by spaces:
def myCustomFunction myFirstArgument mySecondArgument
# This function contains a pair of variables called myFirstArgument and mySecondArgument.
# When used, these variable will contain whatever values you send to them when you invoke
# the function.
To send an argument to a function, type the function name followed by a colon and a space, and then a list of values you want to send to the function seperated by spaces. The number and order of values must match the arguments listed in the function definition:
def anotherFunction go: https://example.com myCustomFunction: someString 123 # The above command invoked the myCustomFunction function. In it, myFirstArgument will have # the value "someString" and mySecondArgument will have the value 123.
Example
def wrapperFunction go: https://www.example.com wait for: Example
replacePageContentsWith: "Hello, World!" wait for: Hello, World!
def replacePageContentsWith newContents js: document.write("<p>{newContents}</p>");
In this example we define two functions, wrapperFunction and replacePageContentsWith. replacePageContentsWith takes one argument: newContents. wrapperFunction invokes replacePageContentsWith and passes it the value "Hello, World!". replacePageContentsWith then runs, and when it uses its variable newContents, it has the value "Hello, World!".
Comments
0 comments
Article is closed for comments.