Z++
Suppose that Zamyla has designed a new programming language called Z++. The language is "loosely typed," which means you needn’t specify a variable’s type; values that look like integers will be treated as integers, values that look like strings will be treated as strings, and true
and false
will be treated as Booleans. As in C, a program written in Z++ must have a function called main
, which is automatically called when the program is executed. However, in Z++, functions don’t need to be declared, as with prototypes, before they are called. As in C, comments in Z++ begin with //
. But the language comprises only these building blocks, wherein { }
denotes one or more lines of code:
function name(parameters) { }
-
which defines a function called
name
that accepts as input a comma-separated list of zero or more parameters return(value)
-
which returns a single
value
from a function $variable
-
which represents a variable (of any type) called
$variable
x <- y
-
which stores
y
inx
, wherex
is necessarily a variable if (x) { }
-
which executes any code within
{ }
if and only ifx
istrue
(or non-zero) x < y
-
which evaluates to
true
ifx
is less thany
andfalse
otherwise add(x, y)
-
which returns the sum of
x
andy
, which are assumed to be integers get()
-
which gets a value (of any type) from the user and returns it
not(x)
-
which converts
true
tofalse
andfalse
totrue
print(x)
-
which prints
x
(of any type) and supports escape sequences like\n
while (x) { }
-
which iteratively executes any code within
{ }
whilex
istrue
(or non-zero)
Z++ has no other features. In particular, it does not support binary operators (other than <
), such as +
, -
, *
, /
, %
, &&
, ||
, !
, >
, >=
, ==
, or <=
(though it does support -
as a prefix for negative integers). But you can still program in it!
For instance, whereas in C you might write:
x = x - y;
in Z++ you would write:
$x <- add($x, -$y)
And here’s a whole program in Z++ that gets a user’s name and then says hello:
function main()
{
// Get user's name
$name <- get()
// Say hello
hello($name)
}
function hello($s)
{
// Print "hello, $s\n", where $s is user's name
print("hello, ")
print($s)
print("\n")
}
Answer the below in zpp.md
.
Questions
-
(2 points.) Implement a function in Z++ called
subtract
that accepts two arguments,x
andy
, which are assumed to be integers, and returns the value ofx
minusy
. -
(4 points.) Implement a function in Z++ called
multiply
that accepts two arguments,x
andy
, which are assumed to be integers, and returns the value ofx
timesy
. You may assume thatsubtract
now exists. -
(6 points.) Suppose that Z is a language identical to Z++ except that it does not have
while
. Implement a function in Z calledmultiply
that accepts two arguments,x
andy
, which are assumed to be integers, and returns the value ofx
timesy
. You may again assume thatsubtract
exists, but you may not assume that your other implementation ofmultiply
exists!
Debrief
-
Which resources, if any, did you find helpful in answering this problem’s questions?
-
About how long did you spend on this problem’s questions?