Download presentation
Presentation is loading. Please wait.
Published byDoreen Miles Modified over 9 years ago
1
1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt Assg
2
Copyright 2014 by Janson Industries 2 Objectives ▀ Explain ■ What a function is ■ How to use and create functions ■ IPO Charts ▀ Show how to implement and use functions in Java
3
Copyright 2014 by Janson Industries 3 Function ▀ Per text: “Functions are methods/ modules that return a value” ▀ Just as with methods, the function header defines the function's ■ Name ■ Expected values (arguments) ▀ The function header also defines ■ The returned value type (Real, String) ▀ And the body must contain ■ A Return statement
4
Copyright 2014 by Janson Industries 4 Function ▀ In pseudocode, function starts with the word Function and ends with End Function Function String captureInput() Declare String data Display “Enter input” Input data Return data End Function
5
Copyright 2014 by Janson Industries 5 Function ▀ A function's returned value can be assigned to a variable ▀ Call a function just like a module: ■ Specify the function name followed by parenthesis ▀ Does captureInput accept any data? Module main() Declare String subtotal subtotal = captureInput() End Module
6
Copyright 2014 by Janson Industries 6 All Pseudocode Module main() Declare String subtotal subtotal = captureInput() End Module Function String captureInput() Declare String data Display “Enter input” Input data Return data End Function
7
Copyright 2014 by Janson Industries Function Calls Flowchart 7 Display “Enter input” subtotal = captureInput() main() End Declare String subtotal Input data captureInput() Return data Declare String data
8
Copyright 2014 by Janson Industries SFC The return value type Function name Specify Function The variable/value to return
9
Copyright 2014 by Janson Industries SFC Then add function statements
10
Copyright 2014 by Janson Industries SFC Finished function
11
Copyright 2014 by Janson Industries Raptor ▀ Doesn't support functions Finished function
12
Copyright 2014 by Janson Industries Raptor When run
13
Copyright 2014 by Janson Industries Java ▀ Java very similar to pseudocode ▀ What will be the result? The return value type The value/variable to return Function name
14
Copyright 2014 by Janson Industries 14 Prewritten Functions ▀ Supplied with programming language ▀ Example: System.out.println() the println function is invoked in a class called System ■ You can tell because () follows the name ▀ This is an example of a function that will accept a parameter ■ System.out.println("Stuff");
15
Copyright 2014 by Janson Industries 15 Why Create Functions ▀ Just as with methods, cuts down on duplicate code ▀ Make it easier for programmer ▀ For instance, System.out.println() is a lot to type ▀ Will create a class called MyUtils ■ Create a function called p(String output) ■ Will be able to specify MyUtils.p() instead of System.out.println()
16
Copyright 2014 by Janson Industries 16 Pre-Written functions ▀ MyUtils just acts as a repository of useful functions ■ So, no need for a main() public class MyUtils { public static int p(String output){ System.out.println(output); return 1; }
17
Copyright 2014 by Janson Industries 17 Pre-Written Functions ▀ Now any other class can use the function p() ▀ In any java class, just specify ■ ClassName.functionName(); public class FunctionCall2 { public static void main(String[] args) { MyUtils.p(“Howdy”); }
18
Copyright 2014 by Janson Industries 18 When FunctionCall2 is run, it invokes p in MyUtils Must compile MyUtils before FunctionCall2
19
Copyright 2014 by Janson Industries 19 Prewritten Functions ▀ Most languages come with many to perform common functions ■ Mathematical functions ♦ Sine, cosine, square root, etc. ■ Generate a random number ■ Convert between data types ■ Displaying (println) ■ Exiting ■ Security ♦ Userid/PW, cryptography
20
Copyright 2014 by Janson Industries 20 Prewritten Functions ▀ Advantage of prewritten functions ■ Don't have to create the function yourself ■ Don't have to understand how it works ♦ Called implementation hiding ■ You're sure they work
21
Copyright 2014 by Janson Industries 21 Prewritten Functions ▀ Some examples ■ random(#,#) – returns a number between the first (inclusive) and second number (inclusive) ♦ random(1,6) returns a number between 1 and 6 ■ pow(#,#) – raises the first number to the power of the second number ♦ pow(2,4) returns 16 ■ round(#.#) – returns an integer value for a real value ♦ round(3.49) returns 3 ♦ round(3.5) returns 4
22
Copyright 2014 by Janson Industries 22 Prewritten Functions ▀ Some examples ■ isInteger(string) – returns a Boolean value based on whether the string can be converted to an integer ♦ isInteger("6") returns a value of true ■ isReal(string) – returns a Boolean value based on whether the string can be converted to a Real ♦ isReal(“abc”) returns a value of false
23
Copyright 2014 by Janson Industries 23 Prewritten Functions ▀ Converting data types ▀ All data entered is String must convert to get numeric value ■ stringToInteger(string) – returns the integer value of the string ♦ stringToInteger(“6”) returns the number 6 ■ stringToReal(string)– returns the real value of the string ♦ stringToReal(“2.14”) returns the number 2.14
24
Copyright 2014 by Janson Industries 24 Prewritten Functions ▀ String functions ■ length(string) – returns the number of characters in a string ♦ length(“hello”) returns the number 5 ■ substring(string, 3, 5)– returns the characters from position 3 to 5 ♦ substring(“goodbye”, 3, 5) returns the string “db” ■ contains(string, string)– returns Boolean value of true or false based on if the first string contains the characters in the second string ♦ contains(“goodbye”, “oo”) returns the value true
25
Copyright 2014 by Janson Industries 25 Prewritten Functions Example ▀ Will enhance the captureInput function to convert the data into a numeric value Module main() Declare Real subtotal Display “Enter number” subtotal = captureInput() End Module
26
Copyright 2014 by Janson Industries 26 Prewritten Functions Example Function Real captureInput() Declare String data Declare Real numberInput Display “Enter a number” Input data While(!(isReal(data))) Display “Please enter a number” Input data EndWhile numberInput = stringToReal(data) Return numberInput End Function
27
Copyright 2014 by Janson Industries 27 Prewritten Functions Example
28
Copyright 2014 by Janson Industries 28 Prewritten Functions Java ▀ Are in classes like Math, Integer, Double, String ■ Math.pow(#,#) – raises the first number to the power of the second number ♦ Math.pow(2,4) returns 16 ■ Math.round(#.#) – returns an integer value for a real value ♦ Math.round(3.49) returns 3 ♦ Math.round(3.5) returns 4
29
Copyright 2014 by Janson Industries 29 Prewritten Functions Java ▀ Some examples ■ Math.random() – returns a number between 0 and 1(exclusive) ♦ To get an int in a range, must multiply the result by the max value & add one, then change to an int (truncate) (int) (Math.random()*6) +1 ■ Random class has nextInt(#) method that returns a number from a range 0(inclusive) to #(exclusive) ♦ Random.nextInt(7) Returns integer value of 0, 1, 2, 3, 4, 5, or 6
30
Copyright 2014 by Janson Industries 30 Prewritten Functions Java ▀ Converting data types ▀ All data entered is String must convert to get numeric value ■ Integer.valueOf("6") ■ Double.valueOf("2.14") ▀ There are no isReal or isInteger functions in java
31
Copyright 2014 by Janson Industries 31 Prewritten Functions Java ▀ String functions ■ Assuming: ♦ String testString = new String("Goodbye"); ■ testString.length() - returns 7 ■ testString.substring(3, 5) – returns the string “db” ■ testString.contains("oo") – returns Boolean value true ■ testString.contains("OO") – returns Boolean value false
32
Copyright 2014 by Janson Industries 32 Prewritten Functions Example ▀ How about creating a dice game ■ Ask user if they want to play ■ If yes, generate 2 random numbers between 1 and 6 to rep the user's and computer's dice roll ■ Print out numbers and message saying who won (who got the higher number) or if it was a tie ■ Ask the user if they want to play again
33
Copyright 2014 by Janson Industries 33 Prewritten Functions Example ▀ What's the algorithm? Dice game algorithm ▀ Then the XD is created
34
Copyright 2014 by Janson Industries 34 Prewritten Functions Example ▀ Then an additional requirement is added to create a function called getMessage ▀ getMessage will ■ Accept the two dice rolls ■ Generate the correct message ■ Return the message
35
Copyright 2014 by Janson Industries 35 Prewritten Functions Example ▀ Dice game pseudocode Dice game pseudocode ▀ Generate the SFC flowchart SFC flowchart
36
Copyright 2014 by Janson Industries 36 Dice Game Example: Raptor ▀ Raptor returns random number like java as ■ A non-Integer between 0 and 1 (exclusive) ▀ So must: ■ Multiply by 6 (max value) to get number between 0 and 5.99999 ■ Then add one to get 1 to 6.9999 ■ Then truncate (with floor command) to get 1 to 6
37
Copyright 2014 by Janson Industries 37 Dice Game Example
38
Copyright 2014 by Janson Industries 38 Dice Game Example
39
Copyright 2014 by Janson Industries 39 Dice Game Example When Run
40
Copyright 2014 by Janson Industries 40 Dice Game Example Java
41
Copyright 2014 by Janson Industries 41 Dice Game Example Java
42
Copyright 2014 by Janson Industries 42 Alternative Documentation ▀ IPO (Input Processing Output) Chart ■ Really a table with text (not a chart) ▀ Three columns ■ First column identifies input ■ Second column describes processing ■ Third column identifies output
43
Copyright 2014 by Janson Industries 43 IPO Example ▀ getTotalWithTax(String zip, Real total) ■ Calculates a Sales amount including tax based on the zip code where the transaction takes places ■ Reads file for the Sales tax percentage
44
Copyright 2014 by Janson Industries 44 IPO Example Pseudocode Function Real getTotalWithTax(String zip, Real total) Declare Real taxRate, useableTaxRate, totalWithTax taxRate = Read TaxFile for zip useableTaxRate = taxRate + 1 totalWithTax = total * useableTaxRate Return totalWithTax End Function
45
Copyright 2014 by Janson Industries 45 IPO Example ▀ getTotalWithTax(String zip, Real total) ▀ Notice that taxRate considered input even though not a function argument InputProcessingOutput zip: String total: Real taxRate: Real Real useableTaxRate, totalWithTax, totalWithTax Read taxRate from TaxFile for zip useableTaxRate = taxRate + 1 totalWithTax = total * useableTaxRate totalWithTax
46
Copyright 2014 by Janson Industries 46 Points to Remember ▀ Functions are methods/modules that return values ▀ Lots of prewritten functions that provide commonly used logic ▀ IPO Charts are an alternative design/documentation tool
47
Copyright 2014 by Janson Industries Assignments n Non-Graded u Chap 6 labs 6.1 - 6.3 n Graded u Chap 6 lab 6.4 47
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.