/** * From WeTeach_CS Pseudocode Workbook July 2024 Pages 21-23 - J Newland * Copyright WeTeach_CS 2024. All Rights Reserved. * Note: comments are Java-like rather than Pythonic * * Methods, or functions, or procedures, are actions programs can take. * In general, think of methods as verbs. * * Best practice: name methods based on what they do! * * Why would we want methods or functions or procedures? */ /** * Void methods don't return anything so output happens by printing usually. * * Method first line is called the signature or header. * * If nothing is meant to be returned, the first word of the signature is void * or procedure. Here is the pattern: void verblikeNameHere(any inputs here) algorithm for the method here... end verblikeNameHere */ procedure setName(String newName) name <- newName end setName void helloWorld() // Note the use of camelCase print ("Hello, World!") end helloWorld /** * Return methods send data back to where the method was called. * Java and most languages return just one thing. * Python can return multiple things. * * The signature is a kind of contract: if the user of the method gives the right * kind of input, here is what output this method will give. * * Make methods as short as you can get away with. Break problems into parts. * * Here is the pattern: return-type-here verbyNameHere(inputs and their data types go here) algorithm goes here. return data-to-be-returned //return data and end NOW! end verbyNameHere */ // Precondition: the variable name will exist and have data String getName() return name // When return is called, the method is DONE end getName // Consumes: 2 ints & Returns: sum as an int (contract) int addBoth(int x, int y) // Pre-condition: x and y are ints and exist int result <- x + y return result end addBoth /** * Actual vs formal parameters: * The parameter in the method signature * (or contract) is called formal because * it only exists WHEN THE METHOD IS RUNNING * * The parameter "passed" to the method when * we call the method is called actual because * that's the real data being sent as input (consumed). * * In pseudocode, we assume parameters are passed by value. * So a copy of the actual parameters is placed in the formal parameter. * * If you want to indicate pass by reference, you have to do it explicitly. */ // Defining our methods int doubleIt(int num) // num is formal parameter int result <- num*2 return result // Should return num doubled end doubleIt // post condition: will return num doubled // Precondition: name is an existing String void changeNameToGeorge(pass-by-reference String name) name <- "George" // What actual impact does this have? end changeNameToGeorge int doubleIt2(int n) // passed by value by default int result <- n*2 n <- -1 // What actual impact does this have? return result end doubleIt2 // Using our methods int result <- doubleIt(16) // 16 is actual parameter print (result) // should be? String name <- "Chaim" changeNameToGeorge(name) print (name) // should be? int price <- 114 print(doubleIt2(price)) // should be? print(price) // Is price changed by doubleIt2? // Examples for practice questions. void stuff1(char a) // Note that the parameter a is formal if(a == 'A' OR a == 'a') print "Great job!" else if(a == 'B' OR a == 'b') print "Good work." else if(a == 'C' OR a == 'c') print "You can do better." else print "Try harder!" end stuff1 int stuff2(int x) if(x % 2 == 0) return x*2 else return x*3 end stuff2 /** * Practice Set 6 – Methods * Given the method definitions above, show the output for each method call below. * The symbol -> reads as "should yield" */ // 1. // Note that the parameter 'A' is actual stuff1('A') -> // 2. stuff1('F') -> // 3. (Why do we have to print what stuff2 returns?) print stuff2(14) -> // 4. print stuff2(15) -> // 5. Write the method call to produce the output: You can do better. // 6. Write the method call to produce the output: Good work. // 7. Write the method call to produce the output: 20 // 8. Write the method call to produce the output: 51 // 9. Complete the procedure given the following header to print the phrase // “Hello, George” if the call to the procedure is greet(“George”) procedure greet (String name) // Note the curly braces instead of just spaces! { }end greet // 10. Complete the method given the following header to return “dog” if // the value of the parameter is negative, “cat” otherwise. String bean(int num) { }end bean //Sample call results: // print bean(34) -> cat // print bean(-3) -> dog // 11. Write the output of the call: print bean(0) -> // Python examples: https://trinket.io/python3/7faa3d8ef9 // Java examples: https://trinket.io/java/f038d2c927 // Created by Jimmy Newland - newton@jayfox.net - 2024/08/02