As a preliminary activity within the context of the Dual Year of UK and Mexico 2015, I visited the Centro Universitario de Ciencias Exactas e IngenierĂ­as of the University of Guadalajara, Mexico, last April.

I taught an Introductory Tutorial of OCaml (12 hrs). In addition to OCaml particularities, I discussed with the atendees the differences between functional and imperative languages.

The following code is an extract from a slide that I used to explain side effects caused by global variables used in imperative languages like C. You can find the source of inspiration in page 76 of this old but excellent text book on concurrency and operating systems: Concurrent Euclid, The Unix System and Tunis, R.C. Holt, Addison-Wesley Publishing Company, 1983.

        Example of a Side Effect Caused by Global Variables
#include <stdio.h>

int i=1; /* a global var */

int f(int x){ 
   i=2*i;       /* f changes the val of i from  i=1 to i=2 */
   return i*x;  /* f returns 20 when x= 10                 */
} 

main(){
int  j= i + f(10);  
printf("j= %d",j); /* what is the value of j? */
}

/*
  j= 21 if computed as j= i + f(10)= 1 + 20= 21 where the original val of i=1 is used.

 The compiler might optimize computation as j= f(10) + i, then
 j= 22 computed as j= f(10) + i= 20 + 2= 22 where the new val of i=2 from f is used.
 
The result is ambiguous, different compilers produce different results!
*/

Different C compilers will peoduce different results, for instance, the online GNU GCC v4.8.3 compiler outputs j=22.