Default BYVAL or BYREF Parameter Passing?

General FreeBASIC programming questions.
Post Reply

What do you think the parameter passing default should be?

Poll ended at Jul 14, 2007 22:08

1) Default all BYREF ( except function returns ).
13
29%
2) Default all BYVAL ( except arrays, and implicit THIS ).
13
29%
3) Mixed default: all BYVAL except Strings, UDTs and arrays.
8
18%
4) Don't care ( 1 to 3 ), so long as it is documented.
4
9%
5) No default. Explicit BYVAL/BYREF only.
7
16%
 
Total votes: 45
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

The theory afaik is that Objects really should be references, but they aren't. So there's the inconsistency. We pass objects by reference and scalars by value.

It's a relatively easy explanation to give. I would have gone for all byref too. v1ctor and coderJeff agree with my decision, as do a fair amount of the polls. We didn't take the most popular decision, but I think our decision is far from perilous to FB's future.

Fb's beta, if you're ever unsatisfied, we'll give you your money back, and who knows... maybe down the road it'll change again. Just being realistic.

Could you name some of the 'cons' beside just inconsistency? If you want a strict language with very few rules/keywords, BASIC is not a good choice...
relsoft
Posts: 1767
Joined: May 27, 2005 10:34
Location: Philippines
Contact:

Post by relsoft »

Beginners don't understand "objects". I guess I still don't.

Well, I guess you probably understood what objects are when you started coding. That's probably the difference between us users and you developers.
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Post by jevans4949 »

cha0s wrote:v1ctor and coderJeff agree with my decision, as do a fair amount of the polls.
I hate to be a moaner, so I held back to let someone else have a say.

The fact is, this was the THIRD most popular option. Having asked peoples' opinion, you ignored it!
Sisophon2001
Posts: 1706
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Post by Sisophon2001 »

jevans4949 wrote:
cha0s wrote:v1ctor and coderJeff agree with my decision, as do a fair amount of the polls.
I hate to be a moaner, so I held back to let someone else have a say.

The fact is, this was the THIRD most popular option. Having asked peoples' opinion, you ignored it!
They split the byval vote by giving us two choices (which I had difficulty choosing between), so we need proportional representation :).

Seriously, they made their choice, so just leave it. Vote again next year.

Garvan
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

My thought is now that should be all BYVAL for anything that's more than one length (basic data types except strings) and BYREF otherwise. (arrays, strings)

The UDT is where the problem comes in to this line of thought. UDTs can be mixes of stuff, but sometimes they are just two integers, say .apple and .orange. A bit confusing but I would agree to put it as BYREF.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

By the way, we didn't ignore the votes.... I suggested all byref just for that reason, but we ultimately decided what we decided on. We love you.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

This is just some observations since the change..

It could be that i'm just used to having a blanket default, in all of the languages I use, but this has been rather difficult for me to work with. Before I could just skim code and rely on the fact that if nothing was specified it was BYVAL, now I have to pay attention to types as well. And this is made even more difficult with type aliases - who knows if it's a scalar type or not without hunting for the alias. IMO, the code looks very messy, so much so that I've begun specifying BYVAL for everything not BYREF just for readability. That doesn't really help though, in a large source BYVAL and BYREF tend to look very similar.

Afaik, the theory is that not all objects are designed to be passed by reference. We pass objects both by reference and by value, depending on the design - no inconsistency there. No, I don't think I'd like it if FreeBASIC was uber strict and had few keywords, but this change just doesn't make sense to me, particularly in a BASIC context, which to me means an intuitive, easy-to-learn language.
Basic Coder
Posts: 180
Joined: Aug 02, 2006 23:37
Location: Australia

Post by Basic Coder »

relsoft wrote:Beginners don't understand "objects". I guess I still don't.

Well, I guess you probably understood what objects are when you started coding. That's probably the difference between us users and you developers.
A real beginner understands nothing and I think could learn to think
in terms of objects as in Java easier than it has been for me who
learned to program in the old BASIC with GOSUBs !!!

For me the first version of FreeBasic was great as I could program
the way I always have, badly :)

I haven't given a view or a vote on how to pass things, my only
concern is whatever choice is made I know what it is.
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

No party in this debate is right, all parties have quite legitimate claims to their preference and diverse programming styles. Remember this discussion is only about a default, it is not compulsory.

ByRef is the equivalent of implicitly passing a pointer. QB avoided explicit pointers and therefore passed all ByRef. A programmer who comes to FreeBASIC from a language like QB or Fortran that implicitly passed pointers will write FreeBASIC in that style, they will pass ByRef and avoid explicitly declared pointers. Interfacing to assembly code favours passing addresses on the stack, which is implicitly ByRef.

{
A programmer who comes to FreeBASIC, from a language like Pascal or C that requires the declaration of pointers, will tend to write FreeBASIC in a C style, they will pass ByVal and use explicitly declared pointers. Interfacing FreeBASIC to C required the ability to declare pointers and pass ByVal. Likewise writing the FreeBASIC compiler will favour the C style because of the C development tools and rtl. Compiler developers will therefore be biased to favour a C style while beginners might favour the original QB style.
}

All ByRef was used in QB because it was simple, consistent and worked without explicitly declared pointers. A KISS principle solution now requires we either default ByRef like QB or we demand explicit. It logically follows that if there is a simple default, it would be ByRef because explicit is not a default, it is a demand.

Just because you believe you understand today’s rules for determining the defaults, it does not mean you can rely on them. We have been warned that the rules can be expected to mutate over time. Anyone who does not now always explicitly specify either ByRef or ByVal is knowingly writing potentially unreliable code. We have spawned a chimera.

There can be no pragmatic solution to this continuing problem along the present datatype dependent hybrid line. This futile tug of war will continue until one clear KISS solution is accepted and implemented.
jmgbsas
Posts: 35
Joined: Dec 26, 2020 16:03

Re: Default BYVAL or BYREF Parameter Passing?

Post by jmgbsas »

coderJeff wrote:Default BYVAL or BYREF Parameter Passing?

One issue for programmers that keeps coming up in different ways is the changed default BYVAL/BYREF paraneter passing expectation. In the 0.17-final release, the default is BYVAL for -lang fb. For those who may not know, in previous versions it was BYREF. The BYVAL/BYREF default affects how parameters are passed to procedures. With BYVAL, a copy of the variable is passed to the procedure. With BYREF a reference (adddress) is passed to the procedure.

I would like to change the default back to BYREF, because:
1) I always want to pass STRINGs BYREF.
2) I almost always want to pass UDT's BYREF.
3) The default in -lang fb is different from -lang qb and -lang deprecated which causes confusion.
4) When I copy code from other basics (like VB), I always must remember to add in the BYREF, or I have sometimes hard to find bugs.

But, I do not think OPTION BYVAL should come back with it. Any header that used it, was not easily (if at all) shareable in another project. However, a push/pop only #pragma for BYVAL could be added, such that it would only be in effect for the header/module in which it is used.

I am thinking that for anyone who made it through the first change from BYREF to BYVAL, this change won't matter, because you probably now do what I do: always specify BYVAL/BYREF.

As always, comments and questions are welcome!
Today I had a parameter that gave me an error and could not find the problem in FB 64 on 64-bit windows. It didn't return anything, and now I know that the default is ByVal. I Look in the help and there is nothing on this topic.To reduce memory I always prefer ByRef. But it is dangerous for data integrity. Thanks
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Default BYVAL or BYREF Parameter Passing?

Post by Josep Roca »

Add BYVAL or BYREF explicitily to the parameters, instead of relying on defaults, and you won't have any problem.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Default BYVAL or BYREF Parameter Passing?

Post by fxm »

jmgbsas wrote:I Look in the help and there is nothing on this topic.
Look at BYVAL or BYREF documentation page:
Dialect Differences:
  • In the -lang fb dialect, Byval is the default parameter passing convention for all built-in types except String and user-defined Type which are passed Byref by default. The Zstring and Wstring built-in types are also passed Byref by default, but passing Byval is forbidden. Arrays are always passed Byref and the use of the specifier Byref or Byval is forbidden.
  • In -lang qb and -lang fblite dialects, Byref is the default parameter passing convention.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Default BYVAL or BYREF Parameter Passing?

Post by jj2007 »

fxm wrote:In the -lang fb dialect, Byval is the default
In -lang fblite dialects, Byref is the default
That's easy, thumbs up!
jmgbsas
Posts: 35
Joined: Dec 26, 2020 16:03

Re: Default BYVAL or BYREF Parameter Passing?

Post by jmgbsas »

fxm wrote:
jmgbsas wrote:I Look in the help and there is nothing on this topic.
Look at BYVAL or BYREF documentation page:
Dialect Differences:
  • In the -lang fb dialect, Byval is the default parameter passing convention for all built-in types except String and user-defined Type which are passed Byref by default. The Zstring and Wstring built-in types are also passed Byref by default, but passing Byval is forbidden. Arrays are always passed Byref and the use of the specifier Byref or Byval is forbidden.
  • In -lang qb and -lang fblite dialects, Byref is the default parameter passing convention.
Is my mistake yes and in Passing Arguments to Procedures too, I was reading with small fonts and do not see sorry Thanks..
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Default BYVAL or BYREF Parameter Passing?

Post by marcov »

fxm wrote:
jmgbsas wrote:I Look in the help and there is nothing on this topic.
Look at BYVAL or BYREF documentation page:
Dialect Differences:
  • In the -lang fb dialect, Byval is the default parameter passing convention for all built-in types except String and user-defined Type which are passed Byref by default. The Zstring and Wstring built-in types are also passed Byref by default, but passing Byval is forbidden. Arrays are always passed Byref and the use of the specifier Byref or Byval is forbidden.
  • In -lang qb and -lang fblite dialects, Byref is the default parameter passing convention.
Sounds fine. Maybe emit a warning when a value type exceeds a certain size ?

Note that ABI requirements also influence byval/byref.
Post Reply