ECE 242 Spring 2003 Data Structures in Java - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

ECE 242 Spring 2003 Data Structures in Java

Description:

The function may take some input parameters on which it computes ... myscore is the parameter. Return Value. mygrade = GetGrade( myscore ) ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 21
Provided by: jxia
Category:

less

Transcript and Presenter's Notes

Title: ECE 242 Spring 2003 Data Structures in Java


1
ECE 242 Spring 2003Data Structures in Java
  • http//rio.ecs.umass.edu/ece242
  • Functions and Parameters
  • Prof. Lixin Gao

2
Todays Topics
  • Function or Method
  • Parameters

3
The 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?

4
Functions
  • 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

5
Common 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()

6
Defining 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
7
Function 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
8
Calling 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)

9
void
  • 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)
10
Function 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
11
Function 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 )
12
Parameter 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)

13
Control 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
14
Multiple 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
15
GetGrade 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

16
GetGrade Example
  • Calling Function, example
  • int myscore 88
  • char mygrade
  • mygrade GetGrade( myscore )
  • Complete Example QueryMyGrade.java

17
Organizing 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

18
QueryMyGrade.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

19
QueryMyGrade.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)

20
GetGrade Parameters
  • Function parameter
  • mygrade GetGrade( myscore )
  • myscore is the parameter
  • Return Value
  • mygrade GetGrade( myscore )
  • mygrade is the return value
Write a Comment
User Comments (0)
About PowerShow.com