W1206 Expression Evaluation vs Printing

From Coder Merlin
(Redirected from W1206 Return Value)
Jump to navigation Jump to search
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.

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

  • 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]

KeyConceptsIcon.png
Key Concepts
  • 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
  • Expressions, when evaluated, yield a specific value
  • The evaluation of an expression is very different from printing
  • When using an environment such as a REPL, the results of expressions are automatically displayed to the console, however, in a real program this does not occur
  • 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
    • The numbered portion of this identifier is an integer which increases with each expression which is displayed

Exercises[edit]

References[edit]


CoderMerlin™ proudly recommends:
HintIcon.png

Your purchase via these links helps keep  Coder Merlin™ 's orbuculum ablaze. As an Amazon Associate we earn from qualifying purchases. Thank you for your support!


Designed with pride in Silicon Valley, CA, USA