1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1.

Slides:



Advertisements
Similar presentations
ITEC 1001 Tutorial 1 Using Common Features of Microsoft Office 2003.
Advertisements

Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
CS0004: Introduction to Programming Repetition – Do Loops.
Scripts and Flow Control. Scripts So far we have been entering commands directly into the command line But there is a better way Script files (and functions)
Program Design and Development
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
XP Using Common Features of Microsoft Office Tutorial 1.
Chapter 3 Planning Your Solution
ADVANCED MICROSOFT POWERPOINT Lesson 6 – Creating Tables and Charts
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Functions 1 parameter, 2 return-values "Conversion of time format" One problem. 5 steps to solve it. 1.
XP New Perspectives on Introducing Microsoft Office XP Tutorial 1 1 Introducing Microsoft Office XP Tutorial 1.
Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
1 -Defined Functions 1. Goals of this Chapter 2. General Concept 3. Advantages 4. How it works Programmer.
1 Functions Advantages Name, parameters, return info Flow of the data when running functions Menus Functions Applying Functions to Real Projects.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
An Introduction to Designing and Executing Workflows with Taverna Katy Wolstencroft University of Manchester.
XP New Perspectives on Integrating Microsoft Office XP Tutorial 2 1 Integrating Microsoft Office XP Tutorial 2 – Integrating Word, Excel, and Access.
CSC 221: Computer Programming I Fall 2001  top-down design  approach to problem solving, program design  focus on tasks, subtasks, …  reference parameters.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Software Project Planning Defining the Project Writing the Software Specification Planning the Development Stages Testing the Software.
Linux Operations and Administration
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Intro to Programming Web Design ½ Shade Adetoro. Programming Slangs IDE - Integrated Development Environment – the software in which you develop an application.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Review Expressions and operators Iteration – while-loop – for-loop.
Project Planning Defining the project Software specification Development stages Software testing.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
User-Written Functions
Chapter 6: User-Defined Functions I
COMPUTATIONAL CONSTRUCTS
Scripts & Functions Scripts and functions are contained in .m-files
CSCI 161: Introduction to Programming Function
User-Defined Functions
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Building Web Applications
Introduction to Programming
Chapter 6: User-Defined Functions I
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Functions continued.
Introduction to Programming
Presentation transcript:

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

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

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: 4

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

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

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

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

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

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

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

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

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

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

DONE WITH FUNCTIONS.. Any questions? 15