Author |
Message
Pages: 1
|
||||
Admin Site Admin (send private message) Posts: 933 Topics: 55 Location: OverHertz Studio | [1382] [BEGINNER]: Procedures and Functions - posted: 2013-04-12 14:47:42 -TUTORIAL BEGIN- In this tutorial i assume you have already read the Console Hello World Tutorial. We are going to take a look at writing procedures and functions. Procedures and Functions can also be refered to as methods, so I will use this term often in this tutorial. A method is a piece of code that is called from another part of your program, it is similar to using a goto as the CPU will move to the location of the code of your method and execute the method, at the end of the method it will return to the previous calling point. It is also refered to as a sub-routine. A small psuedo example of this is as follows: Code:
so here we see at the start of the program the cpu will move to the position of method1 and write hello world and then return back to the call position which next in line is goodbye world. We will look at this again soon as will write our sample to perform the above code. Now i will explain the difference between a procedure and a function. A procedure can be called then return, where-as a function can be called and return a result to the caller. Code:
So to begin we will add a procedure to our application and move out println to the procedure, we will also replace println with print, print is a macro, i will go into macros in another tutorial. Code:
Now we can see that our program will call our method which in-turn will print Hello World to the console. We will also take a look at embedded methods. An embedded method is basically another method that is only accessable from inside the function it was created. This is often refered to as the scope. So for example Code:
we can see in this example the code will NOT assemble, the GoodbyeWorld method is within the HelloWorld method's scope, so only within the HelloWorld procedure can GoodbyeWorld be accessed, so we will change our code to the following: Code:
and now when our program is assembled and run we will see the following output to the console:
our full program should look like this: Code:
You can embed many sub-methods inside of procedures as needed, this allows you to create private methods that are not accessable to the caller. for the last example we will call a function that returns a number and prints it to the console. We will add a new function named Get512 and inside return 512, as simple as that. Code:
when calling a function, in most scenarios the result will be contained within the register reference gp_ax so we have 2 choices. Code:
or just Code:
in those cases they assemble into identical code, the first sample is the prefered as this allows the code to be more readable to others and easier to see it is a function. Code:
And now we will see on the console
In another tutorial i will go into passing parameters to methods I hope this tutorial has helped, keep checking back for more! -TUTORIAL END- Download Ziron Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message. |
Currently Active Users: There are currently 3 user(s) online. 0 member(s) and 3 guest(s) Most users ever online was 1046, January 28, 2022, 2:08 pm. Statistics: Threads: 225 | Posts: 1848 | Members: 51 | Active Members: 51 Welcome to our newest member, yecate |