Notes on Homework 1¶
In [1]:
format compact
The last question asks you to use boolean_print_TT_fn.m (available on canvas) to print the truth table. Please read this additional note if you have trouble using this function.
Function as an input variable¶
Typically you pass data (e.g. scalars, arrays…) to a function. But the
function boolean_print_TT_fn()
accepts another function as the
input variable.
Inline function as an input variable¶
Let’s make two inline functions:
In [2]:
func1 = @(a) a;
func2 = @(a,b) a&b;
boolean_print_TT_fn(func,1)
prints the truth table for a function
with a single input:
In [3]:
boolean_print_TT_fn(func1,1)
_____
a|out
0|0
1|1
boolean_print_TT_fn(func,2)
prints the truth table for a function
with two inputs:
In [4]:
boolean_print_TT_fn(func2,2)
_______
a|b|out
0|0| 0
0|1| 0
1|0| 0
1|1| 1
Standard function as an input variable¶
However, if you try to pass a standard function (i.e. defined in a
separate file) to boolean_print_TT_fn()
, it will throw you some
weird error:
In [5]:
%%file func2_fn.m
function s=func2_fn(a,b)
s = a&b;
end
Created file '/Users/zhuangjw/Research/Computing/personal_web/AM111/docs/func2_fn.m'.
In [6]:
boolean_print_TT_fn(func2_fn,2)
Not enough input arguments.
Error in func2_fn (line 2)
s = a&b;
To fix this error, you can put @
in front of your function, as
suggested
here.
In [7]:
boolean_print_TT_fn(@func2_fn,2)
_______
a|b|out
0|0| 0
0|1| 0
1|0| 0
1|1| 1
(That’s MATLAB-specific design. Other languages like Python treat inline and standard functions in the same way.)
Print truth table for half adder¶
Function with multiple return¶
A Half adder has two return values. One way to return multiple values is
In [8]:
%%file multi_return.m
function [carry,s] = multi_return(a,b)
% This is not a corret adder! You should write your own!
carry = 0;
s = 1;
end
Created file '/Users/zhuangjw/Research/Computing/personal_web/AM111/docs/multi_return.m'.
However, by default you only get the first output variable carry
!
In [9]:
multi_return(0,0)
ans =
0
To get both carry
and s
, you have to use two variables to hold
the output results.
In [10]:
[out1, out2] = multi_return(0,0)
out1 =
0
out2 =
1
A perhaps more convenient way is to return a vector containing all outputs you need:
In [11]:
%%file fake_half_adder.m
function out = fake_half_adder(a,b)
% This is not a corret adder! You should write your own!
carry = 0;
s = 1;
out = [carry,s];
end
Created file '/Users/zhuangjw/Research/Computing/personal_web/AM111/docs/fake_half_adder.m'.
This time, you don’t have to write two variables to hold the output results:
In [12]:
fake_half_adder(1,1)
ans =
0 1
Print truth table using boolean_print_TT_fn.m¶
boolean_print_TT_fn()
will print the complete result only if you
use a single vector as the return value for you adder. Use 3
to get
the format for half-adder.
In [13]:
boolean_print_TT_fn(@multi_return,3) % not printing complete result
_____________
a|b|carry|sum
0|0| 0
0|1| 0
1|0| 0
1|1| 0
In [14]:
boolean_print_TT_fn(@fake_half_adder,3) % can print complete result
_____________
a|b|carry|sum
0|0| 0 1
0|1| 0 1
1|0| 0 1
1|1| 0 1
Print truth table on your own¶
If you don’t want to use boolean_print_TT_fn.m, it is also quite straightforward to print the table on your own:
In [15]:
disp('a,b|c,s')
for a=0:1
for b=0:1
fprintf('%d,%d|%d,%d \n',a,b,fake_half_adder(a,b))
end
end
a,b|c,s
0,0|0,1
0,1|0,1
1,0|0,1
1,1|0,1
Type doc fprintf
to see more formatting options.
Print truth table for full adder¶
Use 4
to get the format for full-adder.
In [16]:
%%file fake_full_adder.m
function out = fake_full_adder(a,b,c)
carry = 0;
s = 1;
out = [carry,s];
end
Created file '/Users/zhuangjw/Research/Computing/personal_web/AM111/docs/fake_full_adder.m'.
In [17]:
boolean_print_TT_fn(@fake_full_adder,4)
________________
a|b|c|carry|sum
0|0|0|0 1
0|0|1|0 1
0|1|0|0 1
0|1|1|0 1
1|0|0|0 1
1|0|1|0 1
1|1|0|0 1
1|1|1|0 1