11. ºÎ ÇÁ·Î±×·¥(Subprograms)

¾î¶² ÇÁ·Î±×·¥ÀÌ ¼ö¹é ÁÙ·Î ±æ¾îÁö¸é µû¶ó°¡±â ¾î·Æ°Ô µÈ´Ù. Fortran codes ½ÇÁ¦ÀÇ °øÇÐÀû ¹®Á¦¸¦ ÇØ°áÇϱâ À§ÇÑ Fortran ÄÚµå´Â ¼ö¸¸ ÁÙ ¾¿ µÇ±âµµ ÇÑ´Ù. e only way to handle such ÀÌ·± ¾öû³­ Äڵ带 ´Ù·ç´Â À¯¸®ÇÑ ¹æ¹ýÀº modular approachÀ» ½á¼­ ÇÁ·Î±×·¥À» ºÎÇÁ·Î±×·¥subprogramsÀ̶ó°í ÇÏ´Â ÀÛÀº ´ÜÀ§ ¿©·¯ °³·Î ³ª´©´Â °ÍÀÌ´Ù.

ºÎ ÇÁ·Î±×·¥À̶õ È®½ÇÇÏ°Ô Á¤ÀÇµÈ ÀÛÀº ¹®Á¦¸¦ ÇØ°áÇÏ´Â (ÀÛÀº) ÄÚµå Á¶°¢ÀÌ´Ù. Å« ÇÁ·Î±×·¥¿¡¼­´Â ´Ù¸¥ ÀڷḦ °®°í °°Àº ¹®Á¦¸¦ Ç®¾î¾ß ÇÏ´Â °æ¿ì°¡ Á¾Á¾ ÀÖ´Ù. ÀÌ·¯ ÀÛ¾÷Àº °°Àº Äڵ带 ¹Ýº¹ÇÏ´Â ´ë½Å¿¡ ºÎÇÁ·Î±×·¥À» »ç¿ëÇÏ¿© ÇØ°áÇÏ¿©¾ß ÇÑ´Ù. °°Àº ºÎÇÁ·Î±×·¥À» ÀÔ·Â ÀڷḸ ´Ù¸£°Ô ÇÏ¿© ¿©·¯ Â÷·Ê »ç¿ëÇÒ ¼ö ÀÖ´Ù.

Fortran¿¡´Â µÎ °¡Áö ÇüÅÂÀÇ ºÎÇÁ·Î±×·¥ - ÇÔ¼ö functions ¿Í ¼­ºê·çƾ subroutines -ÀÌ ÀÖ´Ù.

ÇÔ¼ö (Functions)

Fortran ÇÔ¼ö´Â ¼öÇÐÀÇ ÇÔ¼ö¿Í ¸Å¿ì ºñ½ÁÇÏ´Ù. µÑ ´Ù ÀÏ´ÜÀÇ ÀÔ·Â º¯¼ö (parameters)¸¦ ¹Þ¾Æµé¿© ¾î¶² ÇüÅÂÀÇ °ªÀ» ÁØ´Ù. ¾Õ¿¡¼­ »ç¿ëÀÚ Á¤ÀÇ user defined ºÎÇÁ·Î±×·¥¿¡ ´ëÇÏ¿© »ìÆ캸¾Ò´Ù. Fortran 77 ¿ª½Ã ³»Àå built-in ÇÔ¼ö°¡ ÀÖ´Ù.

´ÙÀ½ÀÇ °£´ÜÇÑ ¿¹´Â ÇÔ¼ö¸¦ »ç¿ëÇÏ´Â ¹æ¹ýÀÌ´Ù.

      x = cos(pi/3.0)
¿©±â¼­ cosÀº cosine ÇÔ¼öÀÌ°í µû¶ó¼­ (pi°¡ ¿Ã¹Ù¸£°Ô Á¤ÀǵǾî ÀÖ´Ù¸é (Fortran 77¿¡´Â ³»ÀåµÈ »ó¼ö´Â ¾ø´Ù.)) x´Â 0.5°¡ µÉ °ÍÀÌ´Ù. Fortran 77¿¡´Â ³»Àå ÇÔ¼ö°¡ ¿©·¯ °¡Áö ÀÖ´Ù. °¡Àå ¸¹ÀÌ »ç¿ëÇÏ´Â °ÍÀ¸·Î´Â:
      abs     absolute value
      min     minimum value
      max     maximum value
      sqrt    square root
      sin     sine
      cos     cosine
      tan     tangent
      atan    arctangent
      exp     exponential (natural)
      log     logarithm (natural)
º¸Åë ÇÔ¼ö´Â ÇüÅ type°¡ ÀÖ´Ù. À§¿¡¼­ ¾ð±ÞÇÑ ´ëºÎºÐÀÇ ³»Àå ÇÔ¼ö´Â ÀϹÝÀûgenericÀÌ´Ù. µû¶ó¼­ À§ ¿¹¿¡¼­ pi¿Í x ´Â realÀ̰ųª double precisionÀÌ´Ù. compiler´Â ÇüŸ¦ Á¶»çÇÏ¿© ¾Ë¸Â´Â (real ¶Ç´Â double precision) cosÀ» »ç¿ëÇÒ °ÍÀÌ´Ù. ºÒÇàÇÏ°Ôµµ, FortranÀº ´Ù¾çÇÑ polymorphic ±â´ÉÀÇ ¾ð¾î°¡ ¾Æ´Ï¶ó ÀϹÝÀûÀ¸·Î´Â »ç¿ëÀÚ°¡ Á¶½ÉÇÏ¿© º¯¼ö¿Í ÇÔ¼öÀÇ ÇüŸ¦ ¸ÂÃß¾î¾ß ÇÑ´Ù!

ÀÌÁ¦ »ç¿ëÀÚ°¡ ¸¸µç ÇÔ¼ö¸¦ »ìÆ캸ÀÚ. ´ÙÀ½ ¹®Á¦¸¦ Ç®¾îº¸ÀÚ: ÁöÁúÇÐÀÚ°¡ Bay AreaÀÇ °­¼ö·®À» Á¶»çÇÏ¿© °ü°è ¼ö½Ä r(m,t) ¸¦ ±¸ÇÏ¿´´Ù. ÀÌ ½Ä¿¡¼­ r Àº ºñÀÇ ¾ç, m Àº ´ÞÀÌ°í, t ´Â À§Ä¡¿¡ µû¶ó ´Ù¸¥ scalar parameterÀÌ´Ù. r¿¡ °üÇÑ ½Ä°ú t °ªÀ» »ç¿ëÇÏ¿© ¿¬°£ °­¼ö·®À» °è»êÇÑ´Ù.

ÀÌ ¹®Á¦¸¦ Ǫ´Â ºÐ¸íÇÑ ¹æ¹ýÀº ¸ðµç ´Þ¿¡ ´ëÇÏ¿© °è»êÇÏ´Â loop¸¦ ¸¸µé¾î ´õÇÔÀ¸·Î½á rÀÇ °ªÀ» ±¸ÇÏ´Â °ÍÀÌ´Ù. rÀÇ °ªÀ» ±¸ÇÏ´Â °ÍÀº µ¶¸³ÀûÀÎ ÀÛÀº ¹®Á¦À̹ǷΠÀ̸¦ ÇÔ¼ö·Î ÇÏ´Â °ÍÀÌ Æí¸®ÇÏ´Ù. ´ÙÀ½ ÁÖ ÇÁ·Î±×·¥ (main program)À» »ç¿ëÇÒ ¼ö ÀÖ´Ù:

      program rain
      real r, t, sum
      integer m
 
      read (*,*) t
      sum = 0.0
      do 10 m = 1, 12
         sum = sum + r(m, t)
  10  continue
      write (*,*) 'Annual rainfall is ', sum, 'inches'

      stop
      end
µ¡ºÙ¿© ÇÔ¼ö rÀº ÇÔ¼ö·Î Á¤ÀÇÇÏ¿©¾ß ÇÑ´Ù. ÁöÁúÇÐÀÚ°¡ ¾òÀº °ø½ÄÀº ´ÙÀ½°ú °°´Ù.
      r(m,t) = t/10 * (m**2 + 14*m + 46) if this is positive
      r(m,t) = 0                         otherwise        
À̸¦ Fortran function·Î ³ªÅ¸³»¸é ´ÙÀ½°ú °°´Ù.
      real function r(m,t)
      integer m
      real t

      r = 0.1*t * (m**2 + 14*m + 46)
      if (r .LT. 0) r = 0.0

      return
      end
ÇÔ¼öÀÇ ±¸Á¶°¡ ÁÖ ÇÁ·Î±×·¥°ú ´à¾Ò´Ù´Â °ÍÀ» ¾Ë ¼ö ÀÖ´Ù. ±× Â÷ÀÌ´Â:

¿ä¾àÇϸé Fortran 77 ÇÔ¼öÀÇ ÀϹÝÀû syntax´Â ´ÙÀ½°ú °°´Ù.

      type function name (list-of-variables)
      declarations
      statements
      return
      end

ÇÔ¼ö´Â ºÎ¸£´Â ÇÁ·Î±×·¥¿¡¼­ Á¤È®ÇÑ ÇüÅ·Π¼±¾ðµÇ¾î¾ß ÇÑ´Ù. ±×¸®°í ±× ÇÔ¼ö À̸§°ú °ýÈ£ ¾È¿¡ ¸Å°³ º¯¼ö¸¦ ³ª¿­ÇÏ¿© ºÎ¸¥´Ù.

¼­ºê·çƾ (Subroutines)

FortranÀÇ functionÀº ÀϹÝÀûÀÎ ÇÔ¼ö¿Í ¸¶Âù°¡Áö·Î ¿ÀÁ÷ ÇÑ°¡Áö °á°ú¸¸À» °®°í µ¹¾Æ¿À´Â °ÍÀÌ ¿øÄ¢ÀÌ´Ù. ¶§¶§·Î´Â µÎ °³³ª ±× ÀÌ»óÀÇ º¯¼ö (¶§¶§·Î´Â °á°ú º¯¼ö ¾øÀÌ)°ªÀ» °®°í µ¹¾Æ ¿À°ÔÇÏ°í ½ÍÀ» ¶§°¡ ÀÖ´Ù. ÀÌ·± °æ¿ì¿¡´Â ¼­ºê·çƾ subroutine ±¸Á¶¸¦ »ç¿ëÇÏ¿©¾ß ÇÑ´Ù. syntax´Â ´ÙÀ½°ú °°´Ù.
      subroutine name (list-of-arguments)
      declarations
      statements
      return
      end
¼­ºê·çƾÀº typeÀÌ ¾ø°í µû¶ó¼­ ÀÌ ¼­ºê·çƾÀ» ºÎ¸£´Â ÇÁ·Î±×·¥¿¡¼­ Á¾·ù type ¸¦ ¼±¾ðÇØÁÙ ÇÊ¿ä°¡ ¾ø´Ù.

´ÙÀ½Àº ¾ÆÁÖ °£´ÜÇÑ ¼­ºê·çƾÀÇ ¿¹ÀÌ´Ù. ÀÌ ¼­ºê·çƾ¿¡¼­´Â µÎ Á¤¼ö º¯¼öÀÇ °ªÀ» ¼­·Î ¹Ù²Ù´Â °ÍÀÌ´Ù.

      subroutine iswap (a, b)
      integer a, b
c Local variables
      integer tmp

      tmp = a
      a = b
      b = tmp

      return
      end
¿©±â¿¡´Â º¯¼ö ¼±¾ðÀÌ µÎ blockÀ¸·Î µÇ¾îÀÖÀ½¿¡ ÁÖÀÇÇÑ´Ù. ¿ì¼± ÀÔÃâ·Â ¸Å°³ º¯¼ö, Áï ºÎ¸£´Â ÇÁ·Î±×·¥ caller °ú ºÒ¸®¾îÁö´Â ÇÁ·Î±×·¥ callee ¾çÂÊ¿¡ °øÅëÀÎ º¯¼ö¸¦ ¼±¾ðÇÑ´Ù. ±×¸®°í ³ª¼­ ±¹ºÎ º¯¼ö local variables, Áï ÀÌ ºÎÇÁ·Î±×·¥ ¾È¿¡¼­¸¸ »ç¿ëÇÏ´Â º¯¼ö¸¦ ¼±¾ðÇÑ´Ù. ¼­·Î ´Ù¸¥ ºÎ ÇÁ·Î±×·¥¿¡¼­ °°Àº º¯¼ö À̸§À» »ç¿ëÇÒ ¼ö ÀÖ°í compiler´Â À̸¦ À̸§ÀÌ °°Áö¸¸ ´Ù¸¥ º¯¼ö·Î Ãë±ÞÇÑ´Ù.

Call-by-reference

Fortran 77Àº ¼ÒÀ§ call-by-reference ¹æ½ÄÀ» ÃëÇÑ´Ù. ÀÌ´Â ÇÔ¼ö/¼­ºê·çƾ ¸Å°³ º¯¼öÀÇ °ª¸¸À» ³Ñ°ÜÁÖ´Â (call-by-value) ´ë½Å º¯¼öÀÇ memory ÁÖ¼Ò (pointer)¸¦ ³Ñ°ÜÁÖ´Â °ÍÀ» ÀǹÌÇÑ´Ù. ´ÙÀ½ °£´ÜÇÑ ¿¹°¡ ±× Â÷À̸¦ ¼³¸íÇÑ´Ù.
      program callex
      integer m, n
c
      m = 1
      n = 2 

      call iswap(m, n)
      write(*,*) m, n

      stop
      end
ÀÌ ÇÁ·Î±×·¥ÀÇ °á°ú´Â ¿¹»óÇÒ ¼ö Àִ´ë·Î "2 1"ÀÌ´Ù. ±×·¯³ª Fortran 77¿¡¼­ call-by-value¸¦ »ç¿ëÇÑ´Ù¸é °á°ú´Â "1 2"·Î º¯¼ö m°ú nÀÌ ¼­·Î ¹Ù²îÁö ¾Ê´Â´Ù. ÀÌ´Â m°ú nÀÇ °ª¸¸ÀÌ ¼­ºê·çƾ iswap¿¡ Àü´ÞµÈ´Ù¸é m°ú nÀÌ ¼­ºê·çƾ ³»ºÎ¿¡¼­ ¼­·Î ºñ²ï´ÙÇÏ´õ¶óµµ »õ °ªÀº ÁÖ ÇÁ·Î±×·¥¿¡ Àü´ÞµÇÁö ¾ÊÀ» °ÍÀ̱⠶§¹®ÀÌ´Ù.

À§ ¿¹Á¦¿¡¼­ call-by-reference°¡ Á¤È®ÇÏ°Ô ¿ì¸®°¡ ¿øÇÏ´Â ¹ÙÀÌ´Ù. ÇÏÁö¸¸ ÀÌ ¶§¹®¿¡ Fortran code¸¦ ¾µ ¶§ ¿øÇÏÁö ¾Ê´ø ºÎÀÛ¿ë side effectsÀÌ ÀϾÁö ¾Êµµ·Ï ÁÖÀÇÇÏ¿©¾ß ÇÑ´Ù. ÇÑ ¿¹·Î, For example, sometimes it is tempting to use an input parameter ºÎ ÇÁ·Î±×·¥ ¾È¿¡¼­ ÀÔ·Â ¸Å°³ º¯¼ö¸¦ ±¹ºÎ º¯¼ö·Î »ç¿ëÇϸé ÆíÇÒ ¶§°¡ Àմµ¥ ÀÌ °æ¿ì ±× °ªÀÌ ¹Ù²î¾î ¹ö¸°´Ù. ±× °á°ú ±â´ëÇÏÁö ¾Ê´ø °ªÀÌ ÀÌ ºÎÇÁ·Î±×·¥À» ºÎ¸¥ ÇÁ·Î±×·¥À¸·Î ÆÛÁ®³ª°¡°Ô µÇ±â¿¡ °áÄÚ »ç¿ëÇÏ¿©¼­´Â ¾È µÈ´Ù.

µÞ Àå¿¡¼­ ¹è¿­(array)À» ¸Å°³ º¯¼ö·Î ³Ñ°Ü ÁÖ´Â ¹®Á¦¿¡¼­ ´Ù½Ã »ìÆì º¼ °ÍÀÌ´Ù.


Exercises

Exercise A
Á¤¼ö nÀ» ¹Þ¾Æµé¿©¼­ n! (n factorial)À» µ¹·Á º¸³»´Â function facÀ» ÀÛ¼ºÇÑ´Ù. ÀÛ¼ºÇÑ ÇÁ·Î±×·¥À» ´ÙÀ½ ÁÖÇÁ·Î±×·¥À» »ç¿ëÇÏ¿© °Ë»çÇ϶ó.
      program tstfac
c
c Exercise A, section 11.
c Main program to test factorial function.
c
      integer n, fac

  10  continue
         write(*,*) 'Give n: '
         read (*,*) n
         if (n.gt.0) then
            write(*,*) n, ' factorial is', fac(n)
            goto 10
         endif
c     End of loop

      stop
      end
n = 5, 10, 20ÀÏ ¶§ÀÇ °á°ú¸¦ »ìÆ캻´Ù. ¶Ç ÀÌ ÇÁ·Î±×·¥À» Á¤Áö½ÃÅ°´Â ¹æ¹ýÀ» ¾Ë¾Æº»´Ù. (Hint: You have to use a loop in your function since Fortran 77¿¡¼­´Â ÀÚ½ÅÀÌ ÀÚ½ÅÀ» ºÎ¸£´Â Àç±Í recursionÀÌ ºÒ°¡´ÉÇϹǷΠfucntion¿¡¼­ loop¸¦ »ç¿ëÇÏ¿©¾ß¸¸ ÇÑ´Ù.)

Exercise B
¼¼ ½Ç¼ö a, b, c¸¦ ÀÔ·ÂÀ¸·Î ÇÏ¿© ÀÌÂ÷¹æÁ¤½Ä ax**2 + bx + c = 0ÀÇ µÎ ½Ç±ÙÀ» ±¸ÇÏ´Â ¼­ºê·çƾ qsolve¸¦ ÀÛ¼ºÇ϶ó. ±ÙÀÌ Çã¼öÀÌ¸é ´ÙÀ½°ú °°Àº ¿À·ù Á¤º¸ error message¸¦ Ãâ·ÂÇÏ°Ô ÇÑ´Ù.
      write(*,*) 'Warning: Complex roots!'
óÀ½¿¡´Â
      a=2.0, b=9.0, c=4.0
À¸·Î ´ÙÀ½¿¡´Â
      a=2.0, b=0.0, c=4.0
ÀÇ ¼ýÀÚ·Î À§¿¡¼­ ÀÛ¼ºÇÑ ¼­ºê·çƾÀ» °Ë»çÇÒ ¼ö ÀÖ´Â ÁÖ ÇÁ·Î±×·¥À» ÀÛ¼ºÇ϶ó.


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