Use Fortran in a notebook!

Each cell is compiled and run separately currently, so they have to be standalone programs.

The following is fine:


In [7]:
program hello
    implicit none
    print*, "Hello, World!"
end program


 Hello, World!

The following, not so much:


In [8]:
subroutine foo
    print*, "Hello, World"
end subroutine


/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../lib64/crt1.o: In function `_start':
/home/abuild/rpmbuild/BUILD/glibc-2.19/csu/../sysdeps/x86_64/start.S:118: undefined reference to `main'
collect2: error: ld returned 1 exit status
[Fortran kernel] gfortran exited with code 1, the executable will not be executed

You can use functions:


In [25]:
program maths
    implicit none
    integer :: x
    
    x = 7
    
    write(*, '(a,i4)') "x   =", x
    write(*, '(a,i4)') "x^2 =", square(x)
    
contains

    integer function square(number)
        integer, intent(in) :: number
        square = number * number
    end function
    
end program


x   =   7
x^2 =  49

You can even use modules! They just have to be in the same cell as the program...


In [26]:
module funcs

    implicit none
    
contains

    integer function square(number)
        integer, intent(in) :: number
        square = number * number
    end function
    
end module funcs

program maths
    
    use funcs

    implicit none
    integer :: x
    
    x = 7
    
    write(*, '(a,i4)') "x   =", x
    write(*, '(a,i4)') "x^2 =", square(x)
    
end program


x   =   7
x^2 =  49

In [ ]: