W1215 Function Signatures

From Coder Merlin
Revision as of 22:51, 19 January 2021 by Chukwuemeka-tinashe (talk | contribs) (→‎Experiment)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 ~/Experiences
mkdir W1215
cd W1215
swift-init

Edit a new file named "main.swift"

emacs main.swift
Hint.pngHelpful Hint

For this project you'll repeatedly edit the same file. It's probably easiest to test this file within emacs by typing: F5-r 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.

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

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)

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

Compile or runtime error
-9.0
9.0
9

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)

9
-9.0
9.0
Compile or runtime error

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 110.0
-1.0 72

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

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