Вы находитесь на странице: 1из 4

L|ements of User Def|ned Iunct|ons:

1here are 1P8LL elemenLs LhaL are relaLed Lo funcLlons:


1. Iunct|on def|n|t|on : ls an lndependenL program module LhaL ls
speclally wrlLLen Lo lmplemenL Lhe requlremenLs of Lhe funcLlon.
2. Iunct|on ca|| : 1hls ls requlred ln order Lo lnvoke lL aL a requlred place ln
Lhe program.
3. Iunct|on dec|arat|on: 1he calllng funcLlon should declare any funcLlon
(llke declaraLlon of a varlable) LhaL ls Lo be used laLer ln Lhe program. 1hls
ls known as funcLlon declaraLlon or funcLlon proLoLype.
Iunct|on Def|n|t|on:- Iunct|on Def|n|t|on:-
- also known as funcLlon lmplemenLaLlon shall lnclude Lhe followlng
elemenLs:
1. luncLlon name
2. luncLlon 1ype
3. LlsL of arameLers
4. Local varlable declaraLlons
3. luncLlon SLaLemenLs
6. A return SLaLemenL
A|| the SIk e|ements are grouped |nto 1WC parts, name|y.
Iunct|on neader ( I|rst 1hree L|ements)
Iunct|on 8ody ( Second 1hree L|ements)
Syntax for funct|on def|n|t|on:
funct|on_type funct|on_name (parameter ||st separated by commas) ]* funct|on
neader*]
{
|oca| Iunct|on-Var|ab|e dec|arat|on,
executab|e statement 1,
executab|e statement 2,
.
..
executab|e statement n,
return statement,
}
If funct|on |s not return|ng anyth|ng then we need to spec|fy the return type as
1he Poromenter /ist dec|ares the var|ab|es that w||| rece|ve the data sent by the ca|||ng program.
1hey serve as |nput data to the funct|on to carry out the spec|f|ed task. S|nce they represent actua|
|nput va|ues, they are often referred to as formo/ porometers.
Iorma| arameters ||st:-
1he parameLer llsL conLalns declaraLlon of varlables separaLed by commas and surrounded by parenLheses.
lnL sum ( lnL a, lnL b)
[
...
}
8eLurn values and oLher Lypes:-
1he calllng funcLlon can only reLurn per call aL mosL.
return, /* acLs as Lhe closlng brace of Lhe funcLlon & Lhe conLrol ls lmmedlaLely
passed Lo Lhe calllng funcLlon */
(or)
return (express|on), /* reLurns Lhe value of Lhe expresslon*/
Iunct|on Ca||:-
- can be done by slmply uslng Lhe funcLlon_name followed by a llsL of acLual parameLers enclosed ln a parenLheses.
Lxample:
ma|n( ) ma|n( )
{
|nt y,
y= mu| ( 10, S),
pr|ntf ( "d\n", y),
}
|nt mu| ( |nt x, |nt y )
{
|nt p,
p = x * y,
return ( p ),
}
Iunct|on Dec|arat|on:-
A funcLlon declaraLlon ( also known as funcLlon proLoLype) conslsL of four
parLs.
- luncLlon 1ype ( reLurn Lype)
- luncLlon name
- arameLer llsL
- 1ermlnaLlng semlcolon
SynLax:-
funcLlon-Lype funcLlon-name (parameLer llsL),
example:
lnL mul ( lnL m, lnL n ), / *funcLlon proLoLype declaraLlon */ lnL mul ( lnL m, lnL n ), / *funcLlon proLoLype declaraLlon */
When a funcLlon does noL Lake any parameLers and does noL reLurn any value, lLs
proLoLype ls wrlLLen as:
vold dlsplay ( vold ),
We can have Lhe funcLlon declaraLlon ln 1wo places:
Clobal proLoLype: - when Lhe funcLlon declaraLlon ls placed before Lhe maln
funcLlon.
Local roLoLype:- When Lhe funcLlon declaraLlon ls placed ln Lhe maln funcLlon.

Вам также может понравиться