Functions

From FizzFuzz

Jump to: navigation, search

In FizzFuzz, functions and methods are defined similiarly to C/C++. This is done by specifying a data type, in addition to any modifiers (such as private, public, etc.), then defining the name and arguments of the method. Functions can be defined globally, or they can be attached to an object, in which case an object is needed to refer to that function (unless the current function is also attached to that object; see below).

Contents

Defining A Function

//Defining a global function.
public var method(var a, var b, ...)
    //Operations.
 
//Defining a class-based function.
class __class__
    public var method(var a, var b, ...)
        //Operations.

Using A Function

Functions and methods are used by referencing them. In most cases, references can be implicit, meaning they will be interpreted to refer to a certain object automatically. Other times, there is ambiguity, and a direct reference must be made to that function or method.

#include <std.dflt.*>
using variable environment
 
//Defines the OutputText() function, which can be accessed
//at any level.
void OutputText(string s)
    outputln("The world says [s].")
 
class __class__
    //Defines the OutputText() method, which can only be accessed
    //by referencing a __class__ instance.
    void OutputText(string s)
        ouputln("This thing says [s].")
 
    void Bloop()
        /*
        self is a keyword which refers to the instance currently
        running. It should be used when there is ambiguity. Note
        that a local method takes precedence over a global one.
        This means that the self keyword can be avoided in most
        cases.
        */
        self.OutputText("Bloop")
 
        /*
        global is similiar to self, except it refers to a variable
        or function defined at the global level. As with self, it
        should be used when there's ambiguity. Any time a local
        method has the same name as a global function, the global
        keyword will have to be used.
        */
        global.OutputText("Bloop")
 
int main()
    __class__ c = new
 
    //This calls the Bloop() method of the __class__ class.
    c.Bloop()
 
    return 1

Overwriting A Method

When dealing with inheritance, often-times methods will be modified or overwritten. This is done by simply using the name of the function in the declaration, without any data type or access modifier.

class __class__
    private int test = 0
 
    public void SetTest(int t)
        test = t
 
class __derived__
    inherit __class__
 
    SetTest(int t)
        test = t * 2

In some cases, performing the operations of the parent type's method may be useful. In this case, the parent keyword may be used.

class __class__
    private int test = 0
 
    public void SetTest(int t)
        test = t
 
class __derived__
    inherit __class__
 
    SetTest(int t, boolean m)
        if(m)
            text = t * 2
        else
            parent.SetText(t)

Special Methods

Two types of special methods exist: constructors and deconstructors. These are used upon the creation and deletion of an instance, respectively. These are indicated by +classname() and -classname(), respectively. Both can take arguments, and both are locked into the void data type, meaning they return nothing.

class __class__
    +__class__(string s)
        environment.outputln("I'm alive! I say, \"[s || "Hello."]\"")
 
    -__class__(string s)
        environment.outputln("I'm dying! I say, \"[s || "Goodbye."]\"")
 
int main()
    __class__ c = new("Bloop.") //This causes the output, 'I'm alive! I say, "Bloop."'
    delete c("Blarg.") //This causes the output, 'I'm dying! I say, "Blarg."'
Personal tools
Navigation
functions
language