L11. User-Defined Functions

Slides:



Advertisements
Similar presentations
10-6 Solving Quadratic Equations by Factoring
Advertisements

Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
solution If a quadratic equation is in the form ax 2 + c = 0, no bx term, then it is easier to solve the equation by finding the square roots. Solve.
Functions.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Insight Through Computing 11. User-Defined Functions Input parameters Local Variables Output Values.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
Solving Quadratic Equations by Completing the Square
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
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.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
The Quadratic Formula. What does the Quadratic Formula Do ? The Quadratic formula allows you to find the roots of a quadratic equation (if they exist)
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Matlab Programming for Engineers
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Week 61 Introduction to Programming Ms. Knudtzon C Period Tuesday October 12.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Week91 APCS-A: Java Problem Solving November 2, 2005.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
4.6 Quadratic formula.
Topic: Python’s building blocks -> Variables, Values, and Types
Chapter 6: User-Defined Functions I
Solving Equations Graphically, Numerically, and Algebraically
Using the Quadratic Formula to Find Solutions
Chapter 4 Quadratic Equations
Equations Quadratic in form factorable equations
Learning to Program D is for Digital.
Key Ideas from day 1 slides
L2. Basics Variables and Expressions Assignment Statements
Algorithm & Programming
Matlab Training Session 4: Control, Flow and Functions
ALGORITHMS AND FLOWCHARTS
Testing UW CSE 160 Winter 2017.
Topic: Functions – Part 2
Introduction to Computer Programming
4.6 Quadratic formula.
Solving Quadratic Equations by the Quadratic Formula
CS21b: Structure and Interpretation
Welcome In The World Of ‘C’.  TEXT BOOK: Programming in ANSI ‘C By: E Balagurusamy. TMH  Reference Books: 1) The ‘C Programming Language By: Kernighan.
Warmup Solve –2x2 + 5x = 0. Approximate non-integral answers. A. 0
Design by Contract Fall 2016 Version.
Higher-Order Procedures
Section 11.2 The Quadratic Formula.
5.6 – The Quadratic Formula And Ch 5 Review
The Quadratic Formula.
Introduction to Classes and Objects
Gillis Mathematics Inequalities.
Dr. Bhargavi Dept of CS CHRIST
Communication and Coding Theory Lab(CS491)
Objective Solve quadratic equations by graphing.
Lecture 4 Structure plan
Chapter 6: User-Defined Functions I
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Solving Simultaneous equations by the Graphical Method
Matlab Basics Tutorial
Unit 3 Day 4.
Equations Quadratic in form factorable equations
Redundant code repositories
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Introduction to Classes and Objects
Presentation transcript:

L11. User-Defined Functions Input parameters Local Variables Output Values

Why? Elevates reasoning by hiding details. Facilitates top-down design. Software management.

Elevates Reasoning Nice to have sqrt function when designing a quadratic equation solver. You get to think at the level of ax2 + bx + c = 0

Elevates Reasoning Easier to understand the finished quadratic equation solving code: : r1 = (-b+sqrt(b^2-4*a*c))/(2*a); r2 = (-b-sqrt(b^2-4*a*c))/(2*a);

Facilitates Top-Down Design

Facilitates Top-Down Design 1. Focus on how to draw the flag given just a specification of what the functions DrawRect and DrawStar do. 2. Figure out how to implement DrawRect and DrawStar.

To Specify a Function… You describe how to use it, e.g., function DrawRect(a,b,L,W,c) % Adds rectangle to current window. % Assumes hold is on. Vertices are % (a,b),(a+L,b),(a+L,b+W), & (a,b+W). % The color c is one of 'r‘,'g', %'y','b','w','k','c',or 'm'.

To Implement a Function… You write the code so that the function works. I.e., code that “lives up to” the specification. E.g., x = [a a+L a+L a a]; y = [b b b+W b+W b]; fill(x,y,c); Not to worry. You will understand this soon.

Software Management Today: I write a function EPerimeter(a,b) that computes the perimeter of the ellipse

Software Management During the Next 10 years : You write software that makes extensive use of EPerimeter(a,b) Imagine 100’s of programs each with several lines that reference EPerimeter

Software Management After 10 years : I discover a more efficient way to approximate ellipse perimeters. I change the implementation of EPerimeter(a,b) You do not have to change your software at all.

Example 1. MySqrt(A) Recall that we can approximate square roots through the process of ractangle averaging L = A; W = A/L; L = (L+W)/2; W = A/L; etc

Package this Idea… L = A; W = A/L; for k=1:10 L = (L+W)/2; W = A/L; end s = (L+W)/2;

A User-Defined Function… function s = MySqrt(A) L = A; W = A/L; for k=1:10 L = (L+W)/2; W = A/L; end s = (L+W)/2;

A Function Begins with a Header function s = MySqrt(A) L = A; W = A/L; for k=1:10 L = (L+W)/2; W = A/L; end s = (L+W)/2; function s = MySqrt(A)

A Function Has a Name function s = MySqrt(A) L = A; W = A/L; for k=1:10 L = (L+W)/2; W = A/L; end s = (L+W)/2; MySqrt

Input Arguments function s = MySqrt( A ) L = A; W = A/L; A for k=1:10 L = (L+W)/2; W = A/L; end s = (L+W)/2; A

Output Arguments function s = MySqrt(A) L = A; W = A/L; s for k=1:10 L = (L+W)/2; W = A/L; end s = (L+W)/2; s

Think of MySqrt as a Factory = Our method for approximating sqrt(A)

Hidden Inner Workings MySqrt A s Can use MySqrt w/o knowing how it works.

Practical Matters The code sits in a separate file. MySqrt.m function s = MySqrt(A) L = A; W = A/L; for k=1:10 L = (L+W)/2; W = A/L; end s = (L+W)/2;

Practical Matters The .m file has the same name as the function. Thus, in MySqrt.m you will find an implementation of MySqrt.

Practical Matters The first non-comment in the file must be the function header statement. E.g., function s = MySqrt(A)

Syntax function = ( ) Name. Same rules as variable names List of input parameters. List of output parameters.

Practical Matters For now*, scripts and other Functions that reference MySqrt must be in the same directory. MyDirectory Script1.m MySqrt.m Script2.m OtherF.m Script3.m *The path function gives greater flexibility. More later.

Using MySqrt : r1 = (-b+MySqrt(b^2-4*a*c))/(2*a);

Understanding Function Calls There is a substitution mechanism. Local variables are used to carry out the computations.

Script function a = 1 b = f(2) c = 3 function y = f(x) z = 2*x y = z+1 Let’s execute the script line-by-line and see what happens during the call to f.

Script function a = 1 b = f(2) c = 3 function y = f(x) z = 2*x y = z+1 x, y, z serve as local variables during the process. x is referred to as an input parameter.

a = 1 b = f(2) c = 3 function y = f(x) z = 2*x y = z+1 1 a: Green dot tells us what the computer is currently doing.

Control passes to the function. b = f(2) c = 3 function y = f(x) z = 2*x y = z+1 The input value is assigned to x x: 2 1 a:

Control passes to the function. b = f( ) c = 3 function y = f( ) z = 2*x y = z+1 x 2 The input value is assigned to x x: 2 1 a:

a = 1 b = f(2) c = 3 function y = f(x) z = 2*x y = z+1 1 a: 2 x: 4 z:

a = 1 b = f(2) c = 3 function y = f(x) z = 2*x y = z+1 The last command is executed 1 a: 2 x: 4 z: 5 y:

Control passes back to the calling program b = f(2) c = 3 function y = f(x) z = 2*x y = z+1 After the the value is passed back, the call to the function ends and the local variables disappear. 1 a: 5 b:

a = 1 b = f(2) c = 3 function y = f(x) z = 2*x y = z+1 1 a: 5 b: 3 c:

Repeat to Stress the distinction between local variables and variables in the calling program.

Script function z = 1 x = f(2) y = 3 function y = f(x) z = 2*x y = z+1 Let’s execute the script line-by-line and see what happens during the call to f.

z = 1 x = f(2) y = 3 function y = f(x) z = 2*x y = z+1 1 z: Green dot tells us what the computer does next.

Control passes to the function. z = 1 x = f(2) y = 3 function y = f(x) z = 2*x y = z+1 The input value is assigned to x x: 2 1 z:

z = 1 x = f(2) y = 3 function y = f(x) z = 2*x y = z+1 1 z: 2 x: 4 z:

This does NOT change z = 1 x = f(2) y = 3 function y = f(x) z = 2*x y = z+1 Because this is the current context 1 z: 2 x: 4 z:

z = 1 x = f(2) y = 3 function y = f(x) z = 2*x y = z+1 The last command is executed 1 z: 2 x: 4 z: 5 y:

Control passes back to the calling program z = 1 x = f(2) y = 3 function y = f(x) z = 2*x y = z+1 After the the value is passed back, the function “shuts down” 1 z: 5 x:

z = 1 x = f(2) y = 3 function y = f(x) z = 2*x y = z+1 1 z: 5 x: 3 y:

Question Time x = 1; function y = f(x) x = f(x+1); x = x+1; y = x+1 What is the output? A. 1 B. 2 C. 3 D. 4 E. 5

Question Time x = 1; function y = f(x) x = f(x+1); x = x+1; y = x+1 What is the output? A. 1 B. 2 C. 3 D. 4 E. 5

Back to MySqrt function s = MySqrt(A) % A is a positive real number % and s is an approximation % to its square root. The specification is given in the form of comments just after the header statement.

Back to MySqrt function s = MySqrt(A) % A is a positive real number % and s is an approximation % to its square root. It must be clear, complete, and concise.

Back to MySqrt function s = MySqrt(A) % A is a positive real number % and s is an approximation % to its square root. If ever you write a function with no specification!!!