0

I have a program that calls functions within the same program but I want to declare and initialize the variables on the top and access them at the end of the program.

!Declaration 
INTEGER TOPCHORD,BOTCHORD, SUPPS, PURLINS;

!Initialization
! Define Layers for connecting lines
TOPCHORD = 32
BOTCHORD = 32
SUPPS    = 36
PURLINS  = 30

INTEGER FUNCTION IFLANGE1(IEND1,IEND2,ISUP)

    IFLANGE1=TOPCHORD

    SELECT CASE(IEND1)
        CASE(2,4,6,8,9,10)
            IFLANGE1=BOTCHORD
            IF(ISUP.EQ.1)IFLANGE1=SUPPS
    END SELECT

    SELECT CASE(IEND2)
        CASE(2,4,6,8,9,10)
            IFLANGE1=BOTCHORD
            IF(ISUP.EQ.1)IFLANGE1=SUPPS
    END SELECT

    RETURN
END 

I get error that variable 'SUPPS' referenced but not set

francescalus
  • 30,576
  • 16
  • 61
  • 96
Isaac Sekamatte
  • 5,500
  • 1
  • 34
  • 40
  • What compiler do you use to try to compile your example? Is this really the exact code that you're using (or reproduces exactly the error)? When I try to compile this I get a very different error message from the compiler. – d_1999 May 04 '17 at 10:19
  • The general answer to your question is - you don't. Usage of global variables is an antipattern in any of the modern programming language due to the nasty and unpredictable behavior. Better to replace them with local ones and pass them to procedures where you want to modify them, or assign the return value of a function to them. – Chaosit May 04 '17 at 11:53
  • as shown the declarations at the top are not global, but local to your main program. There really isnt enough here to see what you mean by global – agentp May 04 '17 at 11:58

1 Answers1

1

You might want to have a look at the documentation - you're missing a couple of important statements. (Also try to always include implicit none -- this is very helpful for catching certain issues).

A slightly modified code that should compile is

!Declaration 
INTEGER TOPCHORD,BOTCHORD, SUPPS, PURLINS;

!Initialization
! Define Layers for connecting lines
TOPCHORD = 32
BOTCHORD = 32
SUPPS    = 36
PURLINS  = 30

contains !Indicate that the program unit contains other procedures
  INTEGER FUNCTION IFLANGE1(IEND1,IEND2,ISUP)

      IFLANGE1=TOPCHORD

      SELECT CASE(IEND1)
          CASE(2,4,6,8,9,10)
              IFLANGE1=BOTCHORD
              IF(ISUP.EQ.1)IFLANGE1=SUPPS
      END SELECT

      SELECT CASE(IEND2)
          CASE(2,4,6,8,9,10)
              IFLANGE1=BOTCHORD
              IF(ISUP.EQ.1)IFLANGE1=SUPPS
      END SELECT

      RETURN
  END FUNCTION !End the function definition
END !This is a required end statement to say we've reached the end of the program

This isn't the nicest style etc. but has the minimum number of changes to compile.

For any slightly more complex program I would strongly recommend using modules for storing variables (and also procedures) that you want to access from different parts of the code.

Graham
  • 7,431
  • 18
  • 59
  • 84
d_1999
  • 854
  • 6
  • 16