Writing a Basic Function
This is how a basic is structured in TorqueScript:
function functionName(%arg1,%arg2,%etc)
{
..code..
}
Then we can call upon the said function via another function, or through the console:
functionName(%arg1,%arg2,%etc);
This will run ..code..
function functionName(%arg1,%arg2,%etc)
{
..code..
}
Then we can call upon the said function via another function, or through the console:
functionName(%arg1,%arg2,%etc);
This will run ..code..
Now, let's start off with a Hello World function:
function helloWorld()
{
echo("Hello World");
}
When we call it:
helloWorld();
OUTPUT:
==> "Hello World"
function helloWorld()
{
echo("Hello World");
}
When we call it:
helloWorld();
OUTPUT:
==> "Hello World"
Let's do something a little more complex. Giving our function arguments!
function echoToConsole(%string)
{
echo(%string);
}
This will echo to the console whatever we put as the argument:
echoToConsole("Hello there, I am learning TorqueScript!");
OUTPUT:
==> "Hello there, I am learning TorqueScript!"
We can do math problems too:
echoToConsole(1+1);
OUTPUT:
==> "2"
function echoToConsole(%string)
{
echo(%string);
}
This will echo to the console whatever we put as the argument:
echoToConsole("Hello there, I am learning TorqueScript!");
OUTPUT:
==> "Hello there, I am learning TorqueScript!"
We can do math problems too:
echoToConsole(1+1);
OUTPUT:
==> "2"
Now, let's learn how to return something from our code. We will be doing math.
function doSubtraction(%a,%b)
{
return %a - %b;
}
Now, this function will return the result of whatever we subtract. Let me show you an example:
%result = doSubtraction(10,5);
echo(%result);
OUTPUT:
==> "5"
Let's move on to namespacing.
function doSubtraction(%a,%b)
{
return %a - %b;
}
Now, this function will return the result of whatever we subtract. Let me show you an example:
%result = doSubtraction(10,5);
echo(%result);
OUTPUT:
==> "5"
Let's move on to namespacing.