Functions: Com - Cplusplis - WWW
Functions: Com - Cplusplis - WWW
com
Functions
C++
c++
www.cplusplis.com
2006
Compiler_x@ yahoo.com
3 .............................................................................................. functions
9 ...................................................................... void
11 ..............................
) Declaring functions(prototype
17 .....................................
: functions
))(( main
-1
-2
-3
-4
..
...
"( .
)"
"(
)"
++
.++
:
)(Int main
{
.
.
.
"<<cout
;"
.
.
.
"<<cout
;"
.
.
"<<cout
;"
.
.
..
;return 0
}
4
)(int main
{
.
.
"<<cout
"
.
.
.
.
.
.
;return 0
}
"(
)"
"(
)"
"<<cout
"
)(int main
{
.
.
.
.
.
.
;return 0
}
-1
++
compiler
-2
++
-3
"
"
.. void
long
float
int
: Type
: Function_name
.
++
: Parameter
)
.
: Statement
++
: Return
(.
value
:Value
type
)(int main
{
;int c
;)c=sum(3,5
;cout<<" 3 + 5 = "<<c
;return 0
}
3
4
5
6
7
8
9
10
. iostram.h
sum
int
x y
int
.z
y
x
int
.c
.c
=3+5
)(. main
++
-1
int a
-2
compiling
sum
)sum(3,5
3,5
)sum (3,5
) int sum ( x , y
;) c=sum ( 3 , 5
. 5
x,y
)4
( return z ) 5
8 )(8=5+3
)(8
sum
sum
). c=sum (3,5
) int sum ( x , y
8
;) c=sum( 3 , 5
.8
10
// function example
>#include <iostream.h
)int subtraction (int a, int b
{
;int r
;r=a-b
;)return (r
}
)( int main
{
;int x=5, y=3, z
;)z = subtraction (7,2
;'cout << "The first result is " << z << '\n
;'cout << "The second result is " << subtraction (7,2) << '\n
;'cout << "The third result is " << subtraction (x,y) << '\n
;)z= 4 + subtraction (x,y
;'cout << "The fourth result is " << z << '\n
;return 0
}
a , b
subtraction
;'cout << "The second result is " << subtraction (7,2) << '\n
cout
;'cout << "The third result is " << subtraction (x,y) << '\n
2
7
)( x,y
Z=4+2
Z=2+4
void
Ty
pef
unc
t
i
o
_n
(name
pa
r
a
me
t
e
r
1
,
par
a
me
t
e
r
2
,.
)
{
;statement 1
;statement 2
;statement 3
.
.
.
return value
}
...
type
++
++
(procedure
void
type
return
!I'm a function
void
)( void printmessage
{
;"!cout << "I'm a function
}
) ( int main
{
;) ( printmessage
;return 0
}
printmessage
""I'm a function
. return
void
) (printmessage
void
)void printmessage(void
) (
printmessage
;)( printmessage
;Printmessage
10
)(void
I'm a function
// function example
>#include <iostream
the result is 8
void
scope of variable
local variable
global variable
11
>#include <iostream.h
//global variable
;int age
//local variable
)(void main
{
;"char name[20]="my name
;cin>>age
;)print_info(name
}
global
age
print_info
print_info
local
name
print_info
name
name
) pass by
(value
)(pass by reference
12
pass by reference
duplicate
) & (
by reference
a,b,c
a b c
x y z
duplicate
a,b,c
&
x
duplicate
&
( duolicate
13
// more than one returning value
>#include <iostream.h
Previous=99, Next=101
next
previous
& ,
& .
) = (
:
.
3
5
14
divide
)divide (6
divide
) . ( int b = 2
) . ( 6/2
)divide (20,4
( int b=2 ) b
5 . (20/4)=5
Overloaded functions
c++
10
2.5
// overloaded function
>#include <iostream.h
)int operate (int a, int b
{
;)return (a*b
}
)float operate (float a, float b
{
;)return (a/b
}
)( int main
{
;int x=5,y=2
;float n=5.0,m=2.0
;)cout << operate (x,y
;"cout << "\n
;)cout << operate (n,m
;"cout << "\n
;return 0
}
int
, operate
compiler
. float
float
. int
15
, int
operate
, float
operate
.(overloaded
Recursive functions
5!= 5 * 4 * 3 * 2 * 1
)!5!= 5 * (4
4!= 4 * 3 * 2 *1
(1!)*2
(2!)*3
(3!) *4
1=1
// factorial calculator
>#include <iostream.h
)long factorial (long a
{
)if (a > 1
;))return (a * factorial (a-1
else
;)return (1
}
)( int main
{
;long number
;" cout << "Please type a number:
;cin >> number
;)cout << number << "! = " << factorial (number
;return 0
}
factorial
number
factorial
16
!3
!3
) !(4* 3
!1
)!( 2 * 1
!2
!2
)!(3*2
factorial (1) =1
// factorial calculator
>#include <iostream.h
)int power (int a,int b
{
)if (b<=1
;return a
else
;)))return (a * power(a,(b-1
}
)( int main
{
;int x=2,y=3
;)cout<<power(x,y
;return 0
}
( b
b=1
2^3
17
2 ^3 = 2 * 2 * 2 = 8
)power(2,3)=2 * power(2,2
power(2,1)=2
) (prototype
Declaring functions
18
) ( %
ood
) Number is odd.
even
odd
) ctrl+F9
debug
Run
...
even
.. even
100/100
..
odd
compiler
even
) even
even
)(Int main
{
;x=4
;int x
;cout<<x
return 0
}
..
void
even
..
++
) ; (
19
Type a number (0 to exit): 9
Number is odd.
Type a number (0 to exit): 6
Number is even.
Type a number (0 to exit): 1030
Number is even.
Type a number (0 to exit): 0
Number is even.
even
viod
odd
odd even
void
*
:
*
.
20
2006
Compiler_x@ yahoo.com