06_functions - 2.1 Functions 2.1 Functions x y z f f (x, y,...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 2.1 Functions 2.1 f x y z f ( x, y, z ) 3 Functions (Static Methods) Java function. Takes zero or more input arguments. Returns one output value. Applications. Scientists use mathematical functions to calculate formulas. Programmers use functions to build modular programs. You use functions for both. Examples. Built-in functions: Math.random() , Math.abs() , Integer.parseInt() . Our I/O libraries: StdIn.readInt() , StdDraw.line() , StdAudio.play() . User-defined functions: main() . 4 Anatomy of a Java Function Java functions. Easy to write your own. f ( x ) = x input 2.0 1.414213… output
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 5 Functions overloading multiple arguments 6 Flow of Control Flow of control. Functions provide a new way to control the flow of execution of a program. "pass-by-value" 7 Scope Scope. Set of statements that can refer to that name. Scope of a variable defined within a block is limited to the statements in that block. Best practice: declare variables to limit their scope. public class Scope { cube ( int i ) { } public static void main ( String [] args ) { } } two variables named i are independent % java Scope 3 1 1 2 8 3 27 including function block Gaussian Distribution
Background image of page 2
3 9 Gaussian Distribution Standard Gaussian distribution. "Bell curve." Basis of most statistical analysis in social and physical sciences. Ex. 2000 SAT scores follow a Gaussian distribution with mean μ = 1019, stddev σ = 209. φ ( x ) = 1 2 π e x 2 / 2 810 601 1437 ( x , μ , σ ) = 1 2 e ( x ) 2 / 2 2 = x ( ) / 10 Java Function for φ (x) Mathematical functions. Use built-in functions when possible; build your own when not available. Overloading. Functions with different signatures are different. Multiple arguments. Functions can take any number of arguments. Calling other functions. Functions can call other functions. library or user-defined public class Gaussian { public static double phi ( double x ) { return Math . exp (- x * x / 2 ) / Math . sqrt ( 2 * Math . PI ); } public static double phi ( double x , double mu , double sigma ) { return phi (( x - mu ) / sigma ) / sigma ; } } ( x ) = 1 2 e x 2 / 2 ( x , , ) = x ( ) / 11 Gaussian Cumulative Distribution Function Goal. Compute Gaussian cdf Φ (z). Challenge. No "closed form" expression and not in Java library. Bottom line. 1,000 years of mathematical formulas at your fingertips. Φ (z) z ( x ) = 1 2 e x 2 / 2 Taylor series 12 Java function for Φ (z) public class Gaussian { public static double phi ( double x ) // as before public static double Phi ( double z ) { if ( z < - 8.0 ) return 0.0 ; if ( z > 8.0 ) return 1.0 ; double sum = 0.0 , term = z ; for ( int i = 3 ; sum + term != sum ; i += 2 ) { sum = sum + term ; term = term * z * z / i ; } return 0.5 + sum * phi ( z ); } public static double Phi ( double z , double mu , double sigma ) { return Phi
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/21/2009 for the course COMPUTER computer 1 taught by Professor Abedauthman during the Fall '08 term at Aarhus Universitet.

Page1 / 11

06_functions - 2.1 Functions 2.1 Functions x y z f f (x, y,...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online