Add more to Hello World page
This commit is contained in:
parent
016af83350
commit
39bdc8b891
@ -20,7 +20,7 @@
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<footer class="flex justify-center pt-10 mt-10 ">
|
||||
Author: Rasmus B. W. Rasmussen
|
||||
<footer class="flex justify-center pt-10 mt-10 inset-x-0 bottom-0">
|
||||
Author: Still under construction
|
||||
</footer>
|
||||
</template>
|
@ -8,25 +8,91 @@
|
||||
<hr>
|
||||
<p></p>
|
||||
|
||||
<div class="px-3 py-2">
|
||||
<pre class="language-fortran"><code class="text-sm">
|
||||
<div>
|
||||
<pre><code class="text-sm">
|
||||
program hello
|
||||
implicit none
|
||||
write(*,*) 'Hello world!'
|
||||
print *, 'Hello world!'
|
||||
end program hello
|
||||
</code></pre>
|
||||
</div>
|
||||
|
||||
<p>With modern compilers, running the newest Fortran standards, <span><code>write(*,*)</code> and <code>print</code></span>
|
||||
will compile to the exact same assembly. So if you only need to print something to stdout, just use <span><code>print</code></span>
|
||||
<p>In the good ol' days (before FORTRAN 77), when printing to console, you would write <span><code>write(*,*)</code></span>,
|
||||
but with the release of FORTRAN 77, that became redundant, as you could use the newest keyword, <span><code>print</code></span>.</p>
|
||||
<br>
|
||||
|
||||
<p>With modern compilers, running the newest Fortran standards, <span><code>write(*,*) 'Hello world!'</code> and <code>print *, 'Hello world!'</code></span>
|
||||
will compile to the exact same assembly. So if you only need to print something to stdout, just use <span><code>print *</code></span>
|
||||
as it conveys the meaning of the code better. Plus we don't need the full functionality of <span><code>write(*,*)</code></span>
|
||||
in this example.</p>
|
||||
<br>
|
||||
|
||||
<div>
|
||||
<pre class="language-fortran"><code class="text-sm">
|
||||
<pre><code class="text-sm">
|
||||
$ gfortran -o hello main.f90
|
||||
$ ./hello
|
||||
> Hello world!
|
||||
Hello world!
|
||||
</code></pre>
|
||||
</div>
|
||||
|
||||
<p>Now, hear me out, why not spice up the program a little bit more? Maybe we want to print to stdout, or what if
|
||||
we suddenly need to print an error message, and want that to be handled correctly by the terminal emulator?</p>
|
||||
<br>
|
||||
<p>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.</p>
|
||||
<br>
|
||||
|
||||
<div>
|
||||
<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>
|
||||
|
||||
<p>So what we're seeing here, is that we have created two variables: <span><code>usertxt</code> and <code>ios</code></span>.
|
||||
<span><code>usertxt</code></span> 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 <span><code>character</code></span>
|
||||
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.</p>
|
||||
<br>
|
||||
|
||||
<p>The other variable, <span><code>ios</code></span>, 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.</p>
|
||||
<br>
|
||||
|
||||
<p>Introducing, <span><code>iostat</code></span>. 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.</p>
|
||||
<br>
|
||||
|
||||
<p>So in this case, where we check if <span><code>ios</code></span> 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 <span><code>trim()</code></span> function to remove any trailing whitespaces. That's because, if you
|
||||
make a character bigger than the actual text, it will be fille with whitespace after the text. So now the output
|
||||
looks like this:</p>
|
||||
<br>
|
||||
|
||||
<div>
|
||||
<pre><code class="text-sm">
|
||||
$ gfortran -o hello main.f90
|
||||
$ ./hello
|
||||
Input text here:
|
||||
Hello world :D
|
||||
Hello world :D
|
||||
</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -6,17 +6,15 @@
|
||||
<div>
|
||||
<h1 class="text-3xl">Fortran</h1>
|
||||
<hr>
|
||||
<p>Fortran, which stands for Formula Translation, is indeed an interesting language with a rich history. As one of
|
||||
the oldest programming languages still in active use today, it has played a significant role in shaping the
|
||||
development of modern computing. First introduced in the 1950s by a team at IBM led by John Backus, Fortran was
|
||||
designed specifically for scientific and engineering applications, where its ability to efficiently handle
|
||||
complex numerical computations proved invaluable.</p>
|
||||
<p>Fortran (Formula Translation), is one of the ancient languages somehow still in use as of today. First introduced
|
||||
in the 1950s by a team at IBM, Fortran was designed specifically for scientific and engineering applications,
|
||||
where its ability to efficiently handle complex mathematical operations.</p>
|
||||
<br>
|
||||
<p>
|
||||
Over the years, Fortran has undergone numerous updates and revisions, with the most recent standard being Fortran
|
||||
2018. Despite the rise of newer languages like C++, Java, and Python, Fortran remains a popular choice in many
|
||||
fields, including physics, chemistry, meteorology, and engineering, due to its performance. Even Nvidia and Intel
|
||||
are still developing their compilers, as well as FOSS compilers such as gfortran and lfortran.</p>
|
||||
2023. Even with the popularity of newer languages, such as C++, Java, and Python, Fortran remains a popular choice
|
||||
in many fields, including physics, chemistry, meteorology, and engineering, due to its performance. Even Nvidia
|
||||
and Intel are still developing their compilers, as well as FOSS compilers such as gfortran and lfortran.</p>
|
||||
<br>
|
||||
|
||||
<p>Table of contents:</p>
|
||||
|
Loading…
Reference in New Issue
Block a user