Ancient-Programming-Site/src/components/Fortran/Fortran_Hello_World.vue
2025-02-25 13:10:24 +01:00

94 lines
4.0 KiB
Vue

<script setup lang="ts">
import Paragraph from "../tags/Paragraph.vue";
import CodeTag from "../tags/CodeTag.vue";
import PageStart from "../tags/PageStart.vue";
</script>
<template>
<page-start>Hello World In Fortran 90</page-start>
<paragraph>A Hello World program is always a great step into a new programming language. It gives a simple oversight into
the build process of said language. And with a simple project come a simple intro (mostly). But fret not, Fortran
can be simple or complex, depending on the size of the project.</paragraph>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<pre><code class="text-sm">
program hello
implicit none
print *, 'Hello world!'
end program hello
</code></pre>
</div>
<paragraph>In the good ol' days (before FORTRAN 77), when printing to console, you would write <code-tag>write(*,*)</code-tag>,
but with the release of FORTRAN 77, that became redundant, as you could use the newest keyword: <code-tag>print</code-tag>. <br><br>
With modern compilers, running the newest Fortran standards, <code-tag>write(*,*) 'Hello world!'</code-tag>
and <code-tag>print *, 'Hello world!'</code-tag>
will compile to the exact same assembly. So if you only need to print something to stdout, just use <code-tag>print *</code-tag>
as it conveys the meaning of the code better. Plus we don't need the full functionality of <code-tag>write(*,*)</code-tag>
in this example.</paragraph>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<pre><code class="text-sm">
$ gfortran -o hello main.f90
$ ./hello
Hello world!
</code></pre>
</div>
<paragraph>Now, hear me out, why not spice up the program a little bit more? What if we want to read user input? Well, fret
not my friend, because I have just the solution for you then. All we gotta do is to read from the terminal, and
then print out the value.</paragraph>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<pre><code class="text-sm">
program hello
implicit none
character(1000) :: usertxt
integer :: ios
print *, 'Input text here: '
read(*,"(A)", iostat=ios) usertxt
if(is_iostat_end(ios)) stop
print *, trim(usertxt)
end program hello
</code></pre>
</div>
<paragraph>So what we're seeing here, is that we have created two variables: <code-tag>usertxt</code-tag>
and <code-tag>ios</code-tag>.<code-tag>usertxt</code-tag> is the input we're reading from the terminal. Although since Fortran 90 doesn't
have an explicit string type, doesn't mean we can't read or write text, as the <code-tag>character</code-tag>
type is just an array of 1000 characters long. It doesn't have to be 1000 characters, but it's what I chose on a
whim.<br><br>
The other variable, <code-tag>ios</code-tag>, I probably don't need to introduce, as it's just an integer.
But what control-flow this value holds, I feel is quite important, as a lot of file-based business logic makes
use of it.<br><br>
Introducing, <code-tag>iostat</code-tag>. AKA, Input/Output Status Specifier. It indicates the status of the
I/O operation. If the integer is positive, it indicates an error code. If it's negative, it indicates an end of file
condition. If it's zero, then it does not indicate anything yet. No error, no End Of File, no particular condition
has triggered yet.<br><br>
So in this case, where we check if <code-tag>ios</code-tag> is End Of File, then that means if we input an
End Of File key combination (Ctrl+D on Unix/Linux, and Ctrl+Z on Windows), then the program would stop immediately.
We use the <code-tag>trim()</code-tag> function to remove any trailing whitespaces. That's because, if you
make a character array longer than the actual text, it will be filled with whitespace after the text. So now the output
looks like this:</paragraph>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<pre><code class="text-sm">
$ gfortran -o hello main.f90
$ ./hello
Input text here:
Hello world :D
Hello world :D
</code></pre>
</div>
</template>