Register | Login
Forum Index > User Tutorials > [BEGINNER]: Procedures and Functions
Author Message
Pages: 1
Admin
Site Admin

avatar

(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:
method1
  say 'Hello World'
  return

start program:
  call method1
  say 'Goodbye world'


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:
procedure A:
  return

function B:
  return 512

start:
  eax = A    // this is invalid as a procedure does not return a value
  eax = B    // this is valid since functions do return values.


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:
program PE32_CUI 'my second application';

#include 'win_def.zir';
#include 'console.zir';

procedure HelloWorld {
  print('Hello, World!\r\n');
}

HelloWorld;

wait_key();
ExitProcess(0);


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:
procedure HelloWorld {
  procedure GoodbyeWorld {
    print('Goodbye, World!\r\n');
  }

  print('Hello, World!\r\n');
}

HelloWorld;
GoodbyeWorld;


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:
procedure HelloWorld {
  procedure GoodbyeWorld {
    print('Goodbye, World!\r\n');
  }

  print('Hello, World!\r\n');
  GoodbyeWorld;
}

HelloWorld;


and now when our program is assembled and run we will see the following output to the console:

Hello, World!
Goodbye, World!


our full program should look like this:

Code:
program PE32_CUI 'my second application';

#include 'win_def.zir';
#include 'console.zir';

procedure HelloWorld {
  procedure GoodbyeWorld {
    print('Goodbye, World!\r\n');
  }

  print('Hello, World!\r\n');
  GoodbyeWorld;
}

HelloWorld;

wait_key();
ExitProcess(0);


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:
function Get512 {
  return 512;
}


when calling a function, in most scenarios the result will be contained within the register reference gp_ax so we have 2 choices.

Code:
gp_ax = Get512();


or just

Code:
Get512();


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:
program PE32_CUI 'my second application';

#include 'win_def.zir';
#include 'console.zir';

function Get512 {
  return 512;
}

procedure HelloWorld {
  procedure GoodbyeWorld {
    print('Goodbye, World!\r\n');
  }

  print('Hello, World!\r\n');
  GoodbyeWorld;
}

HelloWorld;

gp_ax = Get512();
print('Number: ', gp_ax, '\r\n');

wait_key();
ExitProcess(0);


And now we will see on the console

Hello, World!
Goodbye, World!
Number: 512


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.
Pages: 1
create new reply


Quick reply:

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
const Copyright = '2011-2024 © OverHertz Ltd. All rights reserved.';
Web development by OverHertz Ltd