1 / 16

Functions (Methods)

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

ophrah
Download Presentation

Functions (Methods)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions (Methods) • An object has • Properties • Methods

  2. 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.

  3. 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.

  4. 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.

  5. 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)

  6. 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.

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

  8. 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.

  9. Functions (Methods) • Calling a function FunctionName( actual arguments ); if the function returns void or someVar = FunctionName( actual args); if the function returns a value

  10. 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();

  11. 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.

  12. 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.

  13. 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.

  14. 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.

  15. 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.

  16. Functions (Methods) • Examples: • Swap function to demonstrate passing by value. • Display digit in a number

More Related