Difference between revisions of "W1215 Function Signatures"

From Coder Merlin
 
Line 12: Line 12:
Create a directory within your "project" directory.  
Create a directory within your "project" directory.  
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
cd ~/projects
cd ~/Experiences
mkdir project-1215
mkdir W1215
cd project-1215
cd W1215
swift-init
</syntaxhighlight>
</syntaxhighlight>


Line 22: Line 23:
</syntaxhighlight>
</syntaxhighlight>


{{Hint|
For this project you'll repeatedly edit the same file.  It's probably easiest to test this file ''within emacs'' by typing:
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.
{{Key|F5}}-{{Key|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.
 
}}
{{notice|[[File:Oxygen480-actions-help-hint.svg|frameless|30px]]|Helpful hint:  The '''async-shell-command''' can be executed with ''Alt-Shift-7''.  The command to execute each time is: ''swift main.swift''}}


<quiz shuffleanswers=true display=simple>
<quiz shuffleanswers=true display=simple>

Latest revision as of 22:51, 19 January 2021

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

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

-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)

9.0
-9.0
9
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
-1.0 110.0
-1.0 72
Compile or runtime error

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

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.0
1.0
-1