Next: Argument Intent Up: More on Procedures Previous: Recall F90 Program Units   Contents

# Side Effects

A notorious characteristic of bad software design are the side effects. Side effects may happen when a function or subroutine modifies the values of their input arguments, the result being a hard-to-control program logic.

For example, suppose we want a function norm2w (w stands for wrong!) which computes the 2-norm of the vector, then projects the vector on the (y,z) plane by setting its x-coordinate to zero.

At some point in the program we have 2 vectors which have the same (x,y) coordinates, but a different z coordinate, and . To fix the ideas, suppose , , , and . Our intent is to compute the norm of the first vector ( ) plus the norm of the (y,z) projection of the second vector ( ). The correct result is .

We write the following line of code:

y = norm2w(a,b,c) + norm2w(a,b,d)

When the first call is executed, is computed, then is set to 0; at the second call, is computed, and is set again to zero; overall, we get the correct result.

Now, the trouble is that we do not know that norm2w(a,b,c) is evaluated first, and norm2w(a,b,d) second. Except for operator precedence, F95 standard says nothing about the order of argument evaluation. The compiler may choose to evaluate the right operand first (i.e. norm2w(a,b,d)), put the result in temporary storage, evaluate the left operand next (i.e., norm2w(a,b,c)) and then perform the addition. Now the result is , clearly different than what we had in mind. Next: Argument Intent Up: More on Procedures Previous: Recall F90 Program Units   Contents
{"ft_lang_label":"__label__en","ft_lang_prob":0.91278756,"math_prob":0.96265316,"size":1195,"snap":"2023-14-2023-23","text_gpt3_token_len":299,"char_repetition_ratio":0.13602015,"word_repetition_ratio":0.0,"special_character_ratio":0.2585774,"punctuation_ratio":0.18996416,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98417306,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-02T18:56:48Z\",\"WARC-Record-ID\":\"<urn:uuid:31131810-54e6-4631-8e85-40e1bae7240c>\",\"Content-Length\":\"5397\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d046329c-0c36-433b-a9e4-fc703fc60c16>\",\"WARC-Concurrent-To\":\"<urn:uuid:5c18d5e1-c08f-4b81-ac49-8a31d80cc408>\",\"WARC-IP-Address\":\"198.82.184.52\",\"WARC-Target-URI\":\"https://people.cs.vt.edu/~asandu/Courses/MTU/CS2911/fortran_notes/node173.html\",\"WARC-Payload-Digest\":\"sha1:ZB7WODXES2AXK3O3ABSKSKIVLV6C6NK7\",\"WARC-Block-Digest\":\"sha1:FMP6LFCTPANZYFVBDN4OPXJ7VSX6Z3LQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224648850.88_warc_CC-MAIN-20230602172755-20230602202755-00492.warc.gz\"}"}