User Tools

Site Tools


emulating_20the_20c_20assignment_20function

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

emulating_20the_20c_20assignment_20function [2018/03/31 13:19]
127.0.0.1 external edit
emulating_20the_20c_20assignment_20function [2018/04/17 16:02] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //by Richard Russell, July 2009//\\ \\  In the **C** programming language, an //​assignment//​ operation such as:\\ \\  //by Richard Russell, July 2009//\\ \\  In the **C** programming language, an //​assignment//​ operation such as:\\ \\ 
 +<code c>
         variable = expression         variable = expression
 +</​code>​
 can be used either as a //​statement//​ or as a //​function//​. When used as a function, it returns the new value of the variable. Whilst at first sight this might not seem very useful, it is particularly convenient in the case of a **while** loop, for example:\\ \\  can be used either as a //​statement//​ or as a //​function//​. When used as a function, it returns the new value of the variable. Whilst at first sight this might not seem very useful, it is particularly convenient in the case of a **while** loop, for example:\\ \\ 
 +<code c>
         while (variable = expression)         while (variable = expression)
           {           {
             // Do something useful here             // Do something useful here
           }           }
 +</​code>​
 Here **variable** is set equal to the value of **expression** and if its new value is //​non-zero//​ the body of the loop is executed (note that it is //not// testing whether **variable** is equal to **expression**;​ in **C** you do that using the == operator).\\ \\  Since in BBC BASIC an assignment is a //​statement//,​ you can't straightforwardly do this, and you have to code it as follows:\\ \\  Here **variable** is set equal to the value of **expression** and if its new value is //​non-zero//​ the body of the loop is executed (note that it is //not// testing whether **variable** is equal to **expression**;​ in **C** you do that using the == operator).\\ \\  Since in BBC BASIC an assignment is a //​statement//,​ you can't straightforwardly do this, and you have to code it as follows:\\ \\ 
 +<code bb4w>
         variable = expression         variable = expression
         WHILE variable         WHILE variable
Line 14: Line 19:
           variable = expression           variable = expression
         ENDWHILE         ENDWHILE
 +</​code>​
 As you can see, this involves writing the assignment statement **twice**, once outside the loop and again inside the loop. This is inelegant and potentially error-prone,​ for example you might make a change to one of the assignments but forget to change the other.\\ \\  To emulate the **C** behaviour you can utilise this simple function:\\ \\  As you can see, this involves writing the assignment statement **twice**, once outside the loop and again inside the loop. This is inelegant and potentially error-prone,​ for example you might make a change to one of the assignments but forget to change the other.\\ \\  To emulate the **C** behaviour you can utilise this simple function:\\ \\ 
 +<code bb4w>
         DEF FNassign(RETURN variable, expression)         DEF FNassign(RETURN variable, expression)
         variable = expression         variable = expression
         = variable         = variable
 +</​code>​
 Now you can write the loop as follows:\\ \\  Now you can write the loop as follows:\\ \\ 
 +<code bb4w>
         WHILE FNassign(variable,​ expression)         WHILE FNassign(variable,​ expression)
           REM Do something useful here           REM Do something useful here
         ENDWHILE         ENDWHILE
 +</​code>​
 Note that since **variant** numeric variables are used in the function (i.e. without a '​type'​ suffix character) it will work equally well with **integer** variables and values as with **floating-point** variables. Note that since **variant** numeric variables are used in the function (i.e. without a '​type'​ suffix character) it will work equally well with **integer** variables and values as with **floating-point** variables.
emulating_20the_20c_20assignment_20function.txt ยท Last modified: 2018/04/17 16:02 by tbest3112