Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Procedure Vs. Funtion
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
AzureFire




PostPosted: Sat Jan 14, 2006 6:43 pm   Post subject: 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
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sat Jan 14, 2006 7:05 pm   Post subject: (No subject)

Procedures are fine. Processes are not.
masterfenix




PostPosted: Sat Jan 14, 2006 7:07 pm   Post subject: (No subject)

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




PostPosted: Sat Jan 14, 2006 7:57 pm   Post subject: (No subject)

masterfenix wrote:
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.:
Turing:

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




PostPosted: Sat Jan 14, 2006 8:20 pm   Post subject: (No subject)

Delos wrote:

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




PostPosted: Sat Jan 14, 2006 8:33 pm   Post subject: (No subject)

Nice stuff Delos. Every post is a tutorial. Smile

Delos wrote:
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




PostPosted: Sat Jan 14, 2006 9:34 pm   Post subject: (No subject)

MysticVegeta wrote:
Delos wrote:

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




PostPosted: Sat Jan 14, 2006 9:40 pm   Post subject: (No subject)

code:

%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
Sponsor
Sponsor
Sponsor
sponsor
AzureFire




PostPosted: Sat Jan 14, 2006 10:50 pm   Post subject: (No subject)

SuperFreak82 wrote:
code:

%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




PostPosted: Tue Jan 17, 2006 11:13 pm   Post subject: (No subject)

AzureFire wrote:
SuperFreak82 wrote:
code:

%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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: