Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 17:01 27 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Function must be called with 1 argument

Author Message
SteveP
Newbie

Joined: 21/03/2013
Location: United States
Posts: 19
Posted: 06:29pm 11 Jul 2013
Copy link to clipboard 
Print this post

It seems a Function must be called with at least one argument.

Var1=2
Print MyFcn

Function MyFcn(arg1)
Print "inside fcn"
MyFcn = Var1 * 10
End Function

Calling this function as MyFcn does not print the "inside fcn" and the return value is 0
Calling it as MyFcn(1) does print "inside fcn" and returns 20

Declaring MyFcn as Function MyFcn instead of Function MyFcn(arg1) also does not execute and returns 0

Steve
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 06:51pm 11 Jul 2013
Copy link to clipboard 
Print this post

When you used Print MyFun the interpreter thought that MyFun was a variable so it created it and returned its initial value (zero).

You should use: Print MyFcn()

If you want to declare a function with no arguments you should use:
Function MyFcn()
...
End Function

Geoff
Geoff Graham - http://geoffg.net
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 06:53pm 11 Jul 2013
Copy link to clipboard 
Print this post

Functions are not subroutines so you always need the ().
It should actually throw an error instead of returning 0.

MyFunc is a variable and MyFunc(arg1) a function, both existing at the same time.
Very undesireable.
Tested like this
[code]
Var1=2

'Both lines should fail as MyFcn is defined as a function and no more variable with the same name should be possible.
MyFcn = 10 'Error: Cannot assign a value to a function
Print MyFcn 'Error: Function should be called with ()

'The right way to call a function 'with or without arguments is fine'
Print MyFcn()

Function MyFcn(arg1)
Print "inside fcn"
MyFcn = Var1 * 10
End Function
[/code]
It will output:
10
inside fcn
4
Edited by TZAdvantage 2013-07-13
Microblocks. Build with logic.
 
shoebuckle
Senior Member

Joined: 21/01/2012
Location: Australia
Posts: 189
Posted: 08:55pm 11 Jul 2013
Copy link to clipboard 
Print this post

  TZAdvantage said   MyFunc is a variable and MyFunc(arg1) a function, both existing at the same time.
Very undesireable.


The language is so forgiving that even the following use of the word "OUTPUT" is tolerated, not that I am suggesting you write code like this. It is gross but interesting!

Output$="output.tst"
data 1,2,3,4,5
Data "one","two","three","four","five"
Dim output(5)
Dim Output$(5)
for I=1 to 5
Read output(i)
Next
For i=1 to 5
read Output$(i)
Next
output=1
Open output$ for output as output
for i=1 to 5
print #output, Output(i),Output$(i)
Next
Close output


Cheers,
Hugh
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 10:44pm 11 Jul 2013
Copy link to clipboard 
Print this post

:0
That is some output.
No need to obfuscate that piece of code.

Flexibility is good. Forgiving also some times.
Difficult to draw a line what should and should not be acceptable.

My 'line' is allowing variable names and function names to be the same.
And i think BASIC should be stricter then other languages because it is one of the languages people use to learn programming. Errors will then be very helpful.
With interpreted languages anything is a 'runtime' error.
One of the shortcoming because errors can stay hidden a long time.
I think that the OP's case should be handled as an error, not a feature.

In the case of MMBasic it is possible to detect variable names and function names because it does a scan for subroutine and function names when you run a program.
This scan fills a lookup table to speed up execution.
If you have to check every variable name against this list you would be able to detect the variable name/function name but at some execution cost.
Microblocks. Build with logic.
 
isochronic
Guru

Joined: 21/01/2012
Location: Australia
Posts: 689
Posted: 04:14am 12 Jul 2013
Copy link to clipboard 
Print this post

If I remember correctly, (in languages in general) :

if there is no error a function should return a definite value.
The datatype of the returned value is usually specified, which allows
the returned value to be checked against the datatype for error trapping,
important for robust operation.
So a declaration of some sort, using the function name, is required to specify the return datatype. So, allowing a function and variable to have the same name invites bugs and gremlins to creep in via the ambiguity.
(I just use aerogard to be sure, to be sure )
 
djuqa

Guru

Joined: 23/11/2011
Location: Australia
Posts: 447
Posted: 02:04pm 12 Jul 2013
Copy link to clipboard 
Print this post

Best programming practice would be to have a convention naming your variables & functions so there can be no confusion.
VK4MU MicroController Units

 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024