Download presentation
Presentation is loading. Please wait.
Published byIsabel Hopkins Modified over 8 years ago
1
Methods: functions & procedures
2
Top-down programming Divide-and-conquer technique Divide-and-conquer technique Problem is broken down into a series of smaller problems which are solved. Problem is broken down into a series of smaller problems which are solved.
3
Lemma (from http://mathworld.wolfram.com/Lemma.html) http://mathworld.wolfram.com/Lemma.html lemma = a short theorem used in proving a larger theorem The late mathematician P. Erdos has often been associated with the observation that “a mathematician is a machine for converting coffee into theorems.” The late mathematician P. Erdos has often been associated with the observation that “a mathematician is a machine for converting coffee into theorems.” However, this characterization appears to be due to his friend, Alfred Rényi. However, this characterization appears to be due to his friend, Alfred Rényi. This thought was developed further by Erdos' friend and Hungarian mathematician Paul Turán, who suggested that weak coffee was suitable “only for lemmas.” This thought was developed further by Erdos' friend and Hungarian mathematician Paul Turán, who suggested that weak coffee was suitable “only for lemmas.”
4
Program composition mechanisms 1. Structured statements if, for, switch, while if, for, switch, while
5
Program composition mechanisms 2. Data structures Simple (atomic) Simple (atomic) boolean, int, double, float, char boolean, int, double, float, char Objects Objects Scanner, System, Math, String Scanner, System, Math, String Text files Text files
6
Program composition mechanisms 3. Methods Procedures Procedures Do work but don’t return anything. Do work but don’t return anything. Functions Functions Do work and return some result. Do work and return some result. AKA routines, subroutines, etc. AKA routines, subroutines, etc.
7
Problem Say we wish to write a function of our own. Say we wish to write a function of our own. It should return true if a given character is lower case, and false otherwise. It should return true if a given character is lower case, and false otherwise.
8
How would we do this in one of our programs now (w/out a function)? We could use an ‘if’ or a ‘switch.’ We could use an ‘if’ or a ‘switch.’
9
How would we do this in one of our programs now (w/out a function)? We could use an ‘if.’ We could use an ‘if.’ boolean isLC = false; if (ch=='a' || ch=='b' || … || ch=='z') isLC = true;
10
Why use a function at all? W/out function we would have to cut/copy/paste the code to everywhere we wish to use the code. If the variable names at that point in your code change (and they probably will), then you will have to change the names after the paste. W/out function we would have to cut/copy/paste the code to everywhere we wish to use the code. If the variable names at that point in your code change (and they probably will), then you will have to change the names after the paste. If the function is long, your programs will dramatically increase in size. If the function is long, your programs will dramatically increase in size. If we make a mistake, we’ll have to correct the mistake in many different places. If we make a mistake, we’ll have to correct the mistake in many different places. Just imagine if instead of pressing the sine button on our calculator, we have to press 1000 buttons and do the actual calculations! Just imagine if instead of pressing the sine button on our calculator, we have to press 1000 buttons and do the actual calculations!
11
Steps to define your own function. 1. Decide on a name for your function. 2. Determine what arguments your function needs to do its job. 3. Determine the return type of your function. 4. Code your function.
12
Steps to define your own function. 1. Decide on a name for your function. 2. Determine what arguments your function needs to do its job. 3. Determine the return type of your function. 4. Code your function. Before we code our own function, how would we apply these steps to create something familiar like sine?
13
Back to our original problem Say we wish to write a function of our own. Say we wish to write a function of our own. It should return true if a given character is lower case, and false otherwise. It should return true if a given character is lower case, and false otherwise.
14
Steps to define your own function. Step 1. Decide on a name. isLowerCase
15
Steps to define your own function. Step 2. Determine what arguments your functions needs to do its job. What does isLowerCase need to do its job? What does isLowerCase need to do its job? 1. One character to check. ch ch
16
After steps 1 and 2. isLowerCase ( char ch ) { …} Step 3. What type of thing does our function return?
17
After steps 1, 2, and 3. static boolean isLowerCase ( char ch ) { …} Now all we have to do is fill in the body of our functions.
18
Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) {…} Now all we have to do is fill in the body of our functions. Recall that a l.c. character is one that is in the interval [‘a’.. ‘z’].
19
Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //use a switch } Now all we have to do is fill in the body of our functions. Recall that a l.c. character is one that is in the interval [‘a’.. ‘z’].
20
Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //use a switch boolean isLC = false; switch (ch) { case 'a' : case 'b' : … case 'z' : isLC = true; break;} return isLC; }
21
Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //use a switch switch (ch) { case 'a' : case 'b' : … case 'z' : return true; break; default : return false; break;}}
22
Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //use an if } Now all we have to do is fill in the body of our functions. Recall that a l.c. character is one that is in the interval [‘a’.. ‘z’].
23
Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //use an if if (ch=='a' || ch=='b' || … || ch=='z') return true; return false; } Any other way to use an if?
24
Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //use an if if (ch 'z') return false; return true; }
25
Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //just use a return }
26
Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) {//minimal return ('a'<=ch && ch<='z'); }
27
Using our function if (isLowerCase('c')) … if (isLowerCase(c1)) … if (isLowerCase(c1) || isLowerCase(c2)) …
28
Architecture of a function
29
Definition of function static boolean isUpperCase (char p ) { if ('A'<=p && p<='Z')return true; return false; }
30
Function heading or signature (or prototype) static boolean isUpperCase (char p ) { if ('A'<=p && p<='Z')return true; return false; }
31
Function body static boolean isUpperCase (char p ) { if ('A'<=p && p<='Z')return true; return false; }
32
Function parts static boolean isUpperCase (char p ) { if ('A'<=p && p<='Z')return true; return false; } Function type Function name Formal parameters (function parameters)
33
Formal parameters static boolean isUpperCase ( char p ) { if ('A'<=p && p<='Z')return true; return false; } Formal parameters 0 or more of them 0 or more of them Local variables Local variables Hold values of actual parameters Hold values of actual parameters
34
What happens when a function is called/used/invoked? 1. Formal parameter(s) is created as a new variable. New variable is initialized to actual parameter value. 2. Execute function body. Return function value. 3. Program continues after function designator (invocation).
35
More functions
36
Say we wish to define a function that calculates the square of a number. 1. What should we call it? 2. What does it need (formal parameters) to do the calculation? 3. What types of numbers does it need? 4. How will we give back the result? Does it give back a result? Does it give back a result? 5. What is the type of the result (if any)? We use the keyword void to indicate that we will not return anything. We use the keyword void to indicate that we will not return anything.
37
Say we wish to define a function that calculates the square of a number. 1. What should we call it?
38
Say we wish to define a function that calculates the square of a number. 1. What should we call it? square
39
Say we wish to define a function that calculates the square of a number. 2. What does it need (formal parameters) to do the calculation?
40
Say we wish to define a function that calculates the square of a number. 2. What does it need (formal parameters) to do the calculation? A number to square.
41
Say we wish to define a function that calculates the square of a number. 3. What types of numbers does it need?
42
Say we wish to define a function that calculates the square of a number. 3. What types of numbers does it need? double
43
Say we wish to define a function that calculates the square of a number. After steps 1 to 3, we have: square ( double x )
44
Say we wish to define a function that calculates the square of a number. 4. How will we give back the result? Does it give back a result? Does it give back a result?
45
Say we wish to define a function that calculates the square of a number. 4. How will we give back the result? Does it give back a result? Does it give back a result? Yes. Yes.
46
Say we wish to define a function that calculates the square of a number. 5. What is the type of the result (if any)? We use the keyword void to indicate that we will not return anything. We use the keyword void to indicate that we will not return anything.
47
Say we wish to define a function that calculates the square of a number. 5. What is the type of the result (if any)? We use the keyword void to indicate that we will not return anything. We use the keyword void to indicate that we will not return anything. Double Double So now we have: static double square ( double x ) { …}
48
Say we wish to define a function that calculates the square of a number. static double square ( double x ) { //let’s define the body of the function. }
49
Say we wish to define a function that calculates the square of a number. static double square ( double x ) { double result = x*x; return result; } Did you write this in your notes?
50
Areas from geometry A square = length * length A rectangle = width * height A triangle = ½ * base * height A parallelogram = base * height Can you define functions that perform each of these calculations?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.