Sub versus Function ? [SOLVED]

New to FreeBASIC? Post your questions here.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Sub versus Function ? [SOLVED]

Post by deltarho[1859] »

fxm wrote:The first "mul" calls the mul() function as a sub
It seems to me that there is more to this than meets the eye.

If we execute mul only we get 8 - which is to be expected.

However, if we execute 'mul + 21' we do not get 29 but 84 which is 21 * 4, as if x is passed as 21. On the other hand, if we execute 'Print mul + 21' we get two lines, 8 and 29.

How about, 'mul + 21 + 13'. I would expect 42, the answer to life and so on, but we get 136, as if x is passed 34 (21 + 13). On the other hand, if we 'Print mul + 21 + 13' we get two lines, 8 and 42.

I cannot find an explanation in the docs. Is there one?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Sub versus Function ? [SOLVED]

Post by fxm »

'mul' called as a sub (because put at beginning), the parentheses around parameters are not mandatory:
- 'mul + a' is parsed as 'mul (+ a)', let as mul(a)
- 'mul + a + b' is parsed as 'mul (+ a + b)', let as mul(a+b)

'mul' called as a function (because not put at beginning), the parentheses around parameters are mandatory (no parentheses => no parameters):
- 'print mul + a + b' is parsed as 'print mul() + a + b'

The dodicat example uses the parser's shortcomings in relation to an exaustive control of the syntax.
This is why I can only recommend using right syntaxes and without ambiguity such as those I proposed above right.

By always using parentheses (even empty) after any procedure name, this defines the beginning and the end of the arguments field unambiguously.
(only exception for a get-property without index parameter)

It is normal that the documentation does not describe the weird behavior of the parser versus an atrocious syntax, but rather the right syntax to always apply in the code.
Last edited by fxm on Sep 30, 2017 20:59, edited 4 times in total.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Sub versus Function ? [SOLVED]

Post by deltarho[1859] »

Thanks, fxm.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Sub versus Function ? [SOLVED]

Post by dodicat »

Functions need brackets around the parameters

Subs don't

The + sign is a confuser

Code: Select all



'==============
declare function mul(x as double=2,y as double=4) as double

    


function mul(x as double,y as double) as double
    print x*y '<-----------------------------------------
    return x*y'                                          |
end function  '                                          |
              '                                          |
              '                                          |
dim as double x '                                        |
x=mul(10,4) '   need brackets for function             prints 10*4

mul +2+3+5  'sub -- no brackets, looks weird           prints (+2 +3 +5) * 4

'=

mul 10                                                 'print 10*4
'=

' mul +mul +mul  +mul + mul    =    mul 8+8+8+8        prints 32 * 4


sleep



 

 
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Sub versus Function ? [SOLVED]

Post by deltarho[1859] »

dodicat wrote:Subs don't

The + sign is a confuser
<smile>

Code: Select all

mul +2+3+5  'sub -- no brackets, looks weird           prints (+2 +3 +5) * 4
It looks weird becuase we have our Function cap on and see 'mul plus 2 plus 3 plus 5'. It does not look weird if we put our Sub cap on and see the first parameter as +2+3+5 and the second parameter as 4, the default, giving 40.

Code: Select all

mul +2+3+5, +1+9
and we get 100. What else? <laugh>

Code: Select all

mul , +1+9
gives 20 from the first parameter as 2, the default, and the second parameter as +1+9.

So, the parsing is OK.

However, we should take fxm's lead and, with the last example, use 'mul( , +1+9 ).
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Sub versus Function ? [SOLVED]

Post by fxm »

It is strange that people criticize the documentation by saying that it is a bit too technical and complicated at the beginning, and that they like to play at the same time with twisted and ambiguous syntaxes that exploit parser defects.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Sub versus Function ? [SOLVED]

Post by deltarho[1859] »

I am currently reading a book entitled 'Thinking fast and slow' by Dr Daniel Kahneman, which was published in 2011 when he was 77. Effectively, the fast bit is intuition and the slow bit is logic. If we employed intuition only we would probably get run over by a vehicle before the day was out. If we employed logic only we wouldn't get over the threshold before it was time to go back to bed. That is my interpretation so far. It seems highly unlikely that any one person gets the perfect balance all the time. Having said that it seems that nature is very tolerant because otherwise I am unable to figure out how Homo sapiens have gotten this far. I have never advocated referendums being carried out in democracies. I am now convinced that they should never be undertaken.
fxm wrote:It is strange that people criticize the documentation ...
This book tells me that there is nothing strange about it at all - par for the course, more like.

It seems that many folk are lazy thinkers and shy away from an 'uphill struggle' if a level walk presents itself. Intuition is a level walk and logic is an uphill struggle.

Intuition is very powerful but it is not illogical, it is irrational - as is lateral thinking. We can very quickly side step to a solution. However, we can very easily side step into a pot hole.

So, a lazy thinker may say one thing and then contradict themselves by saying another thing later on and be totally oblivious to the fact that they have contradicted themselves because they employed intuition in both cases. They would have to use logic to see the contradiction - oh dear, an uphill struggle. <smile>

It seems that intuition is the default state. The question is when should we call upon logic? That in itself requires logic so a really lazy thinker is bound to get run over. So, how do we get people to stop being lazy thinkers? The book has not told me yet and I am not sure that it will.

Here is an example of intuitive thinking. Don't think about it - come up with an answer quickly.

A bat and ball cost $1.10.
The bat costs one dollar more than the ball.
How much does the ball cost?

Most folk will say 10 cents - I did. Wrong!

Treat the first two as simultaneous equations and solve. The answer is 5 cents.

There is intuition and logic in this thread. I treated 'mul + 21' intuitively until dodicat pointed out that "The + sign is a confuser". Logic may have kicked in had it been 'mul 21'. There again it may not have. <smile>

@Trinity. Folks who come to the forum with questions without researching elsewhere first are lazy thinkers and you are well justified in 'kicking their butt'. The problem is that may have no effect. It reminds me of the statement "The more incompetent someone is the more likely they are to overestimate their competence." How do we correct that? Now, there is a question.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Sub versus Function ? [SOLVED]

Post by fxm »

Intuition is very useful for all that is creativity, even in programming to find the way to solve a given problem, as in geometry.
But once the way is traced, the coding must be rigorous with a syntax both clear (unambiguous) and efficient, as in algebra.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Sub versus Function ? [SOLVED]

Post by deltarho[1859] »

Intuition and thinking are very different and are orthogonal in Jungian Typology and, for me, is what makes coding so much fun. Being adept at both is rare and there are some here, and at PowerBASIC it must be said, who produce elegant code; pleasingly ingenious and simple. What I find refreshing is that most of these folk have no idea how good they are.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Sub versus Function ? [SOLVED]

Post by dodicat »

I think Mark Twain wrote:
Sorry my letter to is very long, I didn't have time to write a short one.
Terse code (like the old dos algorithms) is more difficult to produce, yet probably more efficient.
Deltarho[1859]

What's all this bat and ball costing dollars/cents?
I like it OK though.
Maybe you saw this conundrum while on holiday?

Next thing you'll be quoting euros!

fxm
The help page for input(file/I/O) in the .chm file uses
Int(Rnd(0)*42)
which equates to 23
Very confusing for a beginner.
Sorry to be off topic in a [SOLVED] thread by Trinity.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Sub versus Function ? [SOLVED]

Post by fxm »

result = Rnd( seed )
If seed has a value of zero (0.0), the last random number generated is repeated. For any other number a new random number is returned.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Sub versus Function ? [SOLVED]

Post by deltarho[1859] »

@dodicat

Mark Twain? I seems he did but "Pascal's statement is likely the original source of the phrase". Pascal as in Blaise Pascal. I didn't know that when I wrote, many years ago, "Why write a sentence when a paragraph will do". Pipped to the post again - by about three hundred years this time. <smile>
What's all this bat and ball costing dollars/cents?
The book that I am reading was written by an American. I was starting to question the author's premise until I found out that he was awarded a Nobel Memorial Prize in Economic Sciences in 2002 and described as 'Certainly the most important psychologist alive today' by Steven Pinker, a professor at Harvard. My attention has been sharpened. <smile>
Next thing you'll be quoting euros!
I lived in Dundee for twelve months in the early seventies and returned to England with Scottish pound notes. My local news agent would not accept them. I pointed out that he was obliged to accept them but not give them out as change and I would be more than happy to introduce to him to the local magistrates who would confirm that. My bluff worked and he accepted my payment. <Ha, ha>
Post Reply