2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.

Slides:



Advertisements
Similar presentations
Procedural programming in Java
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Introduction to Computers and Programming Introduction to Methods in Java.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
CS 117 Spring 2002 Functions Hanly - Chapter 5 Friedman-Koffman - Sections & Chapter 6.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
CS 201 Functions Debzani Deb.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Chapter 6: User-Defined Functions I
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Introduction to Methods
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 06 (Part I) Functions and an Introduction to Recursion.
2.1 Functions. f x y z f (x, y, z) 3 Functions (Static Methods) Java function. n Takes zero or more input arguments. n Returns one output value. Applications.
2.1 Functions Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October 21, 2015.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Working with arrays (we will use an array of double as example)
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Procedural programming in Java Methods, parameters and return values.
2.1 Functions Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · 9/10/08 9/10/08.
Recap of Functions. Functions in Java f x y z f (x, y, z) Input Output Algorithm Allows you to clearly separate the tasks in a program. Enables reuse.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
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 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods Dr. Musab Zghoul.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
2.4 A Case Study: Percolation Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 6: User-Defined Functions I
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 6 Methods 1.
Function Call Trace public class Newton {
User-Defined Functions
Exam 2 Review 1.
Chapter 6 Methods.
Chapter 5 Methods.
Chapter 6: User-Defined Functions I
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Introduction to Computer Science I.
Methods/Functions.
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

2.1 Functions

Functions in Mathematics f x y z f (x, y, z) Domain Range

Functions in Computer Science f x y z f (x, y, z) Input Output Algorithm Allows you to clearly separate the tasks in a program. Enables reuse of code

4 Functions (Static Methods) Java function. n Takes zero or more input arguments. n Returns zero or one output value. n public static void main(String args[]) Applications. n Scientists use mathematical functions to calculate formulas. n Programmers use functions to build modular programs. n You use functions for both. Examples. Built-in functions: Math.random(), Math.abs(), Integer.parseInt(). Princeton I/O libraries: StdIn.readInt(), StdDraw.line(), StdAudio.play(). User-defined functions: main().

5 Writing Functions in Java Java functions. Easy to write your own. f(x) =  x input … output Numerically Computing a Square-Root: Newton-Raphson Method

The Newton-Raphson Algorithm To Compute Square Root of Positive Number c: { t = c; //initial estimate of square-root while(square-root of c is not found) { if(t == c/t) found square-root of x; else t = Average of t and c/t; } 6

7 Function to calculate Square Root using Newton-Raphson Method f(x) =  x input … output

8 Functions overloading multiple arguments Overloading: Different argument types Different number of arguments Different return value is NOT overloading

9 Flow of Control Key point. Functions provide a new way to control the flow of execution.

10 Flow of Control Key point. Functions provide a new way to control the flow of execution. Summary of what happens when a function is called: n Control transfers to the function code. n Argument variables are assigned the values given in the call. n Function code is executed. n Return value is assigned in place of the function name in calling code. n Control transfers back to the calling code. Note. This is known as “pass/call by value.”

Can a Function Alter Its Arguments? The short answer for Java: Only if the argument is an array! n (Later we’ll see that objects are like arrays in this way.) Call by Value. n The argument-variable defined in the function signature is like a local variable inside the function. n A copy of the value of the actual argument is copied into the argument-variable when its called n Changes made to the argument-variable inside the function do not update anything back in the “caller”. Call by Reference. n The argument-variable defined in the function signature refers to the same data variable that’s passed when the function is called. n Changes made to the argument-variable inside the function do update the variable in the “caller”. 11

12 Scope Scope (of a name). The code that can refer to that name. Ex. A variable's scope is code following the declaration in the block. Best practice: declare variables to limit their scope. public class Newton { public static double sqrt(double c) { double epsilon = 1e-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * t) t = (c/t + t) / 2.0; return t; } public static void main(String[] args) { double[] a = new double[args.length]; for (int i = 0; i < args.length; i++) a[i] = Double.parseDouble(args[i]); for (int i = 0; i < a.length; i++) { double x = sqrt(a[i]); StdOut.println(x); } two different variables with the same name i scope of c scope of epsilon scope of t

13 Why Are Functions An Important Concept / Technique? Good Software Design. n Problem-solving: from large problems to smaller sub-problems. n Easier to understand solutions to smaller sub-problems. n Easier to test smaller sub-problems. n We can re-use sub-problem solutions (functions). n Hide details from rest of design. (Example: sqrt. Do we care how it’s done inside function?) Abstraction. Reducing or factoring out details so that one can focus on a few important concepts // Two functions for getting random values // between 0 and N-1 public static int randomVal (int N) { return (int) (Math.random() * N); } // between a and b, i.e. in range [a,b) public static double randomVal(double a, double b) { return a + Math.random() * (b-a); }

14 Function Challenge 1a Q. What happens when you compile and run the following code? public class Cubes1 { public static int cube(int i) { int j = i * i * i; return j; } public static void main(String[] args) { int N = Integer.parseInt(args[0]); for (int i = 1; i <= N; i++) StdOut.println(i + " " + cube(i)); } % javac Cubes1.java % java Cubes

15 Function Challenge 1b Q. What happens when you compile and run the following code? public class Cubes2 { public static int cube(int i) { int i = i * i * i; return i; } public static void main(String[] args) { int N = Integer.parseInt(args[0]); for (int i = 1; i <= N; i++) StdOut.println(i + " " + cube(i)); } Compiler error: i is already defined

16 Function Challenge 1c Q. What happens when you compile and run the following code? public class Cubes3 { public static int cube(int i) { i = i * i * i; } public static void main(String[] args) { int N = Integer.parseInt(args[0]); for (int i = 1; i <= N; i++) StdOut.println(i + " " + cube(i)); } Compiler error: missing return statement

17 Function Challenge 1d Q. What happens when you compile and run the following code? public class Cubes4 { public static int cube(int i) { i = i * i * i; return i; } public static void main(String[] args) { int N = Integer.parseInt(args[0]); for (int i = 1; i <= N; i++) StdOut.println(i + " " + cube(i)); } Correct: the i in cube() and the i in main() are different

18 Function Challenge 1e Q. What happens when you compile and run the following code? public class Cubes5 { public static int cube(int i) { return i * i * i; } public static void main(String[] args) { int N = Integer.parseInt(args[0]); for (int i = 1; i <= N; i++) StdOut.println(i + " " + cube(i)); } Correct and preferred style

Dr. Java Demo Printing An Array + Debugging 19