Assigning a value to a function does not produce a compilation error

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
erik
Posts: 39
Joined: Dec 28, 2020 17:27
Location: Krasnoyarsk
Contact:

Assigning a value to a function does not produce a compilation error

Post by erik »

Take a look at this code:

Code: Select all

Function Foo(ByVal param As Integer)As Integer
	Return 2
End Function

Foo(3) = 5
I assign a value to the function. Why does not the compiler throw an error?
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Assigning a value to a function does not produce a compilation error

Post by jj2007 »

You are right, it should throw an error. Gcc simply ignores the statement (and does not generate any code), while GAS calls the function with an argument zero (which doesn't make any sense):

Code: Select all

asm int 3
Foo(&H1234) = &H12345678
asm nop

Code: Select all

004015D5  |.  CC            int3                                     ; |
004015D6  |.  6A 00         push 0                                   ; |/Arg1 = 0
004015D8  |.  E8 B3FFFFFF   call 00401590                            ; |\TmpFb.00401590
004015DD  |.  90            nop                                      ; |
SARG
Posts: 1763
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Assigning a value to a function does not produce a compilation error

Post by SARG »

Same problem with a sub.

Code: Select all

Function Foo(ByVal param As Integer)As Integer
   Return 2
End Function

sub sFoo(ByVal param As Integer)
End sub

Foo(3) = 5
sfoo(3)= 5

sleep
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Assigning a value to a function does not produce a compilation error

Post by fxm »

This behavior only occurs for procedures with one and only one parameter.
This makes me think of functions returning by reference with one and only one parameter that must absolutely be surrounded by parentheses for the assignment.
Ultimately, the parser does not like this type of one-parameter syntax, on the left side of assignment expression.
deltarho[1859]
Posts: 4305
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Assigning a value to a function does not produce a compilation error

Post by deltarho[1859] »

Of course what we should be getting is

Code: Select all

error 3: Expected End-of-Line, found '=' 
which we would get with two or more parameters.

I wouldn't have thought it too difficult to correct the parser.
coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Assigning a value to a function does not produce a compilation error

Post by coderJeff »

Careful, '=' can be assignment or comparison for equality.
x = foo( (3) = 5 )

Also, return values from functions can be discarded:
foo( (3) = 5 )

And, parentheses are optional for procedures used as statements
foo (3) = 5

So in the example given in the OP, it's not an assignment. "(3)=5" is an expression passed to the first argument of foo() and the result discarded.
erik
Posts: 39
Joined: Dec 28, 2020 17:27
Location: Krasnoyarsk
Contact:

Re: Assigning a value to a function does not produce a compilation error

Post by erik »

To prevent such cases, I dream of a restriction in the language so that functions can only be called with parentheses and returned values from functions can not be discarded.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Assigning a value to a function does not produce a compilation error

Post by fxm »

You dream of going back and even more!
See below an extract from 'changelog.txt':
Version 1.00.0 (former 0.91.0):

.....
[added]
.....
- Any function result may now be ignored at the call site (simply by not using the function result in any expression or assignment). Previously this was only allowed for functions returning integer types or pointers.
.....
Post Reply