16. °øÅë ºí·Ï (Common blocks)

Fortran 77¿¡´Â global º¯¼ö Áï ¿©·¯ rogram units (subroutines)¿¡¼­ °øÅëÀ¸·Î »ç¿ëÇÏ´Â º¯¼ö°¡ ¾ø´Ù. Áö±Ý±îÁö º¸¾Æ¿Â °Íó·³ subroutine »çÀÌ¿¡¼­ Á¤º¸¸¦ Àü´ÞÇÏ´Â À¯ÀÏÇÑ ¹æ¹ýÀº subroutine parameter ¸ñ·ÏÀ» »ç¿ëÇÏ´Â °ÍÀÌ´Ù. ¿©·¯ subroutines¿¡¼­ ¸¹Àº parameters¸¦ °øÀ¯ÇؾßÇÒ °æ¿ì µî¿¡´Â ÀÌ·± Á¦ÇÑÀº ºÒÆíÇÑ´Ù. ±×·¯ÇÑ °æ¿ì °øÅë ºí·Ï common blockÀ» »ç¿ëÇÒ ¼ö ÀÖ´Ù. ÀÌ´Â ¾î¶² subroutine »çÀÌ¿¡»ç ¾î¶² º¯¼ö¸¦ °øÀ¯Çϴ°¡¸¦ Á¤ÇÏ´Â ¹æ¹ýÀÌ´Ù. ±×·¯³ª ÀϹÝÀûÀ¸·Î common blocks »ç¿ëÀº ÃÖ¼Ò·Î ÇÏ¿©¾ß ÇÑ´Ù.

Example

¿©·¯ subroutines¿¡¼­ ÇÊ¿äÇÑ µÎ º¯¼ö alpah¿Í beta°¡ ÀÖ´Ù. ´ÙÀ½Àº common blockÀ» »ç¿ëÇÏ¿© ¾î¶»°Ô ÇÒ ¼ö Àִ°¡¸¦ º¸¿©ÁÖ´Â ¿¹ÀÌ´Ù.
      program main
      some declarations
      real alpha, beta
      common /coeff/ alpha, beta

      statements
      stop
      end

      subroutine sub1 (some arguments)
      declarations of arguments
      real alpha, beta
      common /coeff/ alpha, beta

      statements
      return
      end

      subroutine sub2 (some arguments)
      declarations of arguments
      real alpha, beta
      common /coeff/ alpha, beta

      statements
      return
      end
¿©±â¼­ coeffÀ̶ó´Â À̸§ÀÇ common blockÀ» Á¤ÀÇÇÑ´Ù. common blockÀÇ ³»¿ëÀº alpha¿Í beta µÎ º¯¼öÀÌ´Ù. common block¿¡´Â ¿©·¯ °³ÀÇ º¯¼ö¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù. ¸ðµÎ°¡ °°Àº ÇüÅÂÀÏ ÇÊ¿ä´Â ¾ø´Ù. common block ¾ÈÀÇ º¯¼ö Áß ¾î´À °ÍÀ» ÇÊ¿ä·Î ÇÏ´Â ¸ðµç subroutine ¿¡¼­ Àüü blockÀ» Á¤ÀÇÇÏ¿©¾ß ÇÑ´Ù. declare the whole block.

ÀÌ ¿¹¿¡¼­´Â alpah¿Í beta¸¦ parameter(argument)·Î ³Ñ°ÜÁÜÀ¸·Î½á common blockÀ» »ç¿ëÇÏÁö ¾ÊÀ» ¼öµµ ÀÖ´Ù. ÁÁÀº ¹æ¹ýÀº °¡´ÉÇÑ common blockÀ» »ç¿ëÇÏÁö ¾Ê´Â °ÍÀÌ´Ù. ±×·¯³ª ´Ù¸¥ ¹æ¹ýÀÌ ¾ø´Â °æ¿ì°¡ °£È¤ ÀÖ´Ù.

Syntax

      common / name / list-of-variables
´ÙÀ½À» ¾Ë¾Æ¾ß ÇÑ´Ù. À̸¦ ÀÌÇØÇϱâ À§ÇÏ¿© ´ÙÀ½ ¿¹¸¦ »ìÆì º»´Ù.
      subroutine sub3 (some arguments)
      declarations of arguments
      real a, b
      common /coeff/ a, b

      statements
      return
      end
ÀÌ ¼±¾ðÀº alpha¿Í beta¸¦ »ç¿ëÇÑ ¾ÕÀÇ ¿¹¿Í °°´Ù. È¥µ¿À» ÇÇÇϱâ À§ÇÏ¿© °°Àº common block¿¡¼­´Â º¯¼ö À̸§À» °°°Ô ÇÒ °ÍÀ» ±ÇÇÑ´Ù. ´ÙÀ½Àº ¾ÆÁÖ ³ª»Û °æ¿ìÀÇ ¿¹ÀÌ´Ù.
      subroutine sub4 (some arguments)
      declarations of arguments
      real alpha, beta
      common /coeff/ beta, alpha

      statements
      return
      end
¿©±â¼­ alpha´Â main programÀÇ betaÀÌ°í beta´Â main programÀÇ alphaÀÌ´Ù. ÀÌ·¯ÇÑ °ÍÀ» º»´Ù¸é ÀÌ´Â ¾Æ¸¶ ½Ç¼öÀÏ °ÍÀÌ´Ù. ±×·± À߸øÀº ã±â Èûµé´Ù.

°øÅë ºí·Ï ¾ÈÀÇ ¹è¿­ (Arrays in common blocks)

common block¿¡´Â ¹è¿­µµ »ç¿ëÇÒ ¼ö ÀÖ´Ù. ±×·¯³ª ±ÇÇÏÁö´Â ¾Ê´Â´Ù. ÁÖµÈ ÀÌÀ¯´Â À¶Å뼺¶§¹®ÀÌ´Ù. ¾Æ·¡´Â ¿Ö ³ª»Û°¡¸¦ º¸¿©ÁÖ´Â ¿¹ÀÌ´Ù. main program¿¡¼­ ´ÙÀ½°ú °°ÀÌ ¼±¾ðÇÏ¿´´Ù°í ÇÏÀÚ.
      program main
      integer nmax
      parameter (nmax=20)
      integer n
      real A(nmax, nmax)
      common /matrix/ A, n, nmax
common block¿¡´Â ¿ì¼± AÀÇ ¸ðµç ¿ø¼Ò, ±×¸®°í Á¤¼ö n°ú nmaxÀÌ Æ÷ÇԵǾî ÀÖ´Ù. ÀÌÁ¦ matrix A¸¦ ¾î¶² subroutine¿¡¼­ »ç¿ëÇÏ°íÀÚ ÇÒ °æ¿ì ¸ðµç subroutines¿¡ °°Àº ¼±¾ðÀ» ³Ö¾î¾ß ÇÑ´Ù. Áï,
      subroutine sub1 (...)
      integer nmax
      parameter (nmax=20)
      integer n
      real A(nmax, nmax)
      common /matrix/ A, n, nmax
common block¿¡ °¡º¯ Â÷¿ø ¹è¿­Àº »ç¿ëÇÒ ¼ö ¾ø´Ù. µû¶ó¼­ nmaxÀÇ °ªÀº main program¿¡¼­¿Í Á¤È®ÇÏ°Ô ÀÏÄ¡ÇÏ¿©¾ß ÇÑ´Ù. matrixÀÇ Å©±â´Â compileÇÒ ¶§ ¾Ë·ÁÁÖ¾î¾ß Çϱ⿡ nmax´Â parameter ¹®Àå¿¡¼­ Á¤ÀÇÇÏ¿©¾ß¸¸ ÇÑ´Ù. nmax´Â common block¿¡ Àֱ⿡ subroutine¿¡¼­ parameter ¹®À» »ç¿ëÇÏÁö ¾Ê°í ½ÍÀ» Å×Áö¸¸ ÀÌ´Â ±ÔÄ¢¿¡ ¾î±ß³ª´Â ÀÏÀÌ´Ù.

´ÙÀ½Àº º¸Åë common block¿¡ ¹è¿­À» ³Ö¾îµµ ¾ò´Â °ÍÀÌ ¾ø´Ù´Â °ÍÀ» º¸¿© ÁÖ´Â ¿¹ÀÌ´Ù. µû¶ó¼­ Fortran 77¿¡¼­ ¹Ù¶÷Á÷ÇÑ ¹æ¹ýÀº ¹è¿­À» argument·Î (leading Â÷¿ø°ú ÇÔ²²)subroutine¿¡ ³Ñ°Ü ÁÖ´Â °ÍÀÌ´Ù.


Exercises

Exercise A
common blockÀ» »ç¿ëÇÏÁö ¾Êµµ·Ï ´ÙÀ½ ÇÁ·Î±×·¥°ú ºÎ ÇÁ·Î±×·¥À» ´Ù½Ã ÀÛ¼ºÇ϶ó. Àüü ±¸Á¶´Â À¯ÁöÇϵµ·Ï ÇÑ´Ù.
      program main
      real origo(3), x(3)
      real d, dist
      common /silly/ origo
      
      read(*,*) origo(1), origo(2), origo(3)
  10  continue
         read(*,*) x(1), x(2), x(3)
         d = dist(x)
         write(*,*) 'The distance is ', d
      if (x(1).ge.0.) goto 10

      stop
      end
      
      real function dist (x)
      real x(3)
      real x0, y0, z0
      common /silly/ x0, y0, z0

      dist = sqrt((x(1)-x0)**2 + (x(2)-y0)**2 + (x(3)-z0)**2)

      return
      end


[Fortran Tutorial]
Ãæ³²´ëÇб³ ¹°¸®Çаú ¿À º´¼º