EGR 115 Introduction to Computing for Engineers User-Defined Functions1 – Part 1 Wednesday 22 Oct...
-
Upload
ginger-richards -
Category
Documents
-
view
212 -
download
0
Transcript of EGR 115 Introduction to Computing for Engineers User-Defined Functions1 – Part 1 Wednesday 22 Oct...
EGR 115 Introduction to Computing for Engineers
User-Defined Functions1 – Part 1
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
Lecture Outline
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
• User Defined Functions Intro & variable passing
Slide 2 of 17
User Defined FunctionsIntro & Variable Passing
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
• What are functions in programming? “Small” Self Contained Computing Units
o Dedicated to a Single Computing TaskGives rise to reusable code
o Isolation from unintended side effectsSelf-contained blocks of code
o MATLAB: Pass by value rather than pass by referencePass the contents (i.e., value) array rather than the address
(i.e., reference) of the start of the arrayo Independent test & exclusion from debug once fully verified
Divide and conquer!! o More organized code compartmentalized by function
MODULARITY!!!
Slide 3 of 17
User Defined FunctionsIntro & Variable Passing
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
• What are the main benefits of using functions?1. Independent testing of sub-tasks
o Verification of a small piece of code in isolation is easier than verification of a massive software task
2. Reusable Codeo Once verified & documented a piece of code (e.g., factorial
routine) can be used by many software tasks3. Isolation from unintended side effects
o Data used by function is isolated to that function (scope of data) and will not damage or be damaged by other code in your software project
Slide 4 of 17
User Defined FunctionsIntro & Variable Passing
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
• General Structure of a Function in MATLAB
function [out1, out2, …] = fn_name (in1, in2, …)% Function Description:% …% INPUTS:% …% OUTPUTS:% …% NOTES/COMMENTS:Some code;…end % end fn_name
out1 and out2 are the outputs.
in1 and in2 are the inputs.
These are local copies: MATLAB only passes by "value" not by "reference."
MUST be saved in a file called
fn_name.m
ALWAYS add comments to describe & document the function
The “end” statement is optional but good practice
Slide 5 of 17
User Defined FunctionsIntro & Variable Passing
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
• An Example of a simple function Develop a function to swap two values
function [out1, out2] = swap2(in1, in2)% Function Description:% This function swaps in1 and in2% INPUTS:% in1, in2% OUTPUTS:% out1, out2% NOTES/COMMENTS:% None out1 = in2; % Body of the functionout2 = in1; end % end swap2
swap2.m
Slide 6 of 17
User Defined FunctionsIntro & Variable Passing
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
• Types of Functions Functions that just accomplish a task that may not even
require any inputs (e.g., clock) or outputs (e.g., pause)o Optional Arguments
Functions that accepts one value and return one value.o Single input / single output
Functions that calculate and return more than one value.o Multiple inputs and/or outputs
Slide 7 of 17
User Defined FunctionsIntro & Variable Passing
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
• First Simple example: Develop a function that requires NO input values and NO
output values and prints “Hello World!!” to the command window
• Second Simple example: Develop a function that accepts YourLastName as an input
argument/value and has NO output values and prints “Hello YourLastName!!” to the command window
• Third Simple example: Same as 2nd example, but, also provides a single output
corresponding to the number of letters in YourLastName
Slide 8 of 17
User Defined FunctionsIntro & Variable Passing – Example #1
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
function [] = fn_ex1()% Function Description:% This function prints Hello World!!% INPUTS:% none% OUTPUTS:% none% NOTES/COMMENTS:% None disp('Hello World!!'); % Body of the function end % end fn_ex1
fn_ex1.m
Slide 9 of 17
User Defined FunctionsIntro & Variable Passing – Example #2
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
function [] = fn_ex2(LastName)% Function Description:% This function prints Hello LastName!!% INPUTS:% LastName - A string of characters% OUTPUTS:% none% NOTES/COMMENTS:% None disp(['Hello ',LastName,'!!']); % Body of the function end % end fn_ex2
fn_ex2.m
Slide 10 of 17
User Defined FunctionsIntro & Variable Passing – Example #3
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
function [cnt] = fn_ex3(LastName)% Function Description:% This function prints Hello LastName!! and computes the number of% letters in LastName% INPUTS:% LastName - A string of characters% OUTPUTS:% cnt - The number of letters in LastName% NOTES/COMMENTS:% None disp(['Hello ',LastName,'!!']); % Body of the functioncnt = length(LastName);
end % end fn_ex3
fn_ex3.m
Slide 11 of 17
User Defined FunctionsIntro & Variable Passing
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
• Variable Passing Options Pass-by-Value: Used by default in MATLAB
o Upon Function invocation, a copy of the actual arguments are made, and they are copied into the dataspace of the function using Dummy argument names
o Helps to prevent unintended side effectso Function can modify dummy arguments and the data will
remain unchanged in the outside calling code Pass-by-Reference: Not supported by MATLAB
o Other option, included for completenesso Passes the address (i.e., location) of the array NOT the contentso Used by many other programming languages, when specifiedo Changes to data in function will modify values outside function
Slide 12 of 17
User Defined FunctionsIntro & Variable Passing – Further Example
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
• Develop a function to convert from polar coordinates (r, theta) to rectangular coordinates (x, y) Call the function “my_polar2rect”
o Be careful about potentially using a MATLAB function name Assume that the angle theta is provided in units of deg x, y, and r are in units of meters Two inputs of type double Two outputs of type double
Slide 13 of 17
User Defined FunctionsIntro & Variable Passing – Further Example
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
function [x, y] = my_polar2rect(r, theta)% Function Description:% Function to convert from polar to rectangular coordinates% % INPUTS:% r - radius (in meters)% theta - angle (in deg)%% OUTPUTS:% x - x-coordinate (in meters)% y - y-coordinate (in meters)%% NOTES/COMMENTS:% Note that the angle is provided in deg NOT radians theta = theta * pi / 180; % Convert from deg to radians (rad) x = r * cos(theta);y = r * sin(theta); end % end my_polar2rect
my_polar2rect.m
Slide 14 of 17
User Defined FunctionsIntro & Variable Passing – Further Example
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
%% COURSE: EE 115 Introduction to Computing for Engineers% DESCRIPTION: Main program to test polar2rect function% AUTHOR: Stephen Bruder% DATE: 10/19/2014% COMMENTS: None clc; % Clear the command windowclear all; % Remove all variables from workspaceclose all; theta = 0:5:5*360; % 5 full rotations (deg)N = length(theta); % Number of elements in the angle array (dimless)r = 0:1/N:1; % Grow the radius from 0 to 1 (m)x = zeros(1,N); % Initialize x (m)y = zeros(1,N); % Initialize y (m) for i=1:N [x(i), y(i)] = my_polar2rect(r(i), theta(i)); scatter(x, y, 'ro'); axis([-1.1 1.1 -1.1 1.1]); pause(0.01);end
test_my_polar2rect.m
Slide 15 of 17
User Defined FunctionsIntro & Variable Passing – Further Example
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
• Resultsmy_polar2rect.m
test_my_polar2rect.m
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Slide 16 of 17
Next Lecture
Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers
• User-Defined Functions2 Data visibility (Global, ...)
Slide 17 of 17