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 

1 Answer 1

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

2
  • Thank you very much. One last question - what is end if (what does it do)?
    – florosus
    Commented Aug 23, 2013 at 8: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: freebasic.net/wiki/wikka.php?wakka=KeyPgEndif
    – MrSnrub
    Commented Aug 23, 2013 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.