Title: ECE 242 Spring 2003 Data Structures in Java
1ECE 242 Spring 2003Data Structures in Java
- http//rio.ecs.umass.edu/ece242
- Functions and Parameters
- Prof. Lixin Gao
2Todays Topics
- Function or Method
- Parameters
3The Problem
- Suppose we have a computation or operation we
need to perform over and over again within a
program - Or, suppose we have a computation that many
programs need to perform - How can we write those operations just once, but
allow them to be easily used whenever needed
within a program or from many programs?
4Functions
- A function is a named code sequence that can be
executed by giving its name - The function may take some input parameters on
which it computes - The function may compute and return a value
5Common Functions
- We have already seen and used several functions
- class HelloWorld
- public static void main(String args)
- lt--- function definition for main()
- System.out.println("Hello, World!")
- lt--- function calls to println()
-
6Defining a function
- A function is defined by giving its name and
listing the code it is to execute
Function name
double cube(double n) return (nnn)
Function body
7Function Type and Value
- A function can return a value
- The function is declared to have the type of its
return value
function type
parameter type
double cube(double n) return (nnn)
function parameter
return statement
return value
8Calling a function
- To use the function, it is called or invoked from
within a program or another function
(CallCubeFunction.java)
class CallCubeFunction static double
cube(double n) return (nnn)
public static void main(String args)
double length 3.0 double
itsCube itsCube cube(length)
System.out.println("Its cube is " itsCube)
9void
- The type void is used when a function returns no
result or takes no parameters - Note A function that returns no result does not
require a return statement.
void prompt(void) System.out.println(En
ter next input)
10Function Control Flow
void prompt(void) System.out.println(En
ter the next integer)
main() prompt() prompt()
prompt
public static void main(String args)
prompt() prompt()
return (0)
prompt
11Function Parameters
- The calling program may need to pass the function
several values on which to operate - Such values are function input parameters
- The function specifies its inputs as formal
parameters in the function declaration
dummy, formal input parameter
double cube(double n) return( nnn )
12Parameter Parsing
- The calling program provides an actual parameter
on the call. - Within the function, the value of the actual
parameter is substituted for the formal parameter.
Parameter parsing
double length 3.0 double itsCube itsCube
mybox.cube(length)
double cube(double n) return (nnn)
13Control and Data Flow
- When a function is called, control transfer to
function body, the function executes, then
control returns to the point of call.
public static void main(String args) x
6.0 y cube(x/2.0) z 3.4cube(4.0)
double cube (double n) return (nnn)
3.0
27.0
4.0
64.0
14Multiple parameters
- A function can take multiple parameters
- Parameters must match in number, order and type
int m, n double gpt, gpa gpt 3.0 3.3
3.9 gpa avgFunc(gpt, 3)
double avgFunc(double total, int count)
return (total/(double)count)
actual parameters
formal parameters
15GetGrade Example
- Defining Function, example
- char GetGrade( int score )
-
- char grade
- if( score gt 85 ) gradeA
- else if( score gt 75 ) grade B
- else if( score gt 65 ) grade C
- else if( score gt 60 ) grade D
- else grade E
- return grade
16GetGrade Example
- Calling Function, example
- int myscore 88
- char mygrade
- mygrade GetGrade( myscore )
- Complete Example QueryMyGrade.java
17Organizing Functions in a Class
- class className
- int variable1 // instance variable
- static int function1()
-
- // end of function1
- static int function2(float f)
-
- // end of function2
- public static void main (String args)
-
- // end of class
18QueryMyGrade.java (1)
- class QueryMyGrade
- static char GetGrade( int score )
- char grade
- if( score gt 85 ) grade'A'
- else if( score gt 75 ) grade 'B'
- else if( score gt 65 ) grade 'C'
- else if( score gt 60 ) grade 'D'
- else grade 'E'
- return grade
-
19QueryMyGrade.java (2)
- public static void main (String args)
- EasyIn easy new EasyIn()
- int thescore
- char thegrade
- System.out.print("Input your score")
- thescore easy.readInt()
- thegrade GetGrade( thescore )
- System.out.println("Your grade is "
thegrade) -
20GetGrade Parameters
- Function parameter
- mygrade GetGrade( myscore )
- myscore is the parameter
- Return Value
- mygrade GetGrade( myscore )
- mygrade is the return value