Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Covenant College November 27, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions."— Presentation transcript:

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

2 Covenant College November 27, 20152 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.

3 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

4 Covenant College November 27, 20154 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

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

6 Covenant College November 27, 20156 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

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

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

9 Covenant College November 27, 20159 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

10 Covenant College November 27, 201510 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

11 Covenant College November 27, 201511 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.

12 Covenant College November 27, 201512 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

13 Covenant College November 27, 201513 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

14 Covenant College November 27, 201514 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

15 Covenant College November 27, 201515 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

16 Covenant College November 27, 201516 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

17 Covenant College November 27, 201517 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

18 Covenant College November 27, 201518 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

19 Covenant College November 27, 201519 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);

20 Covenant College November 27, 201520 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

21 Covenant College November 27, 201521 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

22 Covenant College November 27, 201522 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

23 Covenant College November 27, 201523 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

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

25 Covenant College November 27, 201525 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).

26 Covenant College November 27, 201526 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.

27 Covenant College November 27, 201527 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

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


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

Similar presentations


Ads by Google