Warning: Illegal string offset 'html' in /home/hsn/public_html/forum/cache/skin_cache/cacheid_1/skin_topic.php on line 909

Warning: Cannot modify header information - headers already sent by (output started at /home/hsn/public_html/forum/cache/skin_cache/cacheid_1/skin_topic.php:909) in /home/hsn/public_html/forum/admin/sources/classes/output/formats/html/htmlOutput.php on line 114

Warning: Cannot modify header information - headers already sent by (output started at /home/hsn/public_html/forum/cache/skin_cache/cacheid_1/skin_topic.php:909) in /home/hsn/public_html/forum/admin/sources/classes/output/formats/html/htmlOutput.php on line 127

Warning: Cannot modify header information - headers already sent by (output started at /home/hsn/public_html/forum/cache/skin_cache/cacheid_1/skin_topic.php:909) in /home/hsn/public_html/forum/admin/sources/classes/output/formats/html/htmlOutput.php on line 136

Warning: Cannot modify header information - headers already sent by (output started at /home/hsn/public_html/forum/cache/skin_cache/cacheid_1/skin_topic.php:909) in /home/hsn/public_html/forum/admin/sources/classes/output/formats/html/htmlOutput.php on line 137

Warning: Cannot modify header information - headers already sent by (output started at /home/hsn/public_html/forum/cache/skin_cache/cacheid_1/skin_topic.php:909) in /home/hsn/public_html/forum/admin/sources/classes/output/formats/html/htmlOutput.php on line 141
Software Development - HSN forum

Jump to content


Software Development


5 replies to this topic

#1 Steve

    Top of the Class

  • Admin
  • PipPipPipPipPip
  • 435 posts
  • Location:Edinburgh
  • Gender:Male

Posted 10 January 2004 - 09:54 PM

I used COMAL for Higher too.

Parameter Passing

When you have a sub-program (PROC in COMAL), you can pass parameters into and out of the sub-program.

Here is a simple example of some COMAL code which uses parameter passing:

//The main program
get_details_from_user
display_details (name$, address$) CLOSED
END

//The sub-program
PROC display_details (first_value$, second_value$)
PRINT first_value$
PRINT second_value$
ENDPROC display_details

Say name$ = "Steve" and address$ = "West Lothian", this sub-program would output:

Steve
West Lothian

This works because the values of the variables that you put in the brackets in the main program (name$, address$) are passed into the sub-program. The first variable in the brackets of the sub-program (first_value$) then takes on the value of the first variable in the brackets of the main program (name$).

At this point, name$ and first_value$ both have the value "Steve" and the sub-program can display name$ without there being a variable called name$ in the sub-program.

The same thing goes for address$. Since address$ is listed second in the brackets in the main program, its value is passed to the second variable in the brackets of the sub-program (second_value$). So now second_value$ = "West Lothian".

The thing to remember is that the value of the first variable in the brackets in the main program becomes the value of the first variable in the brackets in the sub-program, and the value of the second variable in the brackets in the main program becomes the value of the second variable in the brackets in the sub-program, and so on. Simply put, first_value$ takes on the value of name$ and second_value$ takes on the value of address$ (ie the variables with the same colour above are equal).

Advantages of parameter passing

The same sub-program can be used in many different programs by simply changing the parameters which are passed into it. For example:

//The main program
get_details_from_user
display_details (height$, weight$) CLOSED
END

//The sub-program
PROC display_details (first_value$, second_value$)
PRINT first_value$
PRINT second_value$
ENDPROC display_details

Say height$ = "1.8m" and weight$ = "65kg", this sub-program would output:

1.8m
65kg

This is a very simple example, but hopefully you can see how it could be applied in more complex standard sub-programs.

Portablility

Hopefully you'll see that parameter passing aids portablility as the same sub-program can be used in many programs.

Passing by reference or value

My example used passing by value but passing by reference allows the value of the variable in the main program to be affected by the sub-program (as well as the other way round). So you were right about that!

Hope this helps but feel free to ask again, I know I can go on a bit!
HSN contribute: Help the site grow!

Looking for a Maths tutor in West Lothian? Just PM me!

#2 sparky

    Fully Fledged Genius

  • Moderators
  • PipPipPipPipPipPipPip
  • 1,323 posts
  • Location:Airdrie

Posted 11 January 2004 - 04:44 PM

Thank you! I think I finally understand that! In the notes my teacher gave me theres just so much in them which make them quite hard to pick out the key areas!

Only one more thing! Is an array used to hold a list of values or names?

Mark :lol:
Mark

#3 Steve

    Top of the Class

  • Admin
  • PipPipPipPipPip
  • 435 posts
  • Location:Edinburgh
  • Gender:Male

Posted 11 January 2004 - 08:27 PM

Pretty much. You can think of it as a group of related variables.

An array is made up of elements which are like spaces for values in the array. For example and array called names$(9) in COMAL would hold up to 10 names: one in names$(0), another in names$(1), all the way up to names$(9). The number after in brackets after the array when you set it up determines how many elements (spaces) there are in the array.

For example, this COMAL code sets up an array with 20 elements (remember 0 is the first element):

DIM ages(19) Notice no $ after the array name, so this is an array of floating point numbers.

Arrays make manipulating lists of related data (in my last example all values in the ages array are ages, so that's how they are related) easy. Have a look at this code:

FOR counter = 0 to 19 DO

PRINT ages(counter)

NEXT counter


On the first run through the loop, the variable counter = 0, so the value of ages(0) is printed, on the next run through, counter = 1 so ages(1) is printed, all the way up to the last run through when counter = 19, so ages(19) is printed. This is because the number in brackets after the array in the loop is a variable which increments, so each element is displayed.
HSN contribute: Help the site grow!

Looking for a Maths tutor in West Lothian? Just PM me!

#4 Discogirl17

    HSN Legend

  • Moderators
  • PipPipPipPipPipPipPipPip
  • 3,034 posts
  • Location:South Lanarkshire
  • Gender:Female

Posted 15 April 2004 - 06:57 PM

Arrays can be used to hold values or names. For instance in VB if you Dim an array "as String" it will hold text and "as integer" it will hold values. Thats right isnt it? Yep I think it is

Info on array- arrays can be thought as a group of variables
- arrays are traditionally passed by reference
- algorithms (list operations) such as find the maximum and find the minimum are more easily coded using an array with so many positions than using the equivalent number of individual variables.

Does this help?
Half ideas,half quality, half a million pound law suit!

#5 sparky

    Fully Fledged Genius

  • Moderators
  • PipPipPipPipPipPipPip
  • 1,323 posts
  • Location:Airdrie

Posted 15 April 2004 - 08:36 PM

Yeah this was actually one of my panic moments, when really we hadn't covered it at the time but I thought we had, and it had just by-passed me.
Thanks anyway! smile.gif

Mark
Mark

#6 Discogirl17

    HSN Legend

  • Moderators
  • PipPipPipPipPipPipPipPip
  • 3,034 posts
  • Location:South Lanarkshire
  • Gender:Female

Posted 15 April 2004 - 08:46 PM

Happy to help! smile.gif
Half ideas,half quality, half a million pound law suit!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users