 HW9-Part1  Parameters at instantiation vs. settings after creation  Show ▪ clickCount inside of “for –loop”?   Quiz-5 Readings:  Go Post: Reading Assignments  A “real” C# program.

Download Report

Transcript  HW9-Part1  Parameters at instantiation vs. settings after creation  Show ▪ clickCount inside of “for –loop”?   Quiz-5 Readings:  Go Post: Reading Assignments  A “real” C# program.


HW9-Part1
 Parameters at instantiation vs. settings after
creation
 Show
▪ clickCount inside of “for –loop”?


Quiz-5
Readings:
 Go Post: Reading Assignments

A “real” C# program …
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
1
Functional Abstraction Reduces Complexity
Kelvin Sung
University of Washington, Bothell
(* Use/Modification with permission based on Larry Snyder’s CSE120 from Winter 2011)
© Lawrence Snyder 2004

Today – the two threads of class merge again
as we introduce functions in C# and use them
to build a pong game with bullseye!
 Function Syntax:
▪ Result Type, Parameters, and Return
 Defining a function: writing a function
 Calling a function: using a function
 Arguments to parameters

Two reasons for function:
 Abstraction + ReUse
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
3



Functions have been used in Lightbot 2.0: F1
Functions were in Assignment 03: F.turn( ) …
We’ve used functions, also known as
 procedures
 methods
 subroutines

in all of our C# code: ButtonAClicked()
Recall that functions have two parts:
 function definition … a statement of how it works
 function call … a request to have it performed
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
4
<return type> <name> ( <param list> ) {
<body>
}
as in
void moveBallBy(float x_dist, float y_dist) {
pongBall.CenterX += x_dist;
pongBall.CenterY += y_dist;
}
or
Color pink ( ) {
return new Color(255, 200, 200);
}
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
5


Functions that do something, but do not return
a value, have void as their <return type>
Functions that return a value must say its type
void moveBallBy(float x_dist, float y_dist) {
pongBall.CenterX += x_dist;
pongBall.CenterY += y_dist;
}
Color pink ( ) {
return new Color(255, 200, 200);
}
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
6


Parameters are the values used as input to
the function; parameters are not required,
but the parentheses are
The type of each parameter must be given
void moveBallBy(float x_dist, float y_dist) {
pongBall.CenterX += x_dist;
pongBall.CenterY += y_dist;
}
Color pink ( ) {
return new Color(255, 200, 200);
}
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
7


A function returns its value with the return
statement … the stuff following return is the
result
The function is done when it reaches return
void moveBallBy(float x_dist, float y_dist) {
pongBall.CenterX += x_dist;
pongBall.CenterY += y_dist;
}
Color pink ( ) {
return new Color(255, 200, 200);
}
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
8

Our function definitions are listed after
InitializeWorld() and UpdateWorld()
Function
Call
Function
Definition
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
9

Once defined, functions can be called
repeatedly … it’s the point of writing them!
Function
Call
Function
Call
Function
Definition
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
10


Notice that if the DEFINITION has n
parameters, the CALL needs n arguments
The parameters and arguments correspond
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
11


Notice that if the DEFINITION has n
parameters, the CALL needs n arguments
The parameters and arguments correspond
Inside of the function, the
parameter, e.g. xPos, is declared
and initialized to the
corresponding argument, e.g.
400. Then, the definition uses it,
e.g.
a.CenterX = 400;
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
12



Parameters are automatically declared (and
initialized) on a call, and remain in existence
as long as the function remains unfinished
When the function ends, the parameters
vanish, only to be recreated on the next call
It is wise to choose parameter names, e.g.
x-P-o-s that are meaningful to you
 I chose xPos as the x position of the bullseye
center
 Notice that I used that name a lot, and the
meaning to me remained the same
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
13

Makes your code more readable
 Show your thinking/Hide the details

Parameterized: Support re-use
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
14

Review What We Did
Bullseye as pong ball
Create
Reset
Move
Collide
Bunch of circles

The computation ONLY deals circles, but we
don’t think of it that way … to us, it’s a ball
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
15

Return to the two slides on the topic of
parameters …
Parameters: Customize each function call
to a specific situation – they are the input to
the function
• Parameters are the names of the input
values used inside of the procedure body
• Arguments are the values from outside to
be used for each of the parameters
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
16


Notice that if the DEFINITION has n
parameters, the CALL needs n arguments
The parameters and arguments correspond
Inside of the function, the
parameter, e.g. xPos, is declared
and initialized to the
corresponding argument, e.g.
400. Then, the definition uses it,
e.g.
a.CenterX = 400;
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
17



Parameters are automatically declared (and
initialized) on a call, and remain in existence
as long as the function remains unfinished
When the function ends, the parameters
vanish, only to be recreated on the next call
It is wise to choose parameter names, e.g.
x-P-o-s that are meaningful to you
 I chose xPos as the x position of the bullseye
center
 Notice that I used that name a lot, and the
meaning to me remained the same
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
18

We say that a function definition has 3 parts:
name, params, body
 Name is critical: it names the “concept” created
by the function
 Parameters are critical: they customize a function
to many cases
 Body is critical: it defines how the function works

Function uses (calls) have 2 parts: name, args
 Name is critical: says what concept you will use
 Arguments are critical: says how this case handled
11/6/2015
Kelvin Sung (Use/modify with permission from © 2010 Larry Snyder, CSE)
19