Re: Procedure, function and return

new topic     » goto parent     » topic index » view thread      » older message » newer message

Hi

For simplicity.

A function always returns something.

A procedure doesn't return anything.

Both are subroutines

example

function add2numbers(integer a, integer b) 
 return (a+b)                        -- must have a return in a function 
end function 
c = add2numbers(3,4)                 -- c is returned from the function 
? c                                  -- ? is shorthand for quick and dirty print 
 
procedure subtract2numbers(integer a, integer b) 
 ? (a-b)                             -- ? is here, as no value is returned 
return                               -- this return is optional 
end procedure 
subtract2numbers(9,12)              -- nothing is returned from the procedure 
 
 
--there is a twist, but you don't need this for now - keep it simple 
 

As far as global variables go, funtions and procedures both operate on them, and the result remains global so they can be read wherever in the program. But try to discipline yourself to avoid using them, as using local variables (to the function or procedure) can make your code easier to read. I freely admit in some cases it is just easier to use global variable (eg flags across several functions), but signify them with a global name - form your own convention to recognise the globality (is that a word) - for instance all capitals, or prefixed with G_ etc.

Cheers

Chris


fixed a typo, _tom And another 2 fixed! - Chris

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu