B4J Question Call a function but not assigning the returned value to a variable

aeric

Expert
Licensed User
Longtime User
If I have a function that suppose to return a value but I call it without assigning it to a variable, is it "fine" or a mistake?

Let say I have this function:
B4X:
Sub MyMap As Map
    AnotherValue = 6
    Return CreateMap("key": 1)
End Sub

B4X:
Sub Process_Globals
    Private AnotherValue As Int
End Sub

Sub AppStart (Args() As String)
    Log(AnotherValue)
    
    ' Normal way
    'Dim map1 As Map = MyMap
    'Log(map1)
    
    ' Not sure
    MyMap
    
    Log(AnotherValue)
End Sub
 
Solution
It is totally fine. I believe I read from forum that even a func in B4X has no returned value it actually has a hidden return value in string type (correct me if wrong).

aeric

Expert
Licensed User
Longtime User
I give another example.

Let say I declare a variable name DB as an object of MiniORM class.

B4X:
Sub Class_Globals
    Private DB As MiniORM
End Sub

I have the following code:
B4X:
Private Sub GetCategory (id As Long)
    ' #Version = v2
    ' #Desc = Read one Category by id
    ' #Elements = [":id"]

    DB.Table = "tbl_category"
    DB.Find(id)
    If DB.Found Then
        HRM.ResponseCode = 200
        HRM.ResponseObject = DB.First
    Else
        HRM.ResponseCode = 404
        HRM.ResponseError = "Category not found"
    End If
    DB.Close
    ReturnApiResponse
End Sub

On line #7, DB.Find(id) should return a Map.

But I don't assign it to a variable because I don't need the return value as a Map afterwards.

I can use another Property DB.Found on line #8 which return the Boolean value which is updated internally in the MiniORM class.

1713858730976.png
 
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
It is totally fine. I believe I read from forum that even a func in B4X has no returned value it actually has a hidden return value in string type (correct me if wrong).
 
Upvote 1
Solution

PaulMeuris

Active Member
Licensed User
In the first example the word MyMap is ambiguous because it refers to a subroutine name and to a name of a map.
This is a little bit confusing.

B4X:
Sub Process_Globals
    Private AnotherValue As Int
End Sub

Sub AppStart (Args() As String)
    Log(AnotherValue)
    MyMap
    Log(AnotherValue)
    Log(MyMap)
    Log("value:" & MyMap.Get("key"))
    AnotherValue = 7
    Log(MyMap)
    Log("value:" & MyMap.Get("key2"))
    Dim value As Int = MyMap.Get("key2")
    Log(value)
End Sub

Sub MyMap As Map
    If AnotherValue = 7 Then Return CreateMap("key2": 10)
    AnotherValue = 6
    Return CreateMap("key": AnotherValue)
End Sub
Waiting for debugger to connect...
Program started.
0
6
(MyMap) {key=6}
value:6
(MyMap) {key2=10}
value:10
10
Program terminated (StartMessageLoop was not called).

In the second example the method name DB.Find(id) can not be used as a map name, i think.
But why do you want to use a return value in that method if you are not going to use it later?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
In the first example the word MyMap is ambiguous because it refers to a subroutine name and to a name of a map.
This is a little bit confusing.

B4X:
Sub Process_Globals
    Private AnotherValue As Int
End Sub

Sub AppStart (Args() As String)
    Log(AnotherValue)
    MyMap
    Log(AnotherValue)
    Log(MyMap)
    Log("value:" & MyMap.Get("key"))
    AnotherValue = 7
    Log(MyMap)
    Log("value:" & MyMap.Get("key2"))
    Dim value As Int = MyMap.Get("key2")
    Log(value)
End Sub

Sub MyMap As Map
    If AnotherValue = 7 Then Return CreateMap("key2": 10)
    AnotherValue = 6
    Return CreateMap("key": AnotherValue)
End Sub


In the second example the method name DB.Find(id) can not be used as a map name, i think.
But why do you want to use a return value in that method if you are not going to use it later?
DB.Find() was created earlier then DB.Found is added after that.
So before DB.Found is introduced, I used to check the Map is Initialized or it's Size greater than 0 to be specific.

My question is whether this is correct?
Dim map1 As Map = MyMap
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
It doesn't matter what is the data type. I just give an example using Map.

B4X:
Sub GetValue As Int
    Return 9
End Sub

Log(GetValue) ' <-- I consider this is returning value

GetValue ' <-- This is not, is this correct?

edit: It seems that by just calling GetValue, it is not doing "anything"

edit: and the compiler allows it without showing a warning.
 
Upvote 0

PaulMeuris

Active Member
Licensed User
The Log function calls the subroutine and uses the return value to display in the log window.
The call to the subroutine directly will generate a return value variable behind the scenes.
You have no reference to that variable unless you collect the reference by doing:
B4X:
    myvalue = GetValue
    Log(myvalue)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Yes, my point is can I let the value "hanging" or just keep inside a memory without consume it?
If I am not mistaken last time I get error in certain programming language IDE if I don't use it.
 
Upvote 0
Top