Re: QBASIC color numbers



<news@xxxxxxxxxxxxxxx> wrote:
"Judson McClendon" <ju...@xxxxxxxxxxxxx> wrote:
passing sub arguments by reference makes sense because the purpose of
subroutines is often to modify one or more of the arguments, since subs
do not return a value as do functions.

Wow, that's quite a generalisation! I would say that the vast
majority of subroutines do *not* modify one or more of their
arguments. The original (1981) version of BBC BASIC had *no way* of
passing parameters by reference to either functions or procedures
(subroutines); parameters were always passed by value. It wasn't
until something like five years later that BBC BASIC gained that
capability, and it was (and is) rarely used.

Perhaps it would be more widely used if it had been available. I find
that restricting the 'output' of subroutines to some subset of the
arguments can be useful in the reducing the linkage that results from
using global variables. Of course, it depends on the situation.

Also, passing arguments by value
to functions makes sense because intuitively, you don't expect a
function to change the value of an (external) argument

There is no conceptual difference between functions and subroutines in
this respect, and in fact you're quite wrong: functions in QBASIC
(FUNCTION declaration) *do* receive their parameters by reference just
as SUBs do, and can therefore change their values. You may be
thinking of the old DEF FN kind of function.

No, I realized that. I was speaking in context of the supposition suggested
by Tom Lake's comments (not included above) that, by default, arguments
would be passed by reference to subroutines and by value to functions.

though functions
often need to modify the value internally, for calculation purposes. A
DayOfWeek function based on Zeller's Congruence is a good example:

FUNCTION DOW(M AS INTEGER,D AS INTEGER,Y AS INTEGER) AS INTEGER
IF (M < 3) THEN
Y = Y - 1
M = M + 12
END IF
DOW = ((13*M+3)\5 + D + Y + Y\4 - Y\100 + Y\400 + 1) MOD 7
END FUNCTION

You think this *doesn't* change the values of M and Y external to the
function (when M < 3)? Try it!

You're right, of course. Again, I was speaking in context of arguments being
passed to functions 'by value' as a default. Below is my actual source code
that the function above was modified from:

FUNCTION DayOfWeek (Month AS INTEGER, Day AS INTEGER, Year AS INTEGER) AS INTEGER
DIM M AS INTEGER, D AS INTEGER, Y AS INTEGER
IF (Month < 3) THEN
Y = Year - 1
M = Month + 12
D = Day
ELSE
Y = Year
M = Month
D = Day
END IF
DayOfWeek = ((13 * M + 3) \ 5 + D + Y + Y \ 4 - Y \ 100 + Y \ 400 + 1) MOD 7
END FUNCTION
--
Judson McClendon judmc@xxxxxxxxxxxxx (remove zero)
Sun Valley Systems http://sunvaley.com
"For God so loved the world that He gave His only begotten Son, that
whoever believes in Him should not perish but have everlasting life."


.



Relevant Pages

  • Re: Functions vs. Subroutines
    ... >>Your code is harder to analyze ... >>when functions (and subroutines for that matter) modify their arguments. ... >>to read MATLAB programs partly because the language doesn't permit you ...
    (comp.lang.fortran)
  • Re: Merits of fortran
    ... sneak subroutines in the back door with functions that don't return ... but doing that requires allowing functions to modify their ... severe weakness of pure functions. ... It's best to try to simplify the language by ...
    (comp.lang.fortran)
  • Re: QBASIC color numbers
    ... subroutines is often to modify one or more of the arguments, ... passing parameters by reference to either functions or procedures ... as SUBs do, and can therefore change their values. ...
    (comp.lang.basic.misc)
  • Re: subroutine placement (Layout conventions)
    ... >> I declare the subs, then have the code for the main program, ... >> The fact that some subroutines may or may not call other subroutines ... >> and some of the subs are called by pretty much all the other subs ... > I have a habit of writing them in alphabetical order. ...
    (perl.beginners)
  • Re: subroutine placement (Layout conventions)
    ... a less-than-acceptable Perl script. ... to provide a higher-level action. ... > The fact that some subroutines may or may not call other subroutines ... > and some of the subs are called by pretty much all the other subs ...
    (perl.beginners)