1. finishing functions 2. introducing plotting 1

15
1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1

Upload: avis-gibbs

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 2: 1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1

Functions

Review Advantages

Name, parameters, return-info

Flow of the data when running functions

Menus Functions

Apllying Functions to Real Projects

2

Page 3: 1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1

FunctionsReview:

Why functions?

Clarity – Replace multiple lines of code with a single function call

Modularity – Edit / replacement easily performed without impacting calling program

Reuse – Function can be shared for use with other programs

3

Page 4: 1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1

FunctionsTerminology:

Function definition:

The code that performs the task for which the function was created.

function [locs pks]=peakseek(x, minpeakdist, minpeakh)

% documentation

<function body>

*Source: http://www.mathworks.com/matlabcentral/fileexchange/26581-peakseek4

Page 5: 1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1

FunctionsFunction header – top line of function definition

function [locs pks]=peakseek(x, minpeakdist, minpeakh)

Function name ParametersReturn variables

What you want to call the function; also the main part of the file name

Variables in the function that receive a copy of the input values

Variables in the function that will receive the output values

All of the variables – parameters and return variables - are “local” to the function. This means that they only exist (and are only usable) in the function.

All functions have their own variables!

5

Page 6: 1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1

Functions

Function call – the command telling MATLAB to execute the function definition

[indx xvals] = peakseek(xvector, min_dist, min_h)

Function name – “go execute this function”

Arguments (input values) – information to be used by the function. These values are copied into the corresponding parameters of the function

Argument variables exist in the calling program – they are NOT variables from the function!

Collection variables – these variables receive the values returned by the function (the values stored in the return variables)

6

Page 7: 1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1

FunctionsFunction call

Function headerfunction [locs pks]=peakseek(x, minpeakdist, minpeakh)

[indx xvals] = peakseek(xvector, min_dist, min_h)

Arguments copied into parametersReturn values copied into collection variables

The function body executes: it uses these values to calculate the values of return-values.

7

Page 8: 1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1

FunctionsMain program (test.m): Function (peakseek.m):

.

.

.

[indx xvals] = peakseek(xvector, … min_dist, min_h)

.

.

.

function [locs pks]=peakseek(x,minpeakdist, minpeakh)

.

.

.

“Going”

“Returning”

Variables inside functions are NOT available in the main program (or other functions). Likewise for variables in the main function – they are not available in the functions!

8

Page 9: 1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1

Real life #1: MenusExample function – process menu:

function destination = ShowMenu(dests)% Use it as: destination = ShowMenu(dests)% decides which destination user wants

%find how many option user hass = size(dests);rows = s(1); %store # of rows

choice = 0; %so loop runs oncewhile choice<1 || choice>rows || mod(choice,1)~=0 clc fprintf('Where do you want to go? \n');

%show each destination for d = 1:rows fprintf('%d. %s\n', d, dests(d, :)); end choice = input(‘Enter destination number:'); end

%copy actual final destinationdestination = dests(choice, :);

Example function call

% Make the option listdests = strvcat('Atlanta', 'Chicago', 'New York'); % Call the functionoption = ShowMenu(dests);

Where do you want to go? 1. Atlanta 2. Chicago 3. New YorkEnter destination number:2

9

Page 10: 1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1

FunctionsAs programs grow, begin thinking of programs as

sequences of “code blocks”. Code blocks are sections of the program that together perform a task or are naturally aligned.

Frequently, these code blocks fit naturally into functions. Just a few examples:

- showing and processing a menu- reading / writing disk files- numeric computations

10

Page 12: 1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1

Real life #2, cont.% Fill databasefilename = ‘my_data.txt’;db = FillDatabase(filename);

choice = ‘’;

% Repeat until exitwhile choice ~= ‘x’

% Show menu for user to choosechoice = ShowDBmenu();choice = lower(choice);

% Process menu choicedb = processMenu(choice, db);

end % end of loop

While very simple, this program could act as a database management program – because we have functions to handle

the work!

12

Page 14: 1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1

Real life #2, cont.function db = processMenu(option, db)% Use it as: … , processes the menu option chosen by user

%change the “option” parameter lower case by defaultoption = lower(option);

switch(option) %actually execute optioncase ‘a’

db = db_add(db);case ‘d’

db = db_delete(db);case ‘e’

db = db_edit(db);case ‘r’

db = db_report(db);case ‘x’

db_exit(db);end 14