Difference between revisions of "W1206 Expression Evaluation vs Printing"

From Coder Merlin
Line 43: Line 43:
Notice that a warning is emitted '''but the result of the evaluation of the expression IS NOT PRINTED''':
Notice that a warning is emitted '''but the result of the evaluation of the expression IS NOT PRINTED''':
{{ConsoleLines|{{Red|main.swift:4:1: warning:}} result of call to 'multiplyByTwo(number:)' is unused}}
{{ConsoleLines|{{Red|main.swift:4:1: warning:}} result of call to 'multiplyByTwo(number:)' is unused}}
Think about this carefully.  In the REPL, the results of expressions are ''displayed'' in cyan preceded by a special identifier beginning with '''$R'''.  This is a special variable which enables us to easily reference previous results.  This variable can be subsequently used in other expressions.  The numbered portion of this identifier is integer which ''increases'' with each expression which is displayed.
Think about this carefully.  In the REPL, the results of expressions are ''displayed'' in cyan preceded by a special identifier beginning with '''$R'''.  This is a special variable which enables us to easily reference previous results.  This variable can be subsequently used in other expressions.  The numbered portion of this identifier is an integer which ''increases'' with each expression which is displayed.


Let's return to the REPL.  In order to clearly distinguish between the ''printing'' of a string and the ''display'' of the result of an expression, pay close attention to the following:
Let's return to the REPL.  In order to clearly distinguish between the ''printing'' of a string and the ''display'' of the result of an expression, pay close attention to the following:

Revision as of 18:44, 17 November 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Epson MX-80
Nixdorf PC 8810-25

Prerequisites[edit]

Introduction[edit]

Printing refers to sending a stream of data, most often text but sometimes binary data as well, to an output device. Frequently this device is simply the console. The console refers to the input and output devices used to interact with a computer. It's usually comprised of a screen and keyboard, and sometimes a type of pointing device.

Printing is important not only for interacting with humans, but with other computing systems. For example, you've already had a lot of experience in interacting with the  Merlin Mission Manager  through print statements.

Expression Evaluation[edit]

As we learned earlier, there are many different types of expressions, including integer, floating point, and Boolean. Expressions, when evaluated, yield a specific value. But, the evaluation of an expression is very different from printing. This can sometimes be confusing, because when using an environment such as a REPL, the results of expressions are automatically displayed to the console. In a real program, however, this does not occur. Let's examine a few examples.

Printing vs Expression Evaluation Display[edit]

Let's first have a look at what happens inside the REPL.

jane-williams@codermerlin:~$ swift

Welcome to Swift.

Type :help for assistance.

  1> func multiplyByTwo(number:Int) -> Int {

  2. return number * 2

  3. }

  4> multiplyByTwo(number:3)

$R0: Int = 6

  5>

Now, let's copy exactly the same contents into a file named main.swift and then execute that file.

jane-williams@codermerlin:~$ emacs main.swift

func multiplyByTwo(number:Int) -> Int {
    return number * 2
}
multiplyByTwo(number:3)

Now, exit emacs and execute the program.

jane-williams@codermerlin:~$ swift main.swift

Notice that a warning is emitted but the result of the evaluation of the expression IS NOT PRINTED:

main.swift:4:1: warning: result of call to 'multiplyByTwo(number:)' is unused

Think about this carefully. In the REPL, the results of expressions are displayed in cyan preceded by a special identifier beginning with $R. This is a special variable which enables us to easily reference previous results. This variable can be subsequently used in other expressions. The numbered portion of this identifier is an integer which increases with each expression which is displayed.

Let's return to the REPL. In order to clearly distinguish between the printing of a string and the display of the result of an expression, pay close attention to the following:

jane-williams@codermerlin:~$ swift

Welcome to Swift.

Type :help for assistance.

  1> func multiplyByTwo(number:Int) -> Int {

  2. return number * 2

  3. }

  4> multiplyByTwo(number:3)

$R0: Int = 6

  5> print(multiplyByTwo(number:3))

6

Pay close attention to what happened in this case.

  • The evaluation of the expression was not displayed and there was no $R identifier.
  • The number 6 was printed because we used a print statement.
  • The color used for the printed output was different from that of the expressions.

Key Concepts[edit]

Exercises[edit]

References[edit]