Qbasicnews.com

Full Version: Structured Programming
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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
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???
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
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?
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.
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.
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
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.
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...
Blame FBIde not separating stuff..
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16