1

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 
MPelletier
  • 16,256
  • 15
  • 86
  • 137
florosus
  • 25
  • 5

1 Answers1

2

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).

MrSnrub
  • 1,123
  • 1
  • 11
  • 19
  • Thank you very much. One last question - what is end if (what does it do)? – florosus Aug 23 '13 at 08:18
  • `END IF` is used to end a block of conditional program parts. Everything between `IF ... THEN` and `END IF` only executes if the condition (e.g. `var1=1`) is true. Have a look here: http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgEndif – MrSnrub Aug 23 '13 at 09:14