1 / 23

Introduction to Functions

Introduction to Functions. Chapter 4.5. Delegating Responsibility. In the programs we have written so far, all the work happens inside main . Main is a function – a block of code that performs some task.

nicole
Download Presentation

Introduction to Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Functions Chapter 4.5

  2. Delegating Responsibility • In the programs we have written so far, all the work happens inside main. • Main is a function – a block of code that performs some task. • There may be some tasks that main could hand off to someone else – another function that you define.

  3. What’s going on?What is the output?Who are bob and fred? Try this: publicclass Blah { publicstaticvoid main(String[] args) { int x = 5, y = 10, z; z=function(x,y); System.out.println("z:"+z); z = function(y,x); }//end main publicstaticint function(int bob, int fred){ System.out.println("bob:"+bob); System.out.println("fred:"+fred); return (bob+fred); } }

  4. An Imaginary Company • Mr. Main is the boss of Mr. Main’s Imaginary Manufacturers • At the end of the month, he has to pay all of his employees. Mr. Main

  5. An Imaginary Company • In order to pay his employees, he performs the following actions: • Ask each employee how many hours he worked. • Ask each employee his hourly wage. • Calculate the amount of overtime hours ( more than 40) • Calculate base pay. • Calculate overtime pay. • Deliver money. Mr. Main

  6. importTerminalIO.*;publicclassCompany{publicstaticvoidmain(String[]args){ KeyboardReaderreader=newKeyboardReader();doublehours,othours,rate,paycheck; hours=reader.readDouble ("How many hours did you work?"); rate=reader.readDouble("What is your wage?"); if(hours>40){//there is overtime othours=hours-40;//calc ot hours hours=40; //40 regular hours }else //no overtimeothours=0; paycheck=hours*rate+othours*rate*1.5; System.out.println ("Your check is for $"+paycheck); }}

  7. An Imaginary Company • Mr. Main has decided that he is overworked. • As a result, he hires an assistant named Pay Roll. Mr. Main Pay Roll

  8. Delegating the Work:The New Job Duties • Ask each employee how many hours he worked. • Ask each employee his hourly wage. • Deliver info to Pay Roll. • Report paycheck amount to employee • Calculate the amount of overtime hours ( more than 40) • Calculate base pay. • Calculate overtime pay. • Deduct taxes. • Report the paycheck amount to Mr. Main Pay Roll Mr. Main

  9. Job Demonstration Mr. Main Pay Roll

  10. importTerminalIO.*;public class Company2 { public static void main(String[] args) { KeyboardReader reader = new KeyboardReader(); double hours, othours, rate, paycheck; hours = reader.readDouble("How many hours"); rate = reader.readDouble("What is your wage?"); paycheck = payRoll(hours, rate); System.out.println("Your check is $" + paycheck); }public static double payRoll(double hr, double rt){ double othr= 0; double check = 0; if(hr > 40){ othr = hr - 40; hr = 40; } check = hr * rt + othr * (rt*1.5); return check; }} Hey,How much is a check for these hours at this wage? The check is for this amount. Mr. Main Pay Roll

  11. hours rate paycheck importTerminalIO.*;public class Company2 { public static void main(String[] args) { double hours, rate, paycheck; hours = 45; rate = 5.0; paycheck = payRoll(hours, rate); System.out.println("Your check is $" + paycheck); }public static double payRoll(double hr, double rt){ double othr= 0; double check = 0; if(hr > 40){ othr = hr - 40; hr = 40; } check = hr * rt + othr * (rt*1.5); return check; }} 45 5 zzz… Pay Roll

  12. hours rate paycheck importTerminalIO.*;public class Company2 { public static void main(String[] args) { double hours, rate, paycheck; hours = 45; rate = 5.0; paycheck = payRoll(hours, rate); System.out.println("Your check is $" + paycheck); }public static double payRoll(double hr, double rt){ double othr= 0; double check = 0; if(hr > 40){ othr = hr - 40; hr = 40; } check = hr * rt + othr * (rt*1.5); return check; }} Get to work!!! 45 5 45 5 hr 45 zzz… Pay Roll rt 5

  13. hours rate paycheck importTerminalIO.*;public class Company2 { public static void main(String[] args) { double hours, rate, paycheck; hours = 45; rate = 5.0; paycheck = payRoll(hours, rate); System.out.println("Your check is $" + paycheck); }public static double payRoll(double hr, double rt){ double othr= 0; double check = 0; if(hr > 40){ othr = hr - 40; hr = 40; } check = hr * rt + othr * (rt*1.5); return check; }} zzz… 45 5 hr othr 237.5 40 45 237.5 Pay Roll rt check 5 5 0 0 237.5

  14. hours paycheck rate importTerminalIO.*;public class Company2 { public static void main(String[] args) { double hours, rate, paycheck; hours = 45; rate = 5.0; paycheck = payRoll(hours, rate); System.out.println("Your check is $" + paycheck); }public static double payRoll(double hr, double rt){ double othr= 0; double check = 0; if(hr > 40){ othr = hr - 40; hr = 40; } check = hr * rt + othr * (rt*1.5); return check; }} Your check is $237.5 45 5 237.5 othr hr 45 zzzz.. 237.5 Pay Roll rt check 5 5 237.5

  15. Function Name:this is how main “calls” the method.(should start with lowercase character) Parameters:This shows you what information that the function will need to do the job.Think of these as placeholders. hr and rt will be nicknames for the numbers that main gives the function. Function Header:(aka Method Signature)Tells you all you need to know about how to use the function Visibility Modifier:public or private…more on this later. Return Type:what data type will the function “answer” with?(ex: int, double, String, void) Return Statement:This statement gives the answer back to the main function. The data type MUST match the one specified in the header. Dissecting Pay Roll public static double payRoll(double hr, double rt){ double othr= 0; double check = 0; if(hr > 40){ othr = hr - 40; hr = 40; } check = hr * rt + othr * (rt*1.5); return check; }

  16. Calling the Function • When you run your program, main does it’s thing. • Other functions sit and do nothing…until they are called. • You call a function by using it’s name followed by a list of parameters.

  17. Example:Calling on Pay Roll • Pay Roll is a lazy employee. She does ABSOLUTELY NOTHING until Mr. Main tells her to. • Her function header shows Mr. Main what information she will need to do her job. • When he wants her to work, he just calls her name followed by the information that she needs. Pay Roll

  18. The function is expecting 2 pieces of information: 2 doubles. Notice only the variable namesare given – the function already knows they are doubles. Using the function’s name tells the function that it is time to get to work. Some number was being stored in hours. payRoll makes a clone of that number and nicknames the clone hr. Example:Calling on Pay Roll Remember the function header looked like this: public static double payRoll(double hr, double rt) Therefore main would call the function like so: paycheck = payRoll(hours, rate); Main waits for payRoll to get done.When payRoll is done, it answers by magically transforming into a number. The number is then stored in paycheck.

  19. Parameters Earlier we called payRoll like so. paycheck = payRoll(hours, rate); This worked because hours & rate are both variables of type double. However, any doubles would do; they don’t even have to be stored in variables. paycheck = payRoll(51.5, 5.25);

  20. How Functions Work:A Demonstration • Two volunteers, 5 styrofoam cups, and some post-its should clear things up! • One person represents main. • One person represents payRoll. • We label the cups as follows:paycheck, hours, rate, hr, rt • The numbers on the post-its are the values to be stored in variables.

  21. Try this… publicclass Blah2 { publicstaticvoid main(String[] args) { String s1 = “You"; String s2 = "poo"; System.out.println("Main"); System.out.println(“s1=“+s1+” s2=“+s2); }//end main publicstatic String myFun(String nm, String obj){ System.out.println("Hey...I'm in a function!!! WOOHOO"); System.out.println("nm="+nm); System.out.println("obj="+obj); return nm+" smell like "+obj; }//end myFun } Add a function call here. Now insert this line into myFun...nm+=“blah”; Now change the order of the parameters in your function call.

  22. paycheck = payRoll(hours, rate); Is VERY different from paycheck = payRoll(rate, hours); Parameters:Order Matters • Unfortunately for Mr. Main, it’s hard to find good help. • Pay Roll isn’t so bright; you HAVE to give her the information in the order that she expects it or she gets confused.

  23. You Try… • Make a new class • Ask for a base and an exponent • Make a function to exponentiate using a while loop • Print the results

More Related