W1215 Function Signatures

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

Prerequisites[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
1
Compile or runtime error

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)

Compile or runtime error
-1
1

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)

-1
1
Compile or runtime error

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)

-1
1
Compile or runtime error

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
-1
1.0
Compile or runtime error

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
Compile or runtime error
9
-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.0
9

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)

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

9

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

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

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

What is output? (Ignore whitespace)

-1.0
1.0
-1
Compile or runtime error

10

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

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

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

What is output? (Ignore whitespace)

Compile or runtime error
-1
1.0
-1.0