EGR 115 Introduction to Computing for Engineers User-Defined Functions1 – Part 1 Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers.

Slides:



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

Introducing JavaScript
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
User-defined Functions Selim Aksoy Bilkent University Department of Computer Engineering
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Chapter 6: User-Defined Functions I
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
User-defined Functions Selim Aksoy Bilkent University Department of Computer Engineering
Chapter 6: User-Defined Functions I
MATLAB and Simulinklecture 31 To days Outline  Functions  Strings  Sparse Arrays  Cell Arrays  Structures  Exercises on this days topics.
Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -
Chapter 6: Functions.
The Project AH Computing. Functional Requirements  What the product must do!  Examples attractive welcome screen all options available as clickable.
Programmer Defined Functions Matthew Verleger. Windows It’s estimated that Window’s XP contains 45 million lines of code (and it’s over 10 years old).
November 1, 2005 Lecture 12 - By Paul Lin 1 CPET 190 User Defined Functions Lecture 12 Problem Solving with MATLAB
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Introduction of C++ language. C++ Predecessors Early high level languages or programming languages were written to address a particular kind of computing.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
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.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
EGR 115 Introduction to Computing for Engineers Introduction to MATLAB Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers.
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.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 1 Monday 29 Sept 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers.
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 6 Methods Chapter 6 - Methods.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Chapter 3: User-Defined Functions I
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
EGR 115 Introduction to Computing for Engineers
User-Written Functions
Chapter 7: User-Defined Functions II
Coupling and Cohesion 1.
EGR 115 Introduction to Computing for Engineers
Introduction to Scripting
User-Defined Functions
Starting Out with Programming Logic & Design
Chapter 3 Simple Functions
Functions In Matlab.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Starting Out with Programming Logic & Design
Presentation transcript:

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 Functions Intro & 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 Task  Gives rise to reusable code o Isolation from unintended side effects  Self-contained blocks of code o MATLAB: Pass by value rather than pass by reference  Pass the contents (i.e., value) array rather than the address (i.e., reference) of the start of the array o 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 Functions Intro & 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 Code o Once verified & documented a piece of code (e.g., factorial routine) can be used by many software tasks 3.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 Functions Intro & 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 Functions Intro & 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 function out2 = in1; end % end swap2 swap2.m Slide 6 of 17

User Defined Functions Intro & 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 Functions Intro & 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 2 nd example, but, also provides a single output corresponding to the number of letters in YourLastName Slide 8 of 17

User Defined Functions Intro & 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 Functions Intro & 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 Functions Intro & 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 function cnt = length(LastName); end % end fn_ex3 fn_ex3.m Slide 11 of 17

User Defined Functions Intro & 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 effects o 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 completeness o Passes the address (i.e., location) of the array NOT the contents o Used by many other programming languages, when specified o Changes to data in function will modify values outside function Slide 12 of 17

User Defined Functions Intro & 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 Functions Intro & 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 Functions Intro & 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 window clear all; % Remove all variables from workspace close 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([ ]); pause(0.01); end test_my_polar2rect.m Slide 15 of 17

User Defined Functions Intro & Variable Passing – Further Example Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers Results my_polar2rect.m test_my_polar2rect.m 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