Freebasic Compilation Fail

397 Views Asked by At

I've just started learning BASIC AND using Stackoverflow. This is my code in FBIDE. The error messages are :

42 variable not declared : var1 in 'input "Enter Function Number" ;var1 /
-
32 expected 'END IF' found 'end' in 'end sub'/
-
32 expected 'END IF' in 'end sub'
-

Code:

declare sub premain
declare sub main
dim var1 as integer
premain
sub premain
  print "EMC ALPHA v1.0"
  main
end sub 

sub main
   print "Functions:"
   print "1.Add"
   print "2.Subtract"
   print "3.Multiply"
   print "4.Divide"

   input "Enter Function Number" ;var1
   if var1=1 then
      print "HElo"
end sub 
1

There are 1 best solutions below

2
MrSnrub On

In your program the variable var1 is declared in the main program scope. This variable will not be accessible in sub programs (procedures: SUB or FUNCTION), unless you use the SHARED keyword. Then the variable would become globally available in your program.

The better way is to use local variables:

declare sub premain
declare sub main

premain
sleep: end


sub premain
  print "EMC ALPHA v1.0"
  main
end sub 

sub main
   print "Functions:"
   print "1.Add"
   print "2.Subtract"
   print "3.Multiply"
   print "4.Divide"
   '****vv HAVE A LOOK HERE vv****
   dim var1 as integer
   input "Enter Function Number" ;var1
   if var1=1 then
      print "HElo"
   end if   '<== this was missing, too.  ***** ("Expected END IF")
end sub 

Global variables (created by SHARED) should only rarely be used, for example for program-wide configuration / settings, e.g. the user's selected language in a multilingual application.

Moreover, your program was missing an END IF (fixed in the code snippet above in my posting).