VCE IT Lecture Notes by Mark Kelly, McKinnon Secondary College

Processing features of programming languages

(2011) SD U3O2 KK09- processing features of programming languages, including instructions, procedures, methods, functions and control structures

 

Instructions

Instructions are commands that a particular programming language can interpret and carry out. Each language has its own instruction set; most languages have common instructions that they all can carry out even if their names vary (e.g. LOOP, PRINT, MSGBOX).

Most instructions need further information (parameters) to do their work (e.g. PRINT username)

 

 

Procedures

Procedures are usually self-contained segments of code that carry out a specific job. Modern programs are often made up of a collection of procedures that are called when needed. Procedures are sometimes also known as subprograms or subroutines. Programs built from procedures are sometimes called modular because pre-written modules can easily be inserted as needed into a new program, saving time and effort.

Calling procedures keeps programs smaller because code is not repeated whenever it has to be carried out. Program flow jumps to the desired procedure, the code is carried out, and then program flow returns to the point at which it was called and the program continues.

Many programmers keep a personal library of favourite procedures that they use all the time.

 

 

Methods

In an object-oriented language, a method usually refers to the pre-determined operations that an object can carry out.

Each class of object has its own particular methods. Just like a Dog object has the methods of bark, run, bite, sleep, and sniff, a textbox has methods like show, hide, enable. In Visual Basic, methods are invoked like form1.hide where form1 is the object and hide is the method it can carry out.

Other methods can be devised in procedures by the programmer. Below, when button1 is clicked, it triggers the custom button1.click procedure written by the programmer to call other procedures and start a game, e.g.

PRIVATE SUB button1.click( )
    CALL ResetScreen
    U = GetUserName()
    MSGBOX "Click OK to start the game, " & U
    CALL StartGame
END SUB


Tip: don't confuse an object's methods (the things it can do) with its properties (its settings). e.g. TEXTBOX.KEYDOWN is an action it can respond to, but TEXTBOX.WIDTH is a property that can be set.

 

Functions

A function is a procedure that returns a value to the code that calls it. It is a way to create a custom calculation. The programming language has inherent (built-in) functions for you to use, e.g. square root(number), sine(angle), uppercase(text), but you can create your own functions as needed.

Nearly all functions need to be provided with data to process. These data are called parameters. e.g. it's not much use asking for a square root without saying what number you want the square root of. This number is the function's parameter. Some functions need more than one parameter to calculate their value, e.g.

Private FUNCTION BuildName(firstname AS STRING, lastname AS STRING, title AS STRING)
    NewName = Title & " " & LEFT(Firstname,1) & ". " & lastname
    RETURN NewName
END FUNCTION

If, somewhere else in the program this (pseudo)code appeared:

WHILE NOT EOF(DATAFILE.TXT)
    INPUT Title, Gname, Sname
    NewName = Buildname(Gname,Sname,Title)
END WHILE

it would read every line in the text file and store the title (e.g. "Mrs"), first name and surname into variables. It would then call the Buildname function and pass it the 3 parameters.

The Buildname function takes the values supplied (e.g. "Mary", "Smith" "Miss") and stores them in its own local variables firstname, lastname and title. It then does its magic calculations (stripping the initial from the first name and concatenating the title and surname) to produce "Miss M. Smith". This value is returned to the calling procedure which then stores it in a variable called NewName.

Notice how the names of the parameter variables don't have to match the names of the parameters listed in the function definition. The values are passed as pieces of text. (See below about byval and byref)


Advanced tip:

Values can be passed to a function either by value (byval) or by reference (byref).

If parameters are passed by value, copies of the values of the variables are sent to the function. Whatever the function does to those data, the original values of the variable are not changed.

However if the parameters are passed by reference, any changes made by the function to the parameter also changes the variable that was sent as the parameter. e.g. if Buildname changed the value of firstname, the same change would be made in the variable gname. Because of the risks of byref parameter passing, it's not used often.

Control structures

to be continued

 

Back to the IT Lecture Notes index

Back to the last page you visited

Created $$

Last changed: September 14, 2010 3:07 PM

VCE IT Lecture notes copyright © Mark Kelly 2001-