0% found this document useful (0 votes)
54 views16 pages

Functions (M Ethods) : An Object Has

This document discusses functions and methods in programming. It explains that functions allow programmers to control the flow of execution by transferring control to the function code when called. When a function is called, its arguments are assigned values and its code is executed before returning a value or void. Functions are defined with a name, arguments, body, and return type, and are called by passing actual arguments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views16 pages

Functions (M Ethods) : An Object Has

This document discusses functions and methods in programming. It explains that functions allow programmers to control the flow of execution by transferring control to the function code when called. When a function is called, its arguments are assigned values and its code is executed before returning a value or void. Functions are defined with a name, arguments, body, and return type, and are called by passing actual arguments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Functions (M ethods)

An object has
Properties
Methods

Functions (M ethods)
Functions Methods
A function is a struct
A function is usually defined in

a class
Decisions, loops, and methods
change the execution flow.

Functions (M ethods)
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 (M ethods)
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 (M ethods)
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 (M ethods)
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 (M ethods)
Defining a function

returnType FunctionName( formal args)


returnType --- any type or void (nothing)
FunctionName --- the name of the
function; its an identifier.
Formal args --- could be none. An
argument has the form
dataType argName

Functions (M ethods)
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 (M ethods)
Calling a function

FunctionName( actual
arguments );
if the function returns void
or
someVar = FunctionName( actual
args);
if the function returns a value

Functions (M ethods)
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 (M ethods)
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 (M ethods)
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 (M ethods)
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 (M ethods)
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 (M ethods)
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 (M ethods)
Examples:
Swap function to demonstrate passing

by value.
Display digit in a number

You might also like