
-----------------------------------
AzureFire
Sat Jan 14, 2006 6:43 pm

Procedure Vs. Funtion
-----------------------------------
I saw a post that said procedures aren't reliable, and not to use them. So I was wondering, is function the same thing? And if it is, is there a difference?

Thanks

-----------------------------------
Cervantes
Sat Jan 14, 2006 7:05 pm


-----------------------------------
Procedures are fine.  [url=http://www.compsci.ca/v2/viewtopic.php?t=7842]Processes are not.

-----------------------------------
masterfenix
Sat Jan 14, 2006 7:07 pm


-----------------------------------
A procedure is just a normal peice of code.


a function is usually when you want to do a calculation and get a result.

functiosn have to result 1 thing. and it can only result one. 

while in a procedure you can caluclate stuff, and send it out to variables.

-----------------------------------
Delos
Sat Jan 14, 2006 7:57 pm


-----------------------------------
A procedure is just a normal peice of code.


a function is usually when you want to do a calculation and get a result.

functiosn have to result 1 thing. and it can only result one. 

while in a procedure you can caluclate stuff, and send it out to variables.

Hmm...this sounds oddly off-course.  While you may have the right idea, it's not quite - uh - precise.

Procedures are compartmentalized pieces of code, which theoretically have no idea that an outside world exists.  To them, they see something coming in (their parameters), do stuff with them (perhaps incorportating some of their own local vars), then send stuff out again into the great, big oblivion.
Functions are very similar, and effectively produce a single result.  I say effectively, since if used in conjunction with records, they can produce quite a few results packaged as one.

AzureFire is probably referring to processes, as Cervantes mentioned, though he may be talking about the wtd-popularized concept that functions are inherently more flexible than procedures in many situations.  The most common situation where people use procedures instead of the more efficient functions would be in the initialization of a record type.  I.e.:

type quickType :
    record
        name : string
        value : int
    end record

procedure p_initQT (var inQT : quickType, inName : string, inValue : int)
    inQT.name := inName
    inQT.value := inValue
end p_initQT

function f_initQT (inName : string, inValue : int) : quickType
    var tempQT : quickType
    tempQT.name := inName
    tempQT.value := inValue
    result tempQT
end f_initQT

var qt1, qt2 : quickType

% So, using:
p_initQT (qt1, "QT1", 1)
% Instead of:
qt2 := f_initQT ("QT2", 2)


-----------------------------------
MysticVegeta
Sat Jan 14, 2006 8:20 pm


-----------------------------------

Procedures are compartmentalized pieces of code, which theoretically have no idea that an outside world exists

uh dont they check the "outside world" if they have some vars in them that are not local to check and see if they are global?

-----------------------------------
Cervantes
Sat Jan 14, 2006 8:33 pm


-----------------------------------
Nice stuff Delos.  Every post is a tutorial. :)

Functions are very similar, and effectively produce a single result.  I say effectively, since if used in conjunction with records, they can produce quite a few results packaged as one.


You can also return an array of values.  The key thing to note, however, is that you cannot return a dynamic amount/type of data: You can't return an array of compile-time-unknown length.  That sucks, hugely.

MysticVegeta: Notice how he said "theoretically".  You can have procedures access the outside world via global variables, but it is bad coding practice.  The procedure should be able to perform its function in any program, given its input parameters.  Moving the procedure from one program to another (and hence changing the global variables present) destroys this philosophy.

-----------------------------------
Clayton
Sat Jan 14, 2006 9:34 pm


-----------------------------------

Procedures are compartmentalized pieces of code, which theoretically have no idea that an outside world exists

uh dont they check the "outside world" if they have some vars in them that are not local to check and see if they are global?

procs do in fact check to see if vars inside of procs are global. this is an easy way of using variables that u dont want to change from one proc (or function) to the next without using parameters, and you can send them off to the next procedure too.

-----------------------------------
Clayton
Sat Jan 14, 2006 9:40 pm


-----------------------------------

%sees if a number is greater than ten, or less than ten
%global variable
%this is just an example, this could be done easier and better with parameters but it is an example
var number:int:=0
%procedures
proc greaterLess
     if number>=10 then
          Draw.Cls
          put "Your number is greater than 10"
     else
          put "Your number is less than 10"
     end if
end greaterLess
%Main Line
put "Please enter a number"
get number
greaterLess


-----------------------------------
AzureFire
Sat Jan 14, 2006 10:50 pm


-----------------------------------

%sees if a number is greater than ten, or less than ten
%global variable
%this is just an example, this could be done easier and better with parameters but it is an example
var number:int:=0
%procedures
proc greaterLess
     if number>=10 then
          Draw.Cls
          put "Your number is greater than 10"
     else
          put "Your number is less than 10"
     end if
end greaterLess
%Main Line
put "Please enter a number"
get number
greaterLess


Annnndddd your point is?


Anyway, thanks for the answers. And yes i was confused by procedures and processes. Must have misread it.

-----------------------------------
Clayton
Tue Jan 17, 2006 11:13 pm


-----------------------------------

%sees if a number is greater than ten, or less than ten
%global variable
%this is just an example, this could be done easier and better with parameters but it is an example
var number:int:=0
%procedures
proc greaterLess
     if number>=10 then
          Draw.Cls
          put "Your number is greater than 10"
     else
          put "Your number is less than 10"
     end if
end greaterLess
%Main Line
put "Please enter a number"
get number
greaterLess


Annnndddd your point is?


i was just showing how procs search for vars outside of the proc itself
Anyway, thanks for the answers. And yes i was confused by procedures and processes. Must have misread it.
