Covenant College November 27, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
 2005 Pearson Education, Inc. All rights reserved Introduction.
An Introduction to Programming with C++ Fifth Edition
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 7: Methods.
Chapter 6: User-Defined Functions I
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
chap13 Chapter 13 Programming in the Large.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Introduction to Programming with RAPTOR
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
CPS120: Introduction to Computer Science Functions.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Chapter 5: Enhancing Classes
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
About the Presentations
JavaScript: Functions
Scripts & Functions Scripts and functions are contained in .m-files
Programmazione I a.a. 2017/2018.
User-Defined Functions
C++ for Engineers and Scientists Second Edition
Chapter 5 - Functions Outline 5.1 Introduction
Engineering Computation with MATLAB
Presentation transcript:

Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions

Covenant College November 27, Introduction Chapter discusses the nature, implementation, and behavior of user-defined functions in MATLAB We will consider: –Defining a function –How to return data including multiple results –How to include other functions as assistants to your own function Writing your own functions allows you to isolate and package together a code block, so you can apply that code block to different sets of input data.

Covenant College Ch. 5 Functions I.Concepts: Abstraction and Encapsulation II.Black Box View of a Function III.MATLAB Implementation IV.Engineering Example November 27, 20153

Covenant College November 27, Concepts: Abstraction and Encapsulation Function – an implementation of procedural abstraction and encapsulation. Procedural abstraction – concept that permits a code block that solves a particular subproblem to be packaged and applied to different data inputs. Encapsulation – concept of putting a wrapper around a collection that you wish to protect from outside influence. Functions encapsulate code in two ways: –Variables declared within the function are not visible from elsewhere –Function ’ s ability to change the values of variables (known as side effects) is restricted to its own code body

Covenant College November 27, Black Box View of a Function Abstract view of a function:

Covenant College November 27, Black Box View of a Function Abstraction of a function consists of two parts: –Definition of the interface by which the user passes data items to and from the function –The code block that produces the results required by the interface A function definition consists of the following components; –A name that follows the same syntactic rules as a variable name –A set of 0 or more parameters provided to the function –Zero or more results to be returned to the caller of the function

Covenant College November 27, MATLAB Implementation General template for implementing functions MATLAB implementation of that template

Covenant College November 27, MATLAB Implementation General template for a function definition:

Covenant College November 27, MATLAB Implementation General template for a function definition: – section provides the name(s) of the results returned following an ‘ = ‘ sign If more than one result they are returned in a vector If nothing is to be returned the result list and = sign are omitted – is a name with the same syntactic rules as a variable name, and will be used to invoke the code body – is a comma-separated list of the names of the data to be provided to the function

Covenant College November 27, MATLAB Implementation General template for a function definition – one or more lines of comments that describe what the function does and how to call it; appear; appears in two situations: All the documentation lines up to the first non-document line are printed in the command window when you type the following: –>> help The first line is listed next to the file name in the Current Directory listing

Covenant College November 27, MATLAB Implementation Function Definition –Must be stored in a separate file located in a directory accessible to any script or function that calls it. –File containing the definition of a function named function_name must be.m –Listing 5.1 for function cylinder: Note line by line commentary for Listing 5.1 on page 120 of text.

Covenant College November 27, MATLAB Implementation Function Definition –All comments written immediately after the function header are available to the Command window –First comment line also appears in the Current Directory window as an indication of the basic purpose of the function –Good idea to include in the comments a copy of the function header line; often referred to as the Application Programmer Interface or API –The code body still has access to all built-in MATLAB names –Must make at least one assignment to the result variable –Function definitions need no END statement –Exercise 5.1: Saving and testing the cylinder function –Smith text, page 120, bottom

Covenant College November 27, MATLAB Implementation Storing and Using MATLAB Functions –Must be created like scripts in an m-file –First created – must be saved in an m-file with the same name as the function –May invoke the function by entering its name and parameters of the right type and number in the Command window, in script, or in other function definitions

Covenant College November 27, MATLAB Implementation Calling Functions –At function definition – user provides a list of the names of each data item expected to be provided by the caller –Referred to as formal parameters –Caller must provide the same number of data values expected by the function definition –These represent the actual parameters –Actual parameters can be generated by using constants Variables that have been defined The result of some mathematical operation(s) The result returned from other functions

Covenant College November 27, MATLAB Implementation Calling Functions –When the actual parameters have been computed, copies of their values are assigned as the values of the formal parameters the function is expecting. –Must be a one to one correpondence in name and data type –Values are assigned to parameters by position in the calling statement and function definition –Process of copying the actual parameters into the formal parameters is referred to as “ passing by value ” –Only parameter passing technique used by MATLAB –Restricting parameter access to passing by value can result in poor program performance

Covenant College November 27, MATLAB Implementation Calling Functions –If return variables have been defined for the function, every exit from the code body must assign valid values for the results –Provide the ability to deal with a variable number of parameters, both incoming and returning –MATLAB function NARGIN computes the actual number of parameters provided by the user in the current function call –Function NARGOUT computes the number of storage variables actually provided by the user

Covenant College November 27, MATLAB Implementation Returning Multiple Results –MATLAB unique in providing the ability to return more than one result from a function –Multiple results are specified as a vector of variable names –Assignments must be made to each of the result variables –Calling program is not required to make use of all the return values –Multiple results are specified as a vector of variable names, each of which must be assigned from the code body

Covenant College November 27, MATLAB Implementation Returning Multiple Results –Listing 5.2: CYLINDER function with multiple results – Exercise 5.2: Testing multiple returns –Smith text, page 122, bottom Note line by line commentary for Listing 5.2 on page 122 of text. Note added calculation in last line of function code block

Covenant College November 27, MATLAB Implementation Returning Multiple Results –The normal method to access the multiple answers is to put the names of the variables to receive the results in a vector –If you choose less than the full number of results (or none at all), the answers that are specified are allocated from left to right from the available results –Results are allocated by position in these vectors –Example: if you really only want the second result value, you must provide a dummy variable for the first, by making the call something on the order of: [junk, v] = cylinder(1, 1);

Covenant College November 27, MATLAB Implementation Auxiliary (Local) Functions –MATLAB uses the name of the file to identify the function; every function should normally be saved in its own m-file –May encounter times when you need an auxiliary (supporting) function within you user defined function –If the auxiliary function is only used in within the main function, it can be written in the same file as its calling function after the definition of the main function –Some append the word local_ to the name of local functions to distinguish them from main or generally callable functions; referred to as “ helper functions. ” –Note that the calling code can only reach the first function defined in the m-file

Covenant College November 27, MATLAB Implementation Encapsulation in MATLAB Functions –Use variable scoping –Variable scoping – defines the locations within your Command window, MATLAB system, and m-files to which variables may have access –Associated with the Current workspace window –Global scope - when running a script or using the Command window, and you access the value of a variable, the system will reach into the Current workspace and then into the MATLAB system libraries to find its current value

Covenant College November 27, MATLAB Implementation Encapsulation in MATLAB Functions –Local scope – when you write a function, its local variables, including the internal names of its parameters, are not included in your Current workspace, and it does not look into your Curren workspace for values of variables it needs –Variables within a function are NOT visible from outside and the function is unable to cause side effects by making assignments to outside variables

Covenant College November 27, MATLAB Implementation Global variables –Occasions occur when it is very inefficient to pass data into and out of a function; resolve by using global variables –Global variables must be defined in both the calling script and the function using the key word GLOBAL –Example: with a variable like BUFFER you can declare it global by placing the following statement just before its first use: global buffer –Function will then be able to access and modify the values in BUFFER without having to pass it in and out as a parameter

Covenant College November 27, MATLAB Implementation Global variables –Must be used with caution –By declaring a variable global you open the door to changing those data counterintuitively

Covenant College November 27, Engineering Example: Measuring a Solid Object Consider the disk in the diagram below We need to know the weight of this disk and the amount of paint required to finish it. Write a script to compute the volume of the disk and its wetted area (by paint).

Covenant College November 27, Engineering Example: Measuring a Solid Object Listing 5.3 Volume and area of a disk Note line by line commentary for Listing 5.3 on page 126 of text.

Covenant College November 27, Engineering Example: Measuring a Solid Object The script works fine with a vector of disk thicknesses to check the behavior as thickness varies Notice that for thin disks, the area is smaller with the holes. However, as the thickness increases, the area with the holes is larger than without, as one would expect Examine the results of script on the next slide

Covenant College November 27, Engineering Example: Measuring a Solid Object