Difference between revisions of "W1215 Function Signatures"

From Coder Merlin
Line 122: Line 122:
- 1.0
- 1.0
+ 1.0
+ 1.0
- Compile or runtime error
{
<syntaxhighlight lang="swift>
func doIt(a:Int, b:Int) -> Int {
    return a - b
} //
func doIt(a:Double, b:Double) -> Double {
    return b - a
} //
func doIt(a:Double, b:Double, c:Double) -> Double {
    return c * (b - a)
} //
let a : Double = 7
let b : Double = 8
let c : Double = 9
let d = doIt(a:a, b:b, c:c)
print(d)
</syntaxhighlight>
What is output? (Ignore whitespace)
|type="()"}
- -9.0
+ 9.0
- 9
- Compile or runtime error
{
<syntaxhighlight lang="swift>
func doIt(a:Int, b:Int) -> Int {
    return a - b
} //
func doIt(a:Double, b:Double) -> Double {
    return b - a
} //
func doIt(a:Double, b:Double, c:Double) -> Double {
    return c * (b - a)
} //
let a : Int = 7
let b : Int = 8
let c : Int = 9
let d = doIt(a:a, b:b, c:c)
print(d)
</syntaxhighlight>
What is output? (Ignore whitespace)
|type="()"}
- -9.0
- 9.0
- 9
+ Compile or runtime error
{
<syntaxhighlight lang="swift>
func doIt(a:Int, b:Int) -> Int {
    return a - b
} //
func doIt(a:Double, b:Double) -> Double {
    return b - a
} //
func doIt(p:Double, q:Double) -> Double {
    return p * q
} //
func doIt(a:Double, b:Double, c:Double) -> Double {
    return c * (b - a)
} //
let a : Double = 7
let b : Double = 8
let c : Double  = 9
let m : Double = 10
let n : Double = 11
let d = doIt(a:c, b:b)
let e = doIt(p:m, q:n)
print(d, e)
</syntaxhighlight>
What is output? (Ignore whitespace)
|type="()"}
- -1.0 72
- 1.0 110.0
+ -1.0 110.0
- Compile or runtime error
- Compile or runtime error


</quiz>
</quiz>

Revision as of 22:08, 26 February 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

DRAFT ICON


Function Signatures[edit]

Research[edit]

Function signatures consist of the function's name (identifier); parameter arity, types, names, and order; and the function's return type. (Note that various languages treat some of these items differently. For example, not all languages allow for a different return type if the signature is otherwise identical.)

Experiment[edit]

Create a directory within your "project" directory.

cd ~/projects
mkdir project-1215
cd project-1215

Edit a new file named "main.swift"

emacs main.swift

For this project you'll repeatedly edit the same file. It's probably easiest to test this file within emacs by typing: M-& (async-shell-command) and observing the results. For each question, be sure to understand the behavior before proceeding to the next question. It will be helpful to record your answers and reasoning for later review.

Oxygen480-actions-help-hint.svg Helpful hint: The async-shell-command can be executed with Alt-Shift-7. The command to execute each time is: swift main.swift

1

func doIt(a:Int, b:Int) -> Int {
    return a - b
} //

let a : Int = 7
let b : Int = 8
let c = doIt(a:Int, b:Int)
print(c)

What is output? (Ignore whitespace)

1
Compile or runtime error
-1

2

func doIt(a:Int, b:Int) -> Int {
    return a - b
} //

let a : Int = 7
let b : Int = 8
let c = doIt(a:a, b:b)
print(c)

What is output? (Ignore whitespace)

1
-1
Compile or runtime error

3

func doIt(a:Int, b:Int) -> Int {
    return a - b
} //

let a : Int = 7
let b : Int = 8
let c = doIt(a:b, b:a)
print(c)

What is output? (Ignore whitespace)

Compile or runtime error
1
-1

4

func doIt(a:Int, b:Int) -> Int {
    return a - b
} //

let a : Int = 7
let b : Int = 8
let c = doIt(a:b, b:b)
print(c)

What is output? (Ignore whitespace)

Compile or runtime error
1
-1

5

func doIt(a:Int, b:Int) -> Int {
    return a - b
} //

func doIt(a:Double, b:Double) -> Double {
    return b - a
} //

let a : Double = 7
let b : Double = 8
let c = doIt(a:a, b:b)
print(c)

What is output? (Ignore whitespace)

1.0
Compile or runtime error
-1
1.0

6

func doIt(a:Int, b:Int) -> Int {
    return a - b
} //

func doIt(a:Double, b:Double) -> Double {
    return b - a
} //

func doIt(a:Double, b:Double, c:Double) -> Double {
    return c * (b - a)
} //

let a : Double = 7
let b : Double = 8
let c : Double = 9
let d = doIt(a:a, b:b, c:c)
print(d)

What is output? (Ignore whitespace)

-9.0
9
Compile or runtime error
9.0

7

func doIt(a:Int, b:Int) -> Int {
    return a - b
} //

func doIt(a:Double, b:Double) -> Double {
    return b - a
} //

func doIt(a:Double, b:Double, c:Double) -> Double {
    return c * (b - a)
} //

let a : Int = 7
let b : Int = 8
let c : Int = 9
let d = doIt(a:a, b:b, c:c)
print(d)

What is output? (Ignore whitespace)

Compile or runtime error
9.0
9
-9.0

8

func doIt(a:Int, b:Int) -> Int {
    return a - b
} //

func doIt(a:Double, b:Double) -> Double {
    return b - a
} //

func doIt(p:Double, q:Double) -> Double {
    return p * q
} //

func doIt(a:Double, b:Double, c:Double) -> Double {
    return c * (b - a)
} //

let a : Double = 7
let b : Double = 8
let c : Double  = 9
let m : Double = 10
let n : Double = 11
let d = doIt(a:c, b:b)
let e = doIt(p:m, q:n)
print(d, e)

What is output? (Ignore whitespace)

-1.0 110.0
Compile or runtime error
-1.0 72
1.0 110.0