170 likes | 390 Views
Functions (Methods). An object has Properties Methods. Functions (Methods). Functions Methods A function is a struct A function is usually defined in a class Decisions, loops, and methods change the execution flow. Functions (Methods).
E N D
Functions (Methods) • An object has • Properties • Methods
Functions (Methods) • Functions Methods • A function is a struct • A function is usually defined in a class • Decisions, loops, and methods change the execution flow.
Functions (Methods) • Key point. Functions provide a new way to control the flow of execution. • What happens when a function is called: • Control transfers to the function code. • Argument variables are assigned the values given in the call. • Function code is executed. • Return value is assigned in place of the function name in calling code. • Control transfers back to the calling code.
Functions (Methods) Math functions x f(x) = 5 x x, y, z g(x,y,z) = x y/z f and g are function names x,y,z are arguments 5x and xy/z are function (return) values.
Functions (Methods) Java functions • A function has a name (identifier) • A function takes zero or more arguments • A function has a body • A function returns a value or void (no returns)
Functions (Methods) • Defining a function • Many commonly-used functions are defined and kept in libraries (packages) • Defining a function does not mean to execute the function. A function is executed only when it is called. • Calling a function: actually execute the statements in the called function.
Functions (Methods) • Defining a function returnTypeFunctionName( formal args) returnType --- any type or void (nothing) FunctionName--- the name of the function; it’s an identifier. Formal args--- could be none. An argument has the form dataTypeargName
Functions (Methods) • Examples of defining a function • void setup() --- the function named setup has zero arguments and returns nothing. • double random( double val) --- the function random has one argument and returns a double value. • void rect( double x, double y, double width, double(height) --- the function rect needs 4 double arguments, draws a rectangle, and returns nothing.
Functions (Methods) • Calling a function FunctionName( actual arguments ); if the function returns void or someVar = FunctionName( actual args); if the function returns a value
Functions (Methods) • Examples in Processing: • double randomNumber = random(255.0); • rect(0.0,0.0,100,100); • Example in Java: • System.out.print(“Hello”); • value = inp.nextDouble();
Functions (Methods) • The number and types of the actual arguments must be the same as the “formal” (dummy) arguments in the function definition. • Java passes arguments (from the caller to the defined function) by value. • However, there two types of variables: value type and reference type.
Functions (Methods) • Value variable: variable of a primitive type and stores a value. • Pass a value variable is to pass a copy of the variable. • If the value of the variable changed in the function, it will NOT affect the original variable. That is, the value of the variable in the caller will NOT change. • Examples.
Functions (Methods) • Reference variable: variable of an object type and stores a reference (address). • Pass a reference variable is to pass the address of the variable. • If the value of the variable changed in the function, it will affect the original variable. That is, the value of the variable in the caller will change accordingly. • Examples: later after learning the array or class.
Functions (Methods) • Scope (of a name). The code that can refer to that name. • Example: A variable's scope is code following the declaration in the block.
Functions (Methods) • Local and class variables: • Local variable: a variable defined in a block. Its scope is local to that block. • Class variable: a variable defined at the class level, its scope is the whole class. • The lifetime of a local variable is the same as the block. • The lifetime of a class variable is the same as the object of the class.
Functions (Methods) • Examples: • Swap function to demonstrate passing by value. • Display digit in a number