How computer program works?

by shian ⌂, Tuesday, August 25, 2020, 09:18 (35 days ago) @ shian
edited by shian, Wednesday, August 26, 2020, 06:38

Personal computer program works in a perfect serial order. The PC (Personal Computer) reads and executes each statement that you write one by one, from top (beginning of program) to bottom (end of program). i.e.:

 
First statement
Second statement
Third statement
...
...
...
Last statement
 


The program ends when the PC reads and executes the last statement.

To keep the program running for a long time we must create a main loop, infinite loop usually. The entire program, as large as it is, runs from within that main loop.
The main loop normally waits for an event to occur, such as user typing on the keyboard or clicking with the mouse, or other events such as Internet or system request, etc. i.e.:

 
Some local and/or global declarations
 
any statement
 
start of main loop
  statement one
  statement two
  ...
  ...
end of main loop -- (go back to the start)
 
any statement
 


To make our program more readable and maintainable we must make it MODULAR.
So the main loop is normally calling subroutines, procedures or functions. Each subroutine (routine in short) is doing something very specific, for example the function get_key() in Euphoria 3 is checking if the user typed on the keyboard - that's all it does, nothing else.

Using MODULAR programming we can create very large programs without a problem. Each routine is doing a single task, and doing it well.

To make modular programming even better we create "libraries" of related routines, such as the libraries in Euphoria 3: graphics.e, sort.e, get.e, etc; or the libraries in Lib2 (by shian): string.e, machine2.e, utf8.e, etc.

Whenever we need a routine(s) from a library we just need to include the library in our program, i.e.:

 
include sort.e
 


That is it. In the example above all routines from sort.e library are now available to our program! That is modular, readable, maintainable procedural programming.
Now you know all the ancient secrets about modular procedural programming: it's all comes down to subroutines and libraries of subroutines (which also called "modules") - nothing else.

Note:
-----
OOP (Object Oriented Programming) is based on procedural programming, but OOP is a complete monarchy system, while procedural programming is a democracy. ...That explains why many people goes for OOP - they are afraid of freedom!

OK, so where were we? Yea, main loop that calls subroutines. It looks like this:

 
any statement
 
start of main loop
  ...
  key = get_key()
  ...
  click = get_mouse()
  ...
  ...
end of main loop
 
any statement
 


Now, to make our program useful, we need "branching statements" such as if..then. for example:

 
any statement
 
start of main loop
 
  key = get_key()
 
  click = get_mouse()
  ...
 
  if key = Escape then
    exit main loop
 
  else if key = make_me_coffee then
    my_coffee = coffee_cup()
 
  else if click = bomb_icon then
    shutdown_system()
 
  else
    print "come on, do something, time is money! (as if)."
 
  end if
  ...
end of main loop
 
any statement
 


The key & click in the example above are called: variables. Variables are holding values, so for example key is holding the value that the get_key() routine returns, i.e.: key = get_key().

We must declare variables before we use them, i.e.:

 
any statement
 
integer key
sequence click
 
start of main loop
 
  key = get_key()
 
  click = get_mouse()
  ...
 
end of main loop
 
any statement
 


To make things more human we should add comments here and there which describe WHY we are doing this or that, i.e.:

 
any statement
 
integer key
sequence click
 
-- this is a comment that tells us WHY we need this loop
start of main loop
 
  key = get_key()
  click = get_mouse()
  ...
 
  if key = Escape then
    exit main loop
 
  else if key = make_me_coffee then
    my_coffee = coffee_cup() -- a comment tells us WHY we need coffee
 
  else if click = bomb_icon then
    shutdown_system()        -- a comment tells us WHY we use brutal force
 
  else
    print "come on, do something, time is money! (as if)."
 
  end if
  ...
end of main loop
 
any statement
 


In real Euphoria 3 code the above example will look similar to this:

 
-- my first program for learning Euphoria 3
-- ----------------------------------------
 
-- here I declare all constants (constant is a read-only variable)
constant TRUE = 1
constant ESCAPE = 27, MAKE_ME_COFFEE = -500, BOMB_ICON = 10
 
function coffee_cup()
   integer coffee
 
   -- some statements
   coffee = 2
 
   return coffee
end function
 
function get_my_mouse()
   integer mouse
 
   -- some statements
   mouse = 1
 
   return mouse
end function
 
procedure shutdown_system()
   -- some statements
end procedure
 
 
-- here I declare all local variables for this module
integer key, my_coffee
object click
 
clear_screen()
 
-- the main loop of my program is waiting for the user
-- to press a key or click the mouse
while TRUE do
 
  key = get_key()
 
  click = get_my_mouse()
 
  if key = ESCAPE then
    exit -- end of program, exit main loop
 
  elsif key = MAKE_ME_COFFEE then
    my_coffee = coffee_cup() -- if my_coffee = 1 then pause program for break
 
  elsif click = BOMB_ICON then
    shutdown_system()        -- in case the program gets on my nerves
 
  else
    if key != -1 then
       position(1, 1)
       puts(1, "come on, do something, time is money! (as if).")
    end if
 
  end if
 
end while
 
clear_screen()
 
-- End of program.
 

OK, to sum up, computer program is a sequence of statements that the computer executes in serial order, one by one, to accomplish some task.

Here is a real, working, Euphoria 3 program, that divides two numbers and prints the result on the screen (copy/paste to text file 'divide.ex' and run it):

 
include get.e -- prompt_number()
 
function divide(atom n1, atom n2)
  atom r 
 
  r = n1 / n2
 
  return r
end function
 
 
constant TRUE = 1, SCREEN = 1
 
atom number1, number2, result
 
clear_screen()
 
while TRUE do
  number1 = prompt_number("Enter number 1: ", {})
  number2 = prompt_number("Enter number 2 (0 = Quit): ", {})
 
  if number2 = 0 then
    exit -- exit loop to end program
  else
    result = divide(number1, number2)
    printf(SCREEN, "%g / %g = %g\n\n", {number1, number2, result})
  end if
end while
 
puts(SCREEN, "\nDone.\n")
 
-- end of program.
 

Tags:
lesson


Complete thread:

 RSS Feed of thread

powered by my little forum