Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Abstraction - Functions

Engineering System Design 2

Abstraction – Functions
(includes slides from Shanika Karunasekera)

Adrian Pearce
(adrianrp@unimelb.edu.au)

Engineering System Design 2 1 Melbourne School of Engineering


Abstraction - Functions

In this section you will learn


how to create self-contained modules, called
functions, that can then be put together to
build a MATLAB program

Engineering System Design 2 2 Melbourne School of Engineering


Abstraction - Functions

Where are we – roadmap?

• Programming features you have learnt so far:


– Input-output
– Calculation

• In this section you will learn:


– Abstraction – Creating self-contained modules (functions) that
can then be put together to build a program.

• In the following sections you will learn:


– Selection (Making choices) - Branching
– Iteration (Doing repetitive actions) - Looping

Engineering System Design 2 3 Melbourne School of Engineering


Abstraction - Functions

Overview

• Abstraction with functions

• Using functions

• Writing your own functions

Engineering System Design 2 4 Melbourne School of Engineering


Abstraction - Functions

Functions
• Abstraction: the process of creating self-contained,
reusable, units of software with clearly defined units
called functions.

• Functions contain:
– inputs;
– outputs; and
– behaviour;
that can then be put together to solve complex problems.

• You have already seen and used MATLAB built-in


functions for problem solving (e.g. disp, input) -
commands
Engineering System Design 2 5 Melbourne School of Engineering
Abstraction - Functions

MATLAB functions
• MATLAB functions have the following general usage syntax:

Outputs arguments Input


(Returned values) arguments
[y1, y2, ...] = function_name(x1, x2, ....)

> disp(‘Hello World’);


> fprintf(‘y = %f’, y);
> y = log(x);

– The number of inputs can vary from 0 to any number.


– The number of outputs can vary from 0 to any number.
– The type of data used as inputs and outputs can be any
MATLAB data type (e.g. Scalar variables, Vectors etc).

Engineering System Design 2 6 Melbourne School of Engineering


Abstraction - Functions

Creating MATLAB functions


• If there is no pre-defined function for your needs, you can
build your own.

• Following is a generic template for creating a function:


Output Input
parameters parameters
MATLAB keyword Function name

function [y1, y2, ..] = function_name(x1, x2, ..)


% Comments to describe the function
statement_1
statement_2 Function body
.....
end

Engineering System Design 2 7 Melbourne School of Engineering


Abstraction - Functions

Creating MATLAB functions

• function signature:
– The first line of the function is the function signature
– Contains the function name, input and output parameters

• function body:
– Contains commands for the function
– Input values (e.g. x1, x2) are used in commands within
the function body to compute output values (e.g. y1, y2)

Engineering System Design 2 8 Melbourne School of Engineering


Abstraction - Functions

Example: Function to add two numbers

• Problem Statement: Write a MATLAB function that returns the


sum of two variables.

function y = my_add(x1, x2)


% my_add.m
% This function adds two input values x1 and x2
% and returns the result y
y = x1 + x2;
end

Engineering System Design 2 9 Melbourne School of Engineering


Abstraction - Functions

Creating a function in MATLAB

• Following are the steps to create a function in MATLAB.


1. Change the directory to where you want to save the function.
2. Open the MATLAB editor.
3. Type in the function in the editor.
4. Save to a file [function_name].m (in the above example you must
save the code in a file my_add.m – the file name must be the
same as the function name)
5. Test your function through a script file or command line.
Note: A function must be called with the appropriate input parameters

The function is now available to be used by your MATLAB scripts.

Engineering System Design 2 10 Melbourne School of Engineering


Abstraction - Functions

Example: Function to add two numbers

% my_add_driver.m
clear all;

a = input('Enter the first number ');


b = input('Enter the second number ');
c = my_add(a, b);

fprintf('The sum of %d and %d = %d \n', a, b, c);

The name of the input and output


>> my_add_driver variables do not have to be the same as that
in the function definition. Here we are using
Enter the first number 5 a, b, c not x1, x2, y which are in the function
Enter the second number 6 definition.
The sum of 5 and 6 = 11

Engineering System Design 2 11 Melbourne School of Engineering


Abstraction - Functions

Example: Function with multiple return values

• Problem Statement: Write a function that returns the sum and


the difference of two variables.

function [y1, y2] = my_add_sub(x1, x2)


% my_add_sub.m
% This function returns the sum (y1) and the
% difference (y2) of two input values x1 and x2
y1 = x1 + x2;
y2 = x1 - x2;
end

Engineering System Design 2 12 Melbourne School of Engineering


Abstraction - Functions

Example: Function with multiple return values


% my_add_sub_driver.m
clear all;
a = input('Enter the first number ');
b = input('Enter the second number ');
[c, d] = my_add_sub(a, b);

fprintf('The sum of %d and %d = %d\n', a, b, c);


fprintf('The difference of %d and %d = %d\n', a, b, d);

>> my_add_sub_driver
Enter the first number 6
Enter the second number 7
The sum of 6 and 7 = 13
The difference of 6 and 7 = -1

Engineering System Design 2 13 Melbourne School of Engineering


Abstraction - Functions

Important facts regarding functions


• Rule 1: Variables defined inside a function are not visible to the
calling program, unless they are defined as return parameters.
function my_add_1(x1, x2)
% my_add_1.m
y = x1 + x2;
end

% my_add_driver_1.m
a = 5;
b = 6;
my_add_1(a, b)
fprintf('The sum of %d and %d = %d\n', a, b, y);

>> my_add_driver_1
Undefined function or variable 'y'.
Error in my_add_driver_1 (line 5)
fprintf('The sum of %d and %d = %d\n', a, b, y);
Engineering System Design 2 14 Melbourne School of Engineering
Abstraction - Functions

Important facts regarding functions


• Rule 2: Input values remain unchanged upon returning from the
function regardless of changes to them inside the function.
function my_test(x1)
% my_test.m
x1 = 2*x1;
end

% my_test_driver.m
x1 = 5;
my_test(x1);
fprintf('Value of x1 = %d\n', x1);

>> my_test_driver
Value of x1 = 5

Engineering System Design 2 15 Melbourne School of Engineering


Abstraction - Functions

Important facts regarding functions


• Rule 3: Input arguments to a function can be of any type, as long
as they are valid and meaningful for the commands in the function.

function y = my_add(x1, x2)


% my_add.m
y = x1 + x2;
end
% my_add_driver_2.m
a = [1 3 5];
b = [2 4 6];
c = my_add(a, b)

>> my_add_driver_2
c =

3 7 11

Engineering System Design 2 16 Melbourne School of Engineering


Abstraction - Functions

Function Design

• Following are some ground rules when designing


functions:
– A function should only do one specific task
– If it does more than one task, consider splitting it into multiple
functions
– When designing functions think about reusability – a good
function is one that is reusable and self-contained

Engineering System Design 2 17 Melbourne School of Engineering


Abstraction - Functions

Top-down design/programming with functions

Example: Hamming encoding

Write a MATLAB function called encode sample which accepts a 16 x 1


binary vector as input (the audio sample) and returns the associated
7 x 4 matrix of Hamming(7, 4) codewords.

Engineering System Design 2 18 Melbourne School of Engineering


Abstraction - Functions

Top-down design/programming with functions

Engineering System Design 2 19 Melbourne School of Engineering


Abstraction - Functions

Top-down design/programming with functions


Pseudocode:
– Pseudocode is the textural description of an algorithm that has
the structure of a programming language, but is intended for
human reading rather than machine reading.

Engineering System Design 2 20 Melbourne School of Engineering


Abstraction - Functions

Top-down design/programming with functions


MATLAB top-level code:

function hamming_s = encode_sample(s)


% encode a 16 bit sample into four Hamming (7,4)codes

% convert sample into four nybbles


split_s = nybblise(s);

% allocate empty matrix


hamming_s = zeros(7, 4);

% convert the samples to Hamming (7,4) codes


hamming_s(:,1) = hamming_7_4(split_s(:,1));
hamming_s(:,2) = hamming_7_4(split_s(:,2));
hamming_s(:,3) = hamming_7_4(split_s(:,3));
hamming_s(:,4) = hamming_7_4(split_s(:,4));
end

Engineering System Design 2 21 Melbourne School of Engineering


Abstraction - Functions

Top-down design/programming with functions

MATLAB second-level code:

function S = nybblise(x)
% encode a 16x1 vector x and splits it into a
% 4x4 matrix (four nybbles) S

% Fill me in!

end

Engineering System Design 2 22 Melbourne School of Engineering


Abstraction - Functions

Top-down design/programming with functions

MATLAB second-level code (cont’d):

function h = hamming_7_4(d)
% takes a nybble (four bit vector) and returns
% the associated Hamming (7,4) code
% (p1 p2 d1 p3 d2 d3 d4) h

% Fill me in!

end

Engineering System Design 2 23 Melbourne School of Engineering

You might also like