Warm up!!: Warm up!! public class MoreFunctionFun { public static void main(String[] args) { int a=2, b=3, c=4; c = fun(a, b); b = fun(a, c); System.out.println('a='+a); System.out.println('b='+b); System.out.println('c='+c); }
public static int fun(int n1, int n2){ n1++; n2 += n1; return n1*n2; } }
More Fun With Functions!: More Fun With Functions! AP Computer Science
Lesson 1: functions are dumb: Lesson 1: functions are dumb Functions only know what main 'tells' them.
Main passes information to a function through parameters.
The function ONLY knows what the parameters tell it.
Try this…: Try this… public static void main(String[] args){ final double TAX_RATE = 0.06; KeyboardReader reader = new KeyboardReader(); double subtotal, total;
subtotal = reader.readDouble('Enter Purchase Amount:');
total = subtotal + tax(subtotal); }
public static double tax(double amount){ return amount * TAX_RATE; }
Slide5: public static void main(String[] args){ final double TAX_RATE = 0.06; KeyboardReader reader = new KeyboardReader(); double subtotal, total;
subtotal = reader.readDouble('Enter Purchase Amount:');
total = subtotal + tax(subtotal, TAX_RATE); }
public static double tax(double amount, double tr){ return amount * tr; }
Lesson 2: functions are really dumb: Lesson 2: functions are really dumb Functions assume that they have been passed parameters in a certain order.
This order is defined by the function header.
Even though variable names may make it seem sensible, the function ONLY cares about the order.
Slide7: public static void main(String[] args){ int leg1=3, leg2=4, hyp=5; if(pythag(hyp, leg1, leg2)) System.out.println('Right Triangle!!'); else System.out.println('NOT right triangle');
if(pythag(leg1, leg2, hyp)) System.out.println('Right Triangle!!'); else System.out.println('NOT right triangle'); }
public static boolean pythag(int a, int b, int c){ return ((a*a + b*b)==c*c); } Output: NOT right triangle Right Triangle!!
Lesson 3: main doesn’t trust functions: Lesson 3: main doesn’t trust functions When a function is called, it only gets a copy of the parameters’ values.
The function CANNOT change any of main’s variables.
The only change possible is if main assigns the function’s returned value into a variable.
Slide9: public static void main(String[] args){ int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b); }
public static int change(int n1, int n2){ n1 *= 100; n1 += n2; return (n2-1); }
Slide10: public static void main(String[] args){ int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b); }
public static int change(int n1, int n2){ n1 *= 100; n1 += n2; return (n2-1); } 5 a 15 b 25 c 5 5 15 15
Slide11: public static void main(String[] args){ int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b); }
public static int change(int n1, int n2){ n1 *= 100; n1 += n2; return (n2-1); } 5 a 15 b 25 c 5 n1 15 n2
Slide12: public static void main(String[] args){ int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b); }
public static int change(int n1, int n2){ n1 *= 100; n1 += n2; return (n2-1); } 5 a 15 b 25 c 5 5 15 15
Slide13: public static void main(String[] args){ int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b); }
public static int change(int n1, int n2){ n1 *= 100; n1 += n2; return (n2-1); } 5 a 15 b 25 c 5 n1 15 n2
Slide14: public static void main(String[] args){ int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b); }
public static int change(int n1, int n2){ n1 *= 100; n1 += n2; return (n2-1); } 5 a 14 b 25 c 5 5 14 14
Slide15: public static void main(String[] args){ int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b); }
public static int change(int n1, int n2){ n1 *= 100; n1 += n2; return (n2-1); } 5 a 14 b 25 c 5 n1 14 n2
Function Overloading:Same name, different parameters: Function Overloading: Same name, different parameters Every function has a name public static int area(int l, int w)
Your program can have 2 (or more) functions with the same name if they have a different number and/or type of parameters.
Slide17: public class Overloading { public static void main(String[] args) { System.out.print ('3x5 Rectangle has area: '); System.out.println(area(3,5)); System.out.print ('Circle w/radius 5 has area: '); }
public static double area(int l, int w){ return l*w; }
}//end class Now write an 'area' function for circles and call it from the main.
Slide18: public class Overloading { public static void main(String[] args) { System.out.print ('3x5 Rectangle has area: '); System.out.println(area(3,5)); System.out.print ('Circle w/radius 5 has area: '); System.out.println(area(5)); }
public static double area(int l, int w){ return l*w; } public static double area(int r){ return Math.PI*r*r; } }//end class Output: 3x5 Rectangle has area: 15.0 Circle w/radius 5 has area: 78.53981633974
Don’t Confuse the Computer!: Don’t Confuse the Computer! Overloaded functions MUST have a different number and/or type of parameters.
Having a different parameter name is not enough!! public static double area(int r){ return Math.PI*r*r; }
public static double area(int s){ return s*s; }
Preconditions & Postconditions: Preconditions andamp; Postconditions When you write functions, they should be accompanied by special comments:
Preconditions: What needs to be true when the function is called.
What do the parameters represent?
What values are legal for the parameters?
Postconditions: What will be true when the function returns.
What type of value is returned?
What does it represent?
Preconditions & Postconditions: Preconditions andamp; Postconditions These comments provide a guarantee:
If the preconditions are met, then the postconditions will be true.
If the preconditions are not met, then there is no guarantee the function will work.
Example:: Example: /* * Preconditions:l andamp; w are nonnegative integers * representing the length andamp; width of a rectangle * Postconditions:the area of the rectangle is returned */
public static double area(int l, int w){ return l*w; } /*
* Preconditions:r is a nonnegative integer * representing the radius of a circle * Postconditions:the area of the circle is returned */ public static double area(int r){ return Math.PI*r*r; }
Pizza per Inch:Some Practice: Pizza per Inch: Some Practice Now write a program that helps someone make pizza purchasing decisions.
The program should:
Ask the user for the price and diameter of a large pizza
Ask the user for the price and diameter of a medium pizza
Ask the user for the price and diameter of a small pizza
Use a function to calculate the unit price (price per square inch) for each.
Inform the user of the best deal.