program main implicit none real :: n print*, 'Give N' read*, n print*, square_a(n) print*, square_b(n) print*, square_c(n) print*, square_d(n) contains ! --- First way real function square_a(x) result(sol) implicit none real :: x sol=x**2 end function square_a ! --- Second way real function square_b(x) implicit none real, intent(in) :: x ! The 'intent' is optional (providing additional checking & ! optimization) but IS necessary for elemental procedures square_b=x**2 end function square_b ! --- Third way function square_c(x) real, intent(in) :: x real :: square_c square_c=x**2 end function square_c ! --- Fourth way pure function square_d(x) ! 'pure' here means that the function is free from side ! effects and the compiler can optimize it in ways that ! it otherwise cannot real, intent(in) :: x real :: square_d square_d=x**2 end function square_d end program main