Archive for the ‘Language Constructs’ Category

Confusion!

November 26, 2007

I think I have confused at least half the class with my explanation of parameter passing! Or at least maybe not my explanation but certainly my attempted explanation of the NAB type question.

Let me try and simplify it again:

Call by value means a copy of a variable is used in a sub-routine. If it changes the variable, only the copy is changed, not the original.

Call by reference uses the original variable (by referring to the address of the variable rather than a copy). If it changes, then the original is changed also.

If you understand that then you are probably fine! The NAB question was confusing as it lacked some important details.

The other thing we did to day was functions. The important thing about them is that they produce a value. For example random produces a random number.

functinos.jpg

Here is the powerpoint on parameter passing:

parameter-passing.jpg

Advertisements

Brazilians!

November 23, 2007

This was Mr Polland’s excellent program that I converted in to VB (and am still a bit too excited about).

It has an array of your names, an array of brazilian endings, and it concatenates a random ending onto a substring of your name.

The point of the lesson is to do with Functions. The random function is called with two parameters (low and high) andit then generates a random number between those two. In the program I use that random number to pick a brazilian ending and put it onto your name.

Here is a screenshot:

brazilian-program.jpg

Local and Global Variables

November 22, 2007

A local variable is declared (set up) and only available for use in the sub-routine it is made in. A global variable is declared at the start of your program (in VB anyway) and can be used anywhere.

It is easier to understand when you look at some code:
private sub get_marks

dim marks(10) as integer

for counter 1 to 10

marks(counter) = inputbox(“Type in your mark”)

next

end sub

In the code above the array (marks) is only available inside the sub-routine called get_marks. If you try to access it somewhere else, it won’t work i.e. the scope of the variable (array) is local

If you set up the variable (or array) at the start of the program before any sub-routines (like we normally do) then it will be available for use everywhere i.e. the scope of the variable is global

Notes below:

local-global-variables.jpg