Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1.

Similar presentations


Presentation on theme: "1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1."— Presentation transcript:

1 1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1

2 Functions Review Advantages Name, parameters, return-info Flow of the data when running functions Menus Functions Apllying Functions to Real Projects 2

3 Functions Review: 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

4 Functions Terminology: Function definition: The code that performs the task for which the function was created. function [locs pks]=peakseek(x, minpeakdist, minpeakh) % documentation *Source: http://www.mathworks.com/matlabcentral/fileexchange/26581-peakseek 4

5 Functions Function 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

6 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

7 Functions Function call Function header function [locs pks]=peakseek(x, minpeakdist, minpeakh) [indx xvals] = peakseek(xvector, min_dist, min_h) Arguments copied into parameters Return values copied into collection variables The function body executes: it uses these values to calculate the values of return-values. 7

8 Functions Main 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

9 Real life #1: Menus Example function – process menu: function destination = ShowMenu(dests) % Use it as: destination = ShowMenu(dests) % decides which destination user wants %find how many option user has s = size(dests); rows = s(1); %store # of rows choice = 0; %so loop runs once while 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 destination destination = dests(choice, :); Example function call % Make the option list dests = strvcat('Atlanta', 'Chicago', 'New York'); % Call the function option = ShowMenu(dests); Where do you want to go? 1. Atlanta 2. Chicago 3. New York Enter destination number:2 9

10 Functions As 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

11 Real life#2: databases Create an example database program: % Fill database from file % Repeat until exit % Show menu for user to choose % Process menu choice % end of loop 11

12 Real life #2, cont. % Fill database filename = ‘my_data.txt’; db = FillDatabase(filename); choice = ‘’; % Repeat until exit while choice ~= ‘x’ % Show menu for user to choose choice = ShowDBmenu(); choice = lower(choice); % Process menu choice db = 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

13 Real life #2, cont. function dbase = FillDatabase(fname) % code for reading files after break function choice = ShowDBmenu() % code for menu similar to previous slide % See the processMenu() function for what % items might be on the menu 13

14 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 default option = lower(option); switch(option) %actually execute option case ‘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

15 DONE WITH FUNCTIONS.. Any questions? 15


Download ppt "1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1."

Similar presentations


Ads by Google