0% found this document useful (0 votes)
26 views

07 Lesson7 - Functions

This document provides an overview of functions in MQL4, including: - Functions allow programmers to organize code into reusable blocks called by passing parameters. They are like a "sausage machine" that takes in "meat and spices" (parameters) and outputs a "sausage" (return value). - Special MQL4 functions include Init() for initializing variables, Deinit() for cleanup, and Start() for launching the expert advisor. The document provides examples of defining, calling, and nesting functions to demonstrate their use in MQL4 programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

07 Lesson7 - Functions

This document provides an overview of functions in MQL4, including: - Functions allow programmers to organize code into reusable blocks called by passing parameters. They are like a "sausage machine" that takes in "meat and spices" (parameters) and outputs a "sausage" (return value). - Special MQL4 functions include Init() for initializing variables, Deinit() for cleanup, and Start() for launching the expert advisor. The document provides examples of defining, calling, and nesting functions to demonstrate their use in MQL4 programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MQL4 COURSE

By Coders guru
w w w . f orex -t sd. c om

-7Functions
-------------------

We

lc o m e
T h e fu n c tio
Le a r n i n g t h
U s in g th e m

to
n s
e m
w

th e
in
w
h ic

w o r l d o f M Q L4 F u n c t i o n s .
a n y la n g u a g e ta k e tw o p h a s e s :
h ic h s o m e tim e s a b o rin g th in g .
h a l w a y s a lifeboat.

I w a n t t o t e l l y o u m y t r a di t i o n a l s e n t e n c e :
I h o p e y o u e n j o y e d t h e p r e v i o u s l e s s o n s , w h i c h y o u c a n do w n l o a d t h e m
1- Le s
h ttp
2- Le s
h ttp
3- Le s
h ttp
4- Le s
h ttp
5- Le s
h ttp
6- Le s
h ttp

so n
:/ / w
so n
:/ / w
so n
:/ / w
so n
:/ / w
so n
:/ / w
so n
:/ / w

1 - W e l c o m e t o t h e M Q L4 c o u r
w w .f o r e x -t s d.c o m / a t t a c h m e n t .p
2S Y N T A X .
w w .f o r e x -t s d.c o m / a t t a c h m e n t .p
3 - M Q L4 D a t a t y p e s .
w w .f o r e x -t s d.c o m / a t t a c h m e n t .p
4 - M Q L4 O p e r a t i o n s & E x p r e s
w w .f o r e x -t s d.c o m / a t t a c h m e n t .p
5- Lo o p s & D e c i s i o n s ( P a r t 1).
w w .f o r e x -t s d.c o m / a t t a c h m e n t .p
6 - Lo o p s & D e c i s i o n s ( P a r t 2).
w w .f o r e x -t s d.c o m / a t t a c h m e n t .p

fro m

h e re :

se .
h p ? a t t a c h m e n t i d= 37 2
h p ? a t t a c h m e n t i d= 39 9
h p ? a t t a c h m e n t i d= 469
s io n s .
h p ? a t t a c h m e n t i d= 48 1
h p ? a t t a c h m e n t i d= 50 4
h p ? a t t a c h m e n t i d= 547

Le t s s t a r t t h e s e v e n t h l e s s o n .

W h a t s th e m e a ning of f unctions?
T h e fu n c tio n is v e ry lik e th e s a u s a g e
o u t s t h e sau sag e.
T h e m e a t a n d t h e s p i c e s a r e t h e fu n c
v a l u e . T h e m a c h i n e i t s e l f i s t h e fu n c
T h e r e s o n l y o n e di f f e r e n c e b e t w e e n
th e fu n c tio n s w ill re tu rn n o th in g (n o

m ac h in e, y o u i n p u t t h e m eat a n d t h e sp ic es a n d i t
tion
tion
th e
th in g

p ar am eter s; t h e s a u s a g e i s t h e fu n c tion r etu r n


bod y .
fu n c tio n s a n dy o u r s a u s a g e m a c h in e , s o m e o f
i n M Q L4 c a l l e d v oid ).

Le t s t a k e s o m e e x a m p l e s :
do u b l e
// ty p e o f th e s a u s a g e r e tu rn v a lu e
m y _ f u n c ( do u b l e a , do u b l e b , do u b l e c ) / / f u n c t i o n n a m e a n d p a r a m e t e r s l i s t ( m e a t &
s p ic e s )
{
r e t u r n ( a * b + c );
// s a u s a g e o u ts -r e tu r n e dv a lu e
}
A s y o u s e e a b o v e , t h e f u n c t i o n s t a r t s w i t h t h e ty p e o f t h e r e t u r n e d v a l u e d ou ble
f o l l o w e d b y t h e fu n c tion n am e w h i c h f o l l o w e d b y p a r e n t h e s e s .
I n s i de t h e p a r e n t h e s e s y o u p u t t h e m e a t a n d s p i c e s , s o r r y , y o u p u t t h e p ar am eter s o f t h e
fu n c tio n .
H e r e w e h a v e p u t t h r e e p a r a m e t e r s d ou ble a, d ou ble b, d ou ble c .
T h e n t h e f u n c t i o n b o dy s t a r t s a n d e n ds w i t h b r a c e s . I n o u r e x a m p l e t h e f u n c t i o n b o dy w i l l
p r o du c e t h e o p e r a t i o n ( a* b + c ) .
T h e r etu r n k e y w o r d i s r e s p o n s i b l e a b o u t r e t u r n i n g t h e f i n a l r e s u l t .

R e tur n k e y w or d :
T h e r e t u r n k e y w o r d t e r m i n a t e t h e f u n c t i o n ( l i k e t h e br eak
i t g i v e s t h e c o n t r o l t o t h e fu n c tion c aller ( w e w i l l k n o w i t
T h e r e t u r n k e y w o r d c a n i n c l u de a n e x p r e s s i o n i n s i de i t s p a
e x a m p l e r etu r n ( a* b + c ) ; a n d t h i s m e a n s t o t e r m i n a t e t h e
o f th e e x p re s s io n .
A n dit c a n b e w ith o u t e x p re s s io n a n dits o n ly jo b in th is c a

k e y w
so o n
r e n th
fu n c

o r d do e s i n t h e l o o p ), a n d
).
e s e s lik e th e a b o v e
tio n a n dre tu rn th e r e s u lt

s e is to te r m in a te th e fu n c tio n .

N o t i c e : N o t a l l t h e f u n c t i o n s u s e t h e r e t u r n k e y w o r d, e s p e c i a l l y i f t h e r e s n o r e t u r n v a l u e .
Li k e t h e n e x t e x a m p l e :
v o id
// v o idm e a n th e r e s n o s a u s a g e re tu rn e dv a lu e .
m y _ fu n c ( s tr in g s )// fu n c tio n n a m e a n dp a r a m e te rs lis t ( m e a t & s p ic e s )
{
P r i n t ( s );
}
T h e fu
W h e n
T h e se
c a llin g

n c tio n
th e fu
k i n ds
th e m

a b o v e w i l l n o t r e t u r n v a l u e , b u t i t w i l l p r i n t t h e p a r a m e t e r s y o u p r o v i de d.
n c t i o n h a s n o r e t u r n v a l u e y o u u s e v oid a s t h e f u n c i o t n r e t u r n s t y p e .
o f f u n c t i o n s i n s o m e p r o g r a m m i n g l a n g u a g e c a l l e d M eth od s , b u t M Q L4
fu n c tio n s .

Function ca l l :

W e k n o w v e r y w e l l n o w w h a t t h e f u n c t i o n i s ( I h o p e )? H o w t o u s e t h e f u n c t i o n s i n y o u r
M Q L4?
T h e r e s a n e x tr a s te p s a fte r w ritin g y o u r fu n c tio n to u s e th e fu n c tio n in y o u p r o g ra m .
T h i s s t e p i s c allin g it ( u s i n g i t ).
A s s u m e y o u h a v e a fu n c tio n w h ic h c o lle c ts th e s u m m a tio n o f tw o in te g e rs .
T h is is th e fu n c tio n :
i n t c o l l e c t ( i n t f i r s t _ n u m b e r , i n t s e c o n d_ n u m b e r )
{
r e t u r n ( f i r s t _ n u m b e r + s e c o n d_ n u m b e r );
}
Y o u k n o w h o w th e p r e v io u s fu n c tio n w o rk s , b u t y o u w a n t to u s e it.
Y o u u s e it lik e th is :
in t a
in t b
in t s
P rin

= 10 ;
= 15;
u m = c ollec t( a , b );
t ( s u m );

T h e e x a m p l e a b o v e w i l l p r i n t 2 5 ( i s i t a m a g i c ). B u t h o w di d i t k n o w ?
T h e m a g i c l i n e i s in t su m = c ollec t( a, b) ; h e r e y o u de c l a r e d a v a r i a b l e ( su m ) t o h o l d t h e
f u n c t i o n r e t u r n v a l u e a n d g a v e t h e f u n c t i o n i t s t w o p a r a m e t e r s ( a, b).
Y o u b a s i c a l l y c alled th e fu n c tion .
M Q L4 w h e n s e e y o u r fu n c tion n am e, i t w i l l t a k e y o u p a r a m e t e r s a n d g o t o t h e f u n c t i o n
a n d i t w i l l r e t u r n s o o n - w i t h t h e r e s u l t a n d p l a c e t h e m i n s a m e l i n e .
I t s v e r y lik e c o p y in g a ll th e lin e s o f th e f u n c tio n in s te a do f th e p la c e y o u c a lle dth e
fu n c tio n in , e a s y r ig h t?

N e sting f unctions insid e f unction:


Y o u c a n n e s t f u n c t i o n ( o r m o r e ) i n s i de t h e b o dy o f a n o t h e r f u n c t i o n . T h a t s b e c a u s e t h e
c a l l e r l i n e i s t r e a t e d l i k e a n y n o r m a l s t a t e m e n t ( i t s a c t u a l l y a s t a t e m e n t ).
F o r e x a m p le :
W e w i l l u s e t h e c o l l e c t f u n c t i o n de s c r i b e d a b o v e i n s i de a n o t h e r n e w f u n c t i o n w h i c h i t s
jo b is p rin tin g th e re s u lt o f th e c o lle c tio n :
v o i d p r i n t _ c o l l e c t i o n ( i n t f i r s t _ n u m b e r , i n t s e c o n d_ n u m b e r )
{
i n t s u m = c o l l e c t ( f i r s t _ n u m b e r , s e c o n d_ n u m b e r );
P r i n t ( s u m );

}
H e r e w e c a l l e d t h e c ollec t f u n c t i o n i n s i de t h e p r in t_ c ollec tion f u n c t i o n b o dy a n d p r i n t e d
t h e r e s u l t . v oid m e a n s t h e r e s n o r e t u r n v a l e ( do y o u s t i l l r e m e m b e r ? ).

M Q L 4 S p e cia l f unctions init( ) , d e init( ) a nd sta r t( ) :

I n M Q L4, e v e r y p r o
w h e n y o u a tta c h y o u
c h a rts o r in th e c a s e
jo b is in itia liz in g th e
in itia liz a tio n in th e n

g ra m
r p r
y o u
m a
e x t

b e g in s w ith th e fu
o g r a m ( E x p e r t a dv i s
c h a n g e th e fin a n c ia
in v a ria b le s o f y o u r
l e s s o n ).

n c tio
o r o r
l s y m
p ro g

n
C u
b o
ra m

in it( ) ( i n i t i a l i z e ) a n d i t o c c u r s
s t o m i n di c a t o r ) t o t h e M e t a T r a de r
l o r t h e c h a r t p e r i o di c i t y . A n d i t s
( y o u w ill k n o w a b o u t th e v a ria b le s

W h e n y o u r p r o g r a m f i n i s h e s i t s j o b o r y o u c l o s e t h e c h a r t w i n do w o r c h a n g e t h e
f i n a n c i a l s y m b o l o r t h e c h a r t p e r i o di c i t y o r s h u t do w n M e t a T r a de r t e r m i n a l , t h e f u n c t i o n
" d ein it( ) " ( de -i n i t i a l i z e ) w i l l o c c u r .
T h e t h i r d f u n c t i o n ( w h i c h i s t h e m o s t i m p o r t a n t o n e ) star t( ) w i l l o c c u r e v e r y t i m e
n e w q u o t a t i o n s a r e r e c e i v e d , y o u s p e n d 9 0 o f y o u r p r o g r a m m i n g l i f e i n s i de t h i s
fu n c tio n .

W e w i l l k n o w a l o t a b o u t t h e s e f u n c t i o n s i n o u r r eal w or ld lesson s w h e n w e w r i t e o u r
o w n E x p e r t a dv i s o r a n d C u s t o m I n di c t o r .

I h o p e y o u e n jo y e dth e le s s o n .
I w elc om e v er y m u c h th e q u estion s an d th e su g g estion s.
S e e y o u
C od er s G u r u
25-10 -20 0 5

You might also like