Ok, this lesson will teach you interesting things about functions. Once you got variables and functions, you have a basic understanding of how a script works.
What are these functions, anyway? Let’s see a small example:
function add_numbers( )
{
var a, b, c;
a = 3;
b = 5;
c = a + b;
}
Do you see what I see? A function is nothing more than a collection of C commands that are executed by the computer one after the other. Let’s see some properties for these functions:
► A function is normally defined using the word [B]function [/B]followed by the name of the function and a pair of parentheses B[/B]. The parentheses are used to pass additional variables to the function; we’ll learn about that later. In our case we don’t pass any variables, so they are empty.
► The [B]body [/B]of the function (its list of commands) must be written inside a pair of winged brackets [B]{ }[/B]. The body consists of one or more lines of lite-C code that end with a semicolon. For clarity, programmers usually indent the code in the function body by some spaces or a tab, for making clear that it is inside something.
► The names used for the functions follow the same naming convention as for variables. You shouldn’t use the same name for a variable and a function; this will lead to errors.
If you can read this tutorial I hope that you know your age, too. Not in years, but in days! What, you don’t know it? Ok, so let’s try to write a function that computes the number of days spent by me (or you) on Earth.
I know how to start! I write the keyword [B]function [/B]and then the name of the function; let’s name it compute_days:
[B]function compute_days()
{[/B]
I haven’t forgotten the parenthesis after the name of the function and I have added the first curly bracket!
We will use some variables, so we’d better define them now:
[B]var my_age = 33; // your age (in years) goes here
var days_a_year = 365.25;[/B]
Nothing new so far, right? We have defined two [B]var [/B]variables and they have received initial values, because I know my age in years and I also know that every year has about [B]365.25[/B] days. Now comes the scary part: how will I be able to tell the computer to compute the number of days? How would I do it with a pocket calculator? I would enter something like this:
[B]33 * 365.25 =[/B]
Now let’s take a look at our variables; if I replace [B]33[/B] with [B]my_age [/B]and [B]365.25 [/B]with [B]days_a_year[/B], I will get something like this:
[B]number_of_days = my_age * days_a_year;[/B]
Ok, so our function should end like this:
[B]var number_of_days = my_age * days_a_year;
printf(“I am %.f days old!”,number_of_days);
}[/B]
I have remembered to add the second curly bracket, so now the body of the function is enclosed by the two required curly brackets. I am really curious to see if this function works, so let’s test it. Fire up Zorro, and then select [New Script] in the Script list. Wait until the editor opens. Then copy and paste the lines below into the editor window. Select the entire script below with your mouse, right click and choose Copy (or hit [Ctrl-C]), switch to the editor, right click into the empty window named “script1”, then choose Paste:
function compute_days()
{
var my_age = 33;
var days_a_year = 365.25;
var number_of_days = my_age * days_a_year;
printf("I am %.f days old!",number_of_days);
}
The code looks pretty simple, doesn’t it? We already know how to work with those variables, we know how to add comments… So let’s save it (File / Save As) into the Strategy folder of the Zorro installation, under a name like [B]myscript2.c[/B]. Don’t forget the “[B].c[/B]” at the end - it means that this file contains C code.
If you did everything right, you should now find [B]myscript2[/B] in the Script list. Select it. Time to [Test] our script:
Does this error message mean that a script always need a [B]main()[/B] or [B]run()[/B] function? Yes, main is a predefined function name. If a function is named [B]main[/B], it will automatically run when we start our script. The function named [B]run [/B]is special to Zorro; it contains our trade strategy and is automatically run once for every time period. If a script has neither a [B]main [/B]nor a [B]run [/B]function, Zorro assumes that you made a mistake and will give you this error message.
Now, let’s enter a main function at the end of the script:
function main()
{
compute_days();
}
The way I see it, this code ‘[B]calls[/B]’ (meaning it starts) our [B]compute_days [/B]function. Ok, now that we are here let’s see how we call a function: we write its name followed by a pair of parenthesis and then we end the line of code with a semicolon. Sounds logical, doesn’t it?
Important tip: write the lines of code for your functions first and call them later. The computer reads the code the same way you read a book: it starts with the top of the script page and goes down to the bottom, reading the code line by line. If I would write my script the other way around, like this:
function main()
{
compute_days();
}
function compute_days()
{
...
}
the computer will say: oh, that’s function [B]main[/B]. I know function [B]main[/B]; I need to run it every time. What does it say now? [B]compute_days()[/B]. What’s with this function? I don’t know it yet! I don’t know what it wants from me. I’m going to display an error message and I will take the rest of the day off:
Don’t forget to define your functions first, otherwise the computer will complain when you try to use it.
Tomorrow we’ll look into functions more closely. Please complain here when something didn’t work or was unclear.