1 / 15

1. Finishing Functions 2. Introducing Plotting

1. Finishing Functions 2. Introducing Plotting. Functions. Review Advantages Name, parameters, return-info Flow of the data when running functions Menus Functions Apllying Functions to Real Projects. Functions. Review: Why functions?

dacey
Download Presentation

1. Finishing Functions 2. Introducing Plotting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1. Finishing Functions2. Introducing Plotting

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

  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

  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 <function body> *Source: http://www.mathworks.com/matlabcentral/fileexchange/26581-peakseek

  5. Functions Function header – top line of function definition function [locs pks]=peakseek(x, minpeakdist, minpeakh) Return variables Parameters Function name What you want to call the function; also the main part of the file name All functions have their own variables! 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.

  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 Collection variables – these variables receive the values returned by the function (the values stored in the return variables) Argument variables exist in the calling program – they are NOT variables from the function!

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

  8. Functions Main program (test.m): Function (peakseek.m): function [locs pks]=peakseek(x,minpeakdist, minpeakh) . . . “Going” . . . [indx xvals] = peakseek(xvector, … min_dist, min_h) . . . “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!

  9. Real life #1: Menus Example function call % Make the option list dests = strvcat('Atlanta', 'Chicago', 'New York'); % Call the function option = ShowMenu(dests); 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<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 destination destination = dests(choice, :); Where do you want to go? 1. Atlanta 2. Chicago 3. New York Enter destination number:2

  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

  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

  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!

  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

  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

  15. Any questions? Done with functions..

More Related