From 1434f48e97a65d255f58b8d4b82c525a89f70f71 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 24 Aug 2022 05:47:36 +0000 Subject: [PATCH] Update 'Functions and Methods' --- Functions-and-Methods.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Functions-and-Methods.md diff --git a/Functions-and-Methods.md b/Functions-and-Methods.md new file mode 100644 index 0000000..f4a7cf2 --- /dev/null +++ b/Functions-and-Methods.md @@ -0,0 +1,38 @@ +Functions are written like this: + +``` +func ro someFunction + > input:Int + < output:Int:mut + --- + somePhrase +``` + +The first part of the function's content is its inputs and outputs. A function +may have any number of these, and they act as local variables. Function inputs +must always be immutable, and function outputs must always be mutable. + +The second part is the actual code of the function. It is separated from the +first part of the function with a separator token, and contains an indented +block of phrases. + +To return data from a function, its outputs must be set as you would a normal +variable. + +A function can be a method of an object if it lists the method receiver after +an @ symbol: + +``` +func ro setGreeting + @ greeter:{Greeter} + > greeting + --- + set greeter.greeting greeting +``` + +Method receivers, like inputs, must be immutable. They also must be pointers. +It is important to note that if a pointer is immutable, the pointer cannot +"move", but the data it points to can still be altered. + +Method receivers can be of any type defined in the same module except interface +types. \ No newline at end of file