Cannot Create Subroutine with string parameter

New to FreeBASIC? Post your questions here.
Post Reply
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Cannot Create Subroutine with string parameter

Post by integer »

There is something wrong, but I cannot understand it.
I can create a subroutine using doubles and integers
but the attempt at using a string just doesn't work.

What is wrong?
I do not know what default suffix is being intended.
Why is the string described as mis matched in the sub?

Code: Select all

dim as string JobFileName         'this is line 1
JobFileName="Able"

DECLARE SUB CreateNewJobFile (byval JobFileName as string)
'DECLARE SUB CreateNewJobFile (JobFileName)

SUB CreateNewJobFile (JobFileName)
'SUB CreateNewJobFile JobFileName
		MKDIR JobFileName
		PRINT "The directory has been created."
		CHDIR JobFileName      
      exit sub
end sub

'main
CreateNewJobFile (JobFileName)

end      'this is line 18



/' 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
COMPILE RESULTS ATTEMPT: 1
using this
DECLARE SUB CreateNewJobFile (byval JobFileName as string)
'DECLARE SUB CreateNewJobFile (JobFileName)

SUB CreateNewJobFile (JobFileName)
'SUB CreateNewJobFile JobFileName

================
Command executed:
"C:\Program Files\FreeBASIC\fbc.exe" "C:\crap.bas" -ex

Compiler output:
C:/crap.bas(7) error 136: Default types or suffixes are only valid in -lang deprecated or qb, found ')' in 'SUB CreateNewJobFile (JobFileName)'
C:/crap.bas(7) error 55: Type mismatch, at parameter 1 (JobFileName) of CreateNewJobFile() in 'SUB CreateNewJobFile (JobFileName)'
C:/crap.bas(9) error 55: Type mismatch, at parameter 1 of MKDIR() in 'MKDIR JobFileName'
C:/crap.bas(11) error 55: Type mismatch, at parameter 1 of CHDIR() in 'CHDIR JobFileName'

Results:
Compilation failed

System:
FBIde: 0.4.6
fbc:   FreeBASIC Compiler - Version 0.17 (03-24-2007) for win32 (target:win32)
OS:    Windows XP (build 2600, Service Pack 2)
===============

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
COMPILE RESULTS ATTEMPT: 2

'DECLARE SUB CreateNewJobFile (byval JobFileName as string)
DECLARE SUB CreateNewJobFile (JobFileName)

SUB CreateNewJobFile (JobFileName)
'SUB CreateNewJobFile JobFileName

================
Command executed:
"C:\Program Files\FreeBASIC\fbc.exe" "C:\crap.bas" -ex

Compiler output:
C:/crap.bas(5) error 136: Default types or suffixes are only valid in -lang deprecated or qb, found ')' in 'DECLARE SUB CreateNewJobFile (JobFileName)'
C:/crap.bas(7) error 136: Default types or suffixes are only valid in -lang deprecated or qb, found ')' in 'SUB CreateNewJobFile (JobFileName)'
C:/crap.bas(9) error 55: Type mismatch, at parameter 1 of MKDIR() in 'MKDIR JobFileName'
C:/crap.bas(10) error 3: Expected End-of-Line, found 'Command' in 'PRINT "The directory has been created."Command executed:'
C:/crap.bas(11) error 55: Type mismatch, at parameter 1 of CHDIR() in 'CHDIR JobFileName'
C:/crap.bas(16) error 55: Type mismatch, at parameter 1 (JobFileName) of CREATENEWJOBFILE() in 'CreateNewJobFile (JobFileName)'

Results:
Compilation failed

System:
FBIde: 0.4.6
fbc:   FreeBASIC Compiler - Version 0.17 (03-24-2007) for win32 (target:win32)
OS:    Windows XP (build 2600, Service Pack 2)


'/
Last edited by integer on Apr 07, 2007 0:29, edited 1 time in total.
Peter
Posts: 66
Joined: May 29, 2006 22:16

Post by Peter »

You need to state the type of JobFileName in both the sub and the declaration of the sub

Change

Code: Select all

Sub CreateNewJobFile (JobFileName)
to

Code: Select all

Sub CreateNewJobFile (JobFileName as string)
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Post by integer »

Code: Select all

Command executed:
"C:\Program Files\FreeBASIC\fbc.exe" "C:\crap2.bas"

Compiler output:
C:/crap2.bas(7) : error 56: Type mismatch, at parameter 1 (JobFileName) of CreateNewJobFile()
Sub CreateNewJobFile (JobFileName as string)
                                           ^

Results:
Compilation failed

System:
FBIde: 0.4.6
fbc:   FreeBASIC Compiler - Version 0.16 for win32 (target:win32)
OS:    Windows XP (build 2600, Service Pack 2)
It looks as if the only way to do this is to make ALL the strings that
need to be used in any sub modules as SHARED. That way there
is no worry about mis-matched types.

Thanks for trying.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Cannot Create Subroutine with string parameter

Post by counting_pine »

It's probably not working for you because "byval x as string" and "x as string" don't do exactly the same thing. Try removing the "byval" from the Declare:

Code: Select all

dim as string JobFileName
JobFileName="Able"

DECLARE SUB CreateNewJobFile (JobFileName as string)

SUB CreateNewJobFile (JobFileName as string)
		MKDIR JobFileName
		PRINT "The directory has been created."
		CHDIR JobFileName      
      exit sub
end sub

'main
CreateNewJobFile (JobFileName)

end
The KiD
Posts: 1
Joined: Apr 07, 2007 0:34
Location: Charleston, SC
Contact:

There are a few reasons as to why it's not working

Post by The KiD »

The code below works and is far less to type out. Having spent just a couple hours with FB, I do see it as strange as to why there is a difference between strings and integers being passed to subs. Regardless from what I can tell, this works:

Code: Select all

Declare Sub CreateNewJobFile (JobFileName As String)

'main
CreateNewJobFile ("Able")
sleep
End

Sub CreateNewJobFile (JobFileName As string)

                Mkdir JobFileName
                Print "The directory has been created."
                Chdir JobFileName      

End Sub
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: There are a few reasons as to why it's not working

Post by counting_pine »

The KiD wrote:I do see it as strange as to why there is a difference between strings and integers being passed to subs.
Integer is the default data type in FB. If you don't specify a type then it will make it an Integer.

Code: Select all

declare function myfn(i)

function myfn(byref i as integer) as integer
end function
In the example, I've been as implicit as I can in the Declare, and as explicit as I can in the function header. But, because of the defaults, both look exactly the same to FB, so there are no errors. If I were to change Integer to String/Double/etc, or ByRef to ByVal, then it would fail.
Post Reply