Ch3. FUNCTIONS/MeTHODS
Definition > Function isa sequence or st of statements that cart outa particular task. Every function
hasa unique name and may or may not return avalue
Advantages of functions. >
\) helps to cope with complexty by dividing a complex taskinto smaler and easly manageable blacks
helps in hiding the implementation det 7
i) enhances code reusability
‘General format/Syntax ofa function definition
[access spectieristaticleturn-type method-name (parameter lst)
{ body ofthe methos)
Function header/Prototype > the fist line ofthe function definition that includes access specifier, stale Keyword
‘return type, name and parameter list
Function sighature->name of the method along with number an type of parameter lst sometimes also referred to 35
function prototype)
‘Accass specifier determines the type of access tothe function. Can be ether public, private protected or defaultiwhen
‘no keyword is provided)
‘public The most liberal access where a member is accesible to any ebject of any class of any package
defoulyfriendly/package-> member of ths access can be accessed by any objet of any elas but within 9
sil package,
Protected-> this provides acces tothe objets of single lass and its subclasses in a single package
Private the most restricted access. n this case a method can be accessed only by the objects of a single cass
‘only Even the subclasses within the clas a unly acess,
Static} Isa modifier; it dentifies the method asa dass methd ie function or method canbe called without resting
any object. 7a method is defined stati it can be accessed without creating any object. n the absence of static keyword
‘method can be invoked only by creating an object
‘other Msifiers> fal
Fino! sa keyword to make the valve of variable constant. cannot be charged during ts ifetime within a program,
Eg final nt x225; value of x cannot be updated. 2x15; oF xex=2 wil NOT be accepted
Aetun type specifies the typeof value thatthe return statement ofthe methoe returns. t may be of any vai Jove data
tye or vol
ow to aces «functor 2 methos/uncion i caleovokedexecuted by proving the pethad name ‘lowed bythe
satameters being ent encaseingarenthess though an objet (without abject for stat uncon}
‘rma parameters Poremeter st) used in the function definition tne; variables preceded by a datatype. A function
uses parameter ist to accept dat of any data ype and proceses Ito give the equred outgt
Aca Argument pated wie ling a func: ot preceded by any data ype. Helps o pa the onal values to
‘eta statement) passes/retunsayalue tothe caling function
methestncion i consol stansfertes othe caing onion,” “NN %. tmedate ext from the
"Noe: oreturable unetions, return can be ued or any numberof tines. However ony
‘and remaining are ignored. ly the first return gets executedFunctions can be broadly classified as
{i Predefined funetons{pow( art). ete,)
sersefined functions can futher be csi brs un,
Pure function) Aso calles Aceeior methods: takes arg
the state ofan object but doesnot moa the objec
Classification of methods based on their invoeation.
1. Non Returnable and Non Parameterized
3. _Returnable and Non Parameterises
Function overloading
‘refers tothe conaltion where many functions are de
Static binding/Earty bindi
calls the required functio
‘the order of parameters.
ined with sa
ing: During overloading as al the fun
n by matching withthe parameter st
Need/advantage of overloading
‘rogram, thus makes the program ru
done by using the function overloading)
Key for function overloading->is the parameter ls thatincdes type,
{0:do with the RETURN type}
‘How can you poss the following? Primitive types colby value
catty value
i. Workings done on the formal parameter
2. any change inthe formal parameters does not | 2
affect the value of actual arguments. |
3 Nowa ed when engl assur]
bealered.
4. Aetalparametes passedare of prt pe |
5. Separate memory Ie reserved for actual ind | 4
formal parameter.
Statics variables
1 Static wari
any object. na iflerent cass, they need objet
and dot operator
2. Ipereated when the clas fist refered to 2
3. legestroyed when the program i ove. 2
Me cet wi ve te atone |
Describe the components of an objec creation ine:
sample E=new Example
Here, ample is pame of theclass from which an obec i ereated
£19 is qame ofthe obec ereated from the class Example
iON and impute
umensiprimiie
‘heciginal vale doe
impure freon a0 called tat maods hanger odes the tt
‘Restos the state of
1 Multiple function declarations inthe same
1 implements ROLYMORPHISM an
Faster. [ when same action has
same table,
(i) User defined functions
bee) return information to
the caller abou
n't get changed
ved object
2. Non Retunable and Porameteried
‘4 tunable and Parameterized
Se9pe With same name but diferent parameter st.
me name but hase vared Ist of paramere,
tions have the same name, the comple setifis ae
Le the number of parameters, typeof parameters and
Wao reduces the oumber of comparisons in
to beperformed with diferent datatypes, ean ba
umber ani orde of parameters (and has nothing
+ Deived types cal by reference
cal av nerenence
Wierngs done the actual argaments
When 3 function i ale, the relrence to the
cat gens il ‘be panied tral
parameters and affects the erga values
Normally used when prignal values need to be
update. |
‘Actual parameters pasted are of eject ypes
instance/ Global variables
Insane varabes can be accessed only with the
help an object and dot operator
"screed when an bet of theclassiserested. |
'sdestoyed wen abject is destroyed |
oe |
Each abject will have a liferent value for the |
ew? i 2 keyword to create an obec. Italoated required memory forthe object crested
sao te elu contr he oath enter tes ith glyDitferentia
Se
between the followings
1
nein Prete Hadar | Funcvon Signature
eee en came iter andincludes | itis the prt of Function Header ana” |
Pecans |e on Mem on
Formal Parameters ‘etualAcguments
These are the variables provided withthe
‘a. These are the variables/values passed
method definition ‘white Invoking the method.
|b These areprecedea by ava egl data |b, Doesnt recede nth any data ype
[aap |
‘ati oid sumint nt 77 aa ave Foal Paramtes
{System out prininiasbl
Publi stati vol man)
{int e23ymoa5,
Summ k and mare Actua Arguments
Bi Returnable functions — T Won feturnable Functions
1 They have toreturnavaluetothe | 1 Mayer may not euro ale Tobe
éaling methods cating methods
2. Willrtur only a SINGLE value 2. May provide a single oF multiple or no
| 3. receded by any ONE ofthe jas vate) |
loyal datatype teint Sting ete Preceded by he keyword vol
[—eynamiewading —— Satie Binding
[2 tani were mtemod ae | Foie rn antn Son dg
defned and invoked
2. Thereisacontiuousinvacaionot | _2,One a themultpe functions is identi |
Imethogs where method are nated and invoked depending onthe order,
tne after another depending on he ‘urbe ard type of parameters,
|__ need 7 " aa]
Caling Function > A unetion that invokes another fusction. Emi) invokes other methods and a ugh ti
called 3 Cling Metnos
Called Function-> A funtion that sinvoked in another function. fob sumi); here sum isthe called method
Instanttion> the proces of ereating objets using ‘new kod roma dass
Intalization> the proces of providing value to the variable.
state members can be scered witout czatlg anu bet.
Non tate members cn be invoke acesed ony withthe ln atan object
Buble acces spelter> provides mos tera acces where members can be accessed by
accessed by any objeto am
lass of eny package. ”
r
SNGLecosin sme pce rhesus cos comataen an Se