Posts: 3,368
Threads: 195
Joined: Jan 2003
A function returns an argument, a sub doesn't.
Like so:
PRINT hrmthis%(5, 6)
FUNCTION hrmthis%(a%, b%)
hrmthis% = a% + b% * a%
END FUNCTION
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 488
Threads: 19
Joined: Sep 2005
Quote:A function returns an argument, a sub doesn't.
Like so:
PRINT hrmthis%(5, 6)
FUNCTION hrmthis%(a%, b%)
hrmthis% = a% + b% * a%
END FUNCTION
An argument???
Posts: 320
Threads: 24
Joined: Feb 2005
Quote:Agamemnus Wrote:A function returns an argument, a sub doesn't.
Like so:
PRINT hrmthis%(5, 6)
FUNCTION hrmthis%(a%, b%)
hrmthis% = a% + b% * a%
END FUNCTION
An argument???
A return value.
Code: Declare function blah (x as integer) as integer
print blah(5) 'this prints 10
function blah (x as integer)
blah = x + 5 ' this returns x + 5
end function
.14159265358979323846264338327950288419716939937510582709445
Glarplesnarkleflibbertygibbertygarbethparkentalelelangathaffendoinkadonkeydingdonkaspamahedron.
Posts: 488
Threads: 19
Joined: Sep 2005
Quote:axipher Wrote:Agamemnus Wrote:A function returns an argument, a sub doesn't.
Like so:
PRINT hrmthis%(5, 6)
FUNCTION hrmthis%(a%, b%)
hrmthis% = a% + b% * a%
END FUNCTION
An argument???
A return value.
Code: Declare function blah (x as integer) as integer
print blah(5) 'this prints 10
function blah (x as integer)
blah = x + 5 ' this returns x + 5
end function
So it's more of a way of accessing a mathematical equation?
Posts: 320
Threads: 24
Joined: Feb 2005
Quote:So it's more of a way of accessing a mathematical equation?
Kind of, but it's more than that. You can also use the return value to return error codes and whatnot. APIs like SDL also use functions to return pointers to data structures. Ever seen anything like this?
Code: Dim surf as SDL_Surface ptr
surf = SDL_LoadBMP ("Blah.bmp")
SDL_LoadBMP is a function that returns a pointer to an SDL_Surface.
.14159265358979323846264338327950288419716939937510582709445
Glarplesnarkleflibbertygibbertygarbethparkentalelelangathaffendoinkadonkeydingdonkaspamahedron.
Posts: 324
Threads: 12
Joined: Nov 2005
Quote:So it's more of a way of accessing a mathematical equation?
It's a way of accessing any information from a routine. For example, in a string concatenation routine, you can return the result of the concatenation. In an open file routine, you can return the file handle of the open file. Whatever information is pertinent to the result of the function being executed you can return. In other words, you use functions (as opposed to subs) when you need some kind of information from the routine after it is finished executing.
stylin:
Posts: 3,522
Threads: 189
Joined: Dec 2003
Quote:So it's more of a way of accessing a mathematical equation?
Subds and functions are groups of code that you can reuse over and over without typing the entire code over and over:
Code: declare function myFunction( a, b, c)
screen 12
print myFunction(1, 5, 7)
print myFunction(1, 5, 8)
print myFunction(1, 5, 9)
sleep
function myFunction(a, b, c)
line ( a, b )-( a, c ), 15
circle (a, b), c, 15
return a+b+c
end function
Posts: 488
Threads: 19
Joined: Sep 2005
Oh, ok, but I don't think I'd be using them very often, I'd prefer to cop and paste what I need over again so I don't have to look all over file for it, normaly I don't code with all the SUB's, GOSUB's, and DATA at the bottom, I enter it when I need it and it gets messy fast, atleast if I copy and paste the code, then I can just use FB's replace feature to change it all.
Posts: 320
Threads: 24
Joined: Feb 2005
Quote:Oh, ok, but I don't think I'd be using them very often, I'd prefer to cop and paste what I need over again so I don't have to look all over file for it, normaly I don't code with all the SUB's, GOSUB's, and DATA at the bottom, I enter it when I need it and it gets messy fast, atleast if I copy and paste the code, then I can just use FB's replace feature to change it all.
I've got a bad feeling about this...
.14159265358979323846264338327950288419716939937510582709445
Glarplesnarkleflibbertygibbertygarbethparkentalelelangathaffendoinkadonkeydingdonkaspamahedron.
Posts: 3,368
Threads: 195
Joined: Jan 2003
Blame FBIde not separating stuff..
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
|