1 / 31

Implementing Functions from a Detailed Design Quick Tips

Learn how to implement functions from a detailed design. Understand the concept of inputs, outputs, and modifying variables.

louisc
Download Presentation

Implementing Functions from a Detailed Design Quick Tips

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. Implementing Functionsfrom a Detailed DesignQuick Tips

  2. General Idea A function is segment of code that: - gets inputs from some source - performs some operation (algorithm) to transform the inputs into results - delivers the results to some destination as output

  3. General Idea Graphically, think of something like a Sausage Grinder: usually, several things go “in” and one thing comes “out” (though that’s not always the case)

  4. Input Input is data “coming in” to the function. Sources include: - Data passed in from another function - Data entered by the user (human interaction) - Data read from a file

  5. Output Output is data “going out” of the function. Destinations include: - Data passed to (back to) another function - Data delivered to the user (printed) - Data written to a file

  6. Modify Modify (also called Input/Output) is data that is both "coming in" and "going out". Source: the invoking function Destination: the invoking function

  7. Important Design Philosophy “That’s not MY job!!!” A function should do a single task. The task of some functions is to interact with the user: ask for input and print output. But some functions do NOT interact with the user: they get their input from elsewhere, and deliver their output elsewhere.

  8. Important Design Philosophy “That’s not MY job!!!” Think about workers in a fast food restaurant: Some interact with customers (take and deliver orders), others do not: cooks for instance. One employee takes the order, and “passes” that info to the cook. The cook prepares the food and “passes” it back to the first employee, who then gives it to the customer.

  9. Important Design Philosophy “That’s not MY job!!!” Functions are designed to work in the same way: each does a specific job, and not another job; and some jobs don’t involve interacting with the customer (user). The point: not every function will use cin/cout!

  10. Detailed Designs Detailed Design Documents are very “technical” in nature, but are still written in a human language. This language must be interpreted and translated into actual C++ code. One of the easiest ways to do this is to look for the use of key phrases and words that indicate the specification of the code.

  11. Detailed Designs: Inputs Input Arguments: Look for phrases like: - “function is given” - “function is passed” - “function takes” Data that “is given” indicates use of PBV Formal Variables.

  12. Detailed Designs: Inputs Input Arguments: Examples “Write a function that is given two integers...” Interpretation: void fun(int a, int b) “Write a function that takes a dollar amount...” Interpretation: void fun(float dollarAmt)

  13. Detailed Designs: Inputs Input Arguments: Pitfall Here is where beginning students often make a mistake: they correctly determine that a value is an Input Argument, but when they write the function, they still “ask the user”. Remember that Input Arguments already have a value...assigned by the invoking function. So there is no need to “ask the user” again

  14. Detailed Designs: Inputs Input Arguments: Pitfall “Write a function that is given a subtotal and calculates and returns the tax. The tax rate is 6%” float calcTax(float subtotal) { cout << ”Enter subtotal: ”; cin >> subtotal; float tax = subtotal * 0.06; return tax; }

  15. Detailed Designs: Inputs Input Arguments: Pitfall The cout/cin are not needed, because subtotal = value_of_Actual_Arg; is executed when the function is invoked. ... AND: it is somebody else’s job to “ask”...my job is to calculate tax!

  16. Detailed Designs: Inputs User input: Look for phrases like: “asks the user” “gets from the user” “prompts the user” These are interpreted as a cin/cout pair.

  17. Detailed Designs: Inputs User input: Example “Write a function that asks the user to enter their age...” Interpretation: void fun() { int age; cout << ”Enter your age: ”; cin >> age; }

  18. Detailed Designs: Outputs Output back to the invoking function: Most functions send output back to the function which invoked them. Usually there is only one such output, but sometimes there are more, and sometimes there is none. Look for the phrase “... returns ...” or “... passes back” in the description.

  19. Detailed Designs: Outputs Output back to the invoking function: When there is no value returned: - the return type is void When there is one value returned: - the return type is the type of the value returned. - use the return command to return the value. Where there is more than one value returned: - use a void return type - use a PBR argument for each value “returned”. - do not use the return command; instead: - set the value of each PBR argument to the “returned” value.

  20. Detailed Designs: Outputs Output back to the invoking function: no value “Write a function that prints hello on the screen.” Note, there is no mention of “returns”, so use void: void sayHi() { cout << ”Hello”; }

  21. Detailed Designs: Outputs Output back to the invoking function: 1 value “Write a function that asks the user to enter their age and returns the age entered.” intaskAge() { int age; cout << ”Enter your age: ”; cin >> age; // age is int, so return age; // return type is int }

  22. Detailed Designs: Outputs Output back to the invoking function: 2 values “Write a function that asks the user to enter their name and age and passes these back.” voidaskNameAndAge(string & n, int& a) { cout << ”Enter your name: ”; cin >> n; cout << ”Enter your age: ”; cin >> a; }

  23. Detailed Designs: Outputs User Output: Look for the words: - prints - displays - shows These are interpreted as cout’s

  24. Detailed Designs: Outputs User Output: Example "Write a function that is given a name and greets the user printing Hello followed by the name" void greet(string name) { cout << "Hello " << name; }

  25. Detailed Designs: In/Out Args Input/Output Argument: An argument used to both: - input data from the invoking function - output data back to the invoking function by modifying the data.

  26. Detailed Designs: In/Out Args Input/Output Argument: Look for the words "modifies", "changes", etc. in reference to something that "is given". Since an I/O Argument must send data back, they are always PBR

  27. Detailed Designs: In/Out Args Example 1: Write a function called doubleIt that is given an integer and modifies it by multiplying it by two. void doubleIt(int& n) { n = n * 2; }

  28. Detailed Designs: In/Out Args Example 2: Write a function called forceInRange that modifies an integer such that its value is between a given lower and upper ends of an range, such that: if the integer > upper, set it to upper if the integer < lower, set it to lower.

  29. Detailed Designs: In/Out Args Example 2: void forceInRange(int&n, int low, int hi) { if (n > hi) n = hi; if (n < low) n = low; }

  30. Detailed Designs: In/Out Args Example 3: Write a function called changeName that modifies a name by asking the user to enter a new value. Print the old name before the prompt. void changeName(string & name) { cout << "Old name = " << name; cout << "Enter new name: "; cin >> name; }

  31. Detailed Designs: In/Out Args Example 4: Write a function called order2 that modifies two integers, when needed, such that the first is smaller than the second. void order2(int& a, int& b) { int t; if (a > b) { // swap a and b t = a; a = b; b = t; } }

More Related