Do we still need routines?

In Fortran, there is a clear difference between a function and a routine: functions return a single value, routines return a value. This introduces a cascade of differences between the two. One example is calling semantics: you can call a function in the same way as in other languages, but to call a subroutine you must first issue a call statement.

With the addition of pointers and data types in Fortran95, it seems that there are no technical limitations when creating any subroutine function and storing subroutines for heritage only. Functions can return zero (you just return an integer false), one or more values โ€‹โ€‹(for example, you can return a pointer to a selected instance of a type, for example, C ++ STL Pair).

Am I really wrong? Should we still need Fortran programming routines because of some function that lacks routines and functions?

+4
source share
5 answers

If you search the comp.lang.fortran archives, you will find discussions about the semantics of functions. IIRC clarifies that the standard does not clearly state what is and what is not allowed for functions that have side effects.

For example, can the compiler optimize

x = foo (args) + foo (args)

in

x = 2 * foo (args)

Or for another example, consider

x = y + foo (y)

What if foo () changes the value of y? Remember that Fortran does not have the concept of C sequence points.

In general, the recommendation of several experts is to use functions only if they are clean, otherwise subroutines are used. And, this is the advice that I follow myself too.

+17
source

I don't think routines go anywhere. Most other languages โ€‹โ€‹accept methods that execute and return no value. I see no reason why this is bad. No one has to change to change anything.

Only Legacy says that routines will remain as long as Fortran does. And while Fortran is around, there will be nothing wrong with writing a method that performs an action and returns nothing.

UPDATE:

Why are you saying hassle? What a big deal? I do not agree that subprograms are "troubles" when they are used in an appropriate situation.

Fortran has kept the distinction between functions and routines since version 77 and possibly earlier. Other C-family languages, too. Why is this so unexpected? Even languages โ€‹โ€‹that have pointers and objects for a long time have methods that return void.

+5
source

You are trying to program C again in fortran, aren't you ?; -)

In my opinion, yes, we do. For, if only for one reason - they are easier to understand, understand, and they are more widely used than functions.

Also, -1, as I believe this is not a constructive question. If they do not like you, do not use them.

+2
source

If I understand correctly, Stefano is not against the idea of โ€‹โ€‹routines. Opinion against them is stupid. He is against using different styles for routines / functions.

Fortran is a required programming language. More precisely, it is a procedural programming language (or rather, a structured programming language).

In imperative programming, we have state and affirmations to change it. In procedural programming, our tools for making changes are procedures (we localize changes within procedures). The procedure may or may not return any value. And I do not think that this fact (either the return value of the procedure or not) is such an important reason that there are 2 different objects in the programming language. We can only have functions (for example, in C) and just return something special when we really do not need to return something (void). Or we can only have procedures and special syntax that allows us to return such values โ€‹โ€‹as in Modula-2, Oberon, ...

A language may need to have only one style for declaring procedures. I agree with you, Stefano.

0
source

The fact that I have to answer this question for myself is crazy, but that's how.

The difference is that you cannot "call functions like in other languages" in Fortran. In C, you can call an integer function without assigning a value, e.g.

 int foo() { return 5; } int main() { foo(); // this works } 

In Fortran, you always need to bind a host variable. Example

 module test implicit none contains integer function foo() print *, "hello" foo = 0 end function end module program hello use test integer :: x x = foo() ! this works foo() ! this does not compile end program hello 

The meaning of this is that to "emulate" the void function, returning an integer of fictitious ones will still not allow you to call without a receiver variable.

In Fortran, the void return type does not exist. Technically, you can structure your program with all the functions, replace all occurrences of the call operator with x = , as shown above, but this will not make your syntax look like C or other languages โ€‹โ€‹in any case where there is no difference between void - return functions and non-empty return functions. routines are the only objects that allow you to "return void", but the semantics for making a call are just different. In addition, there is no difference between them.

-3
source

All Articles