Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

return required on function?

larson__michael
Beginner
313 Views

NEVERMIND.... I figured it out...

 

 

 

Hi I am working on re-learning modern Fortran by working through the Project Euler examples.

In writing a fibonacci function a "return" is required to actually get the result pushed back, I seem to remember that was required in F77..

I would suspect that

     f = fibonacci(x)                                                                                                                                                                                         

Would be enough but through trial and error I found that issuing a "return" at the end of the fibonacci function makes it all work correctly. But this is contrary to just about any fortran example I have seen posted on the web

Thanks ahead of time

 

 

! PROBLEM 2                                                                                                                                                                                                   

integer function fibonacci(x)                                                                                                                                                                                 

  implicit none                                                                                                                                                                                               

  integer :: x                                                                                                                                                                                                

  integer :: i                                                                                                                                                                                                

  integer :: x_m_1, x_m_2                                                                                                                                                                                     

                                                                                                                                                                                                              

  if (x == 0) then                                                                                                                                                                                            

     fibonacci = 1                                                                                                                                                                                            

  elseif (x == 1) then                                                                                                                                                                                        

     fibonacci = 2                                                                                                                                                                                            

  else                                                                                                                                                                                                        

     x_m_1 = 2                                                                                                                                                                                                

     x_m_2 = 1                                                                                                                                                                                                

     fibonacci = 0                                                                                                                                                                                            

     do i = 2,x                                                                                                                                                                                               

        fibonacci = x_m_1 + x_m_2                                                                                                                                                                             

        x_m_2 = x_m_1                                                                                                                                                                                         

        x_m_1 = fibonacci                                                                                                                                                                                     

        if (fibonacci < 0) then                                                                                                                                                                               

           stop                                                                                                                                                                                               

        end if                                                                                                                                                                                                

     end do                                                                                                                                                                                                   

  end if                                                                                                                                                                                                                                                                                                                                                                                                                    

  return                                                                                                                                                                                                      

end function fibonacci                                                                                                                                                                                        

                                                                                                                                                                                                              

subroutine problem_2                                                                                                                                                                                          

  implicit none                                                                                                                                                                                               

  integer :: sum = 0                                                                                                                                                                                          

  integer :: even_sum = 0                                                                                                                                                                                     

  integer :: x = 0                                                                                                                                                                                            

  integer :: f                                                                                                                                                                                                

  integer :: fibonacci                                                                                                                                                                                        

                                                                                                                                                                                                              

  do while (f <= 4000000)                                                                                                                                                                                     

     f = fibonacci(x)                                                                                                                                                                                         

     !print *, "Fibonacci ", x, " = ", f                                                                                                                                                                      

                                                                                                                                                                                                              

     sum = sum + f                                                                                                                                                                                            

     !print *, "sum = ", sum                                                                                                                                                                                  

                                                                                                                                                                                                              

     if (mod(f, 2) == 0) then                                                                                                                                                                                 

        even_sum = even_sum + f                                                                                                                                                                               

     end if                                                                                                                                                                                                   

     !print *, "even_sum = ", even_sum                                                                                                                                                                        

                                                                                                                                                                                                              

     x = x + 1                                                                                                                                                                                                

  end do                                                                                                                                                                                                      

                                                                                                                                                                                                              

  print *, "Sum is ", sum                                                                                                                                                                                     

  print *, "Even Sum is ", even_sum                                                                                                                                                                           

end subroutine problem_2                                                                                                                                                                                      

                                            

0 Kudos
2 Replies
GVautier
New Contributor II
313 Views

F is not initialized before the do while loop so it may never enter

0 Kudos
larson__michael
Beginner
313 Views

good point... woops.

0 Kudos
Reply