Insight Through Computing 11. User-Defined Functions Input parameters Local Variables Output Values.

Slides:



Advertisements
Similar presentations
Methods Java 5.1 A quick overview of methods
Advertisements

10-6 Solving Quadratic Equations by Factoring
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.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
Solve by taking roots. Warm up. Homework Review Skills Check.
2-4 completing the square
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.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Functions 1 ENGR 1181 MATLAB 14.
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.
Chapter 1 A Beginning Library of Elementary Functions
Chapter 06 (Part I) Functions and an Introduction to Recursion.
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.
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
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.
Quadratics Solving equations Using “Completing the Square”
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Pre-Calculus Section 1.5 Equations Objectives: To solve quadratics by factoring, completing the square, and using the quadratic formula. To use the discriminant.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Matlab Programming for Engineers
MM2A3 Students will analyze quadratic functions in the forms f(x) = ax 2 +bx + c and f(x) = a(x – h) 2 = k. MM2A4b Find real and complex solutions of equations.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions.
Programming Fundamentals Enumerations and Functions.
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.
Macro Processor Design Options Recursive Macro Expansion General-Purpose Macro Processors Macro Processing within Language Translators.
4.6 Quadratic formula.
Using the Quadratic Formula to Find Solutions
Chapter 4 Quadratic Equations
Equations Quadratic in form factorable equations
L2. Basics Variables and Expressions Assignment Statements
4.6 Quadratic formula.
Solving Quadratic Equations by the Quadratic Formula
Section 11.2 The Quadratic Formula.
5.6 – The Quadratic Formula And Ch 5 Review
The Quadratic Formula.
Introduction to Classes and Objects
Communication and Coding Theory Lab(CS491)
L11. User-Defined Functions
Solving Simultaneous equations by the Graphical Method
Matlab Basics Tutorial
Equations Quadratic in form factorable equations
Classes and Objects Systems Programming.
 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:

Insight Through Computing 11. User-Defined Functions Input parameters Local Variables Output Values

Insight Through Computing Why? 1.Elevates reasoning by hiding details. 2.Facilitates top-down design. 3.Software management.

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

Insight Through Computing 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); :

Insight Through Computing Facilitates Top-Down Design

Insight Through Computing 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.

Insight Through Computing 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'.

Insight Through Computing 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.

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

Insight Through Computing 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

Insight Through Computing 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.

Insight Through Computing 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

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

Insight Through Computing 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;

Insight Through Computing 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)

Insight Through Computing 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

Insight Through Computing Input Arguments 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

Insight Through Computing Output Arguments 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; s

Insight Through Computing Think of MySqrt as a Factory As = Our method for approximating sqrt(A) MySqrt

Insight Through Computing Hidden Inner Workings As Can use MySqrt w/o knowing how it works. MySqrt

Insight Through Computing Practical Matters 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; The code sits in a separate file. MySqrt.m

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

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

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

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

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

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

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

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

Insight Through Computing 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.

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

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

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

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

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

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

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

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

Insight Through Computing 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.

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

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

Insight Through Computing 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 Because this is the current context

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

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

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

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

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

Insight Through Computing 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.

Insight Through Computing 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.

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