Professional Documents
Culture Documents
Glava 8
Glava 8
,
.
. , ,
.
.
, o ce
, .
.
.
. ,
, .
, if n.
if (alpha > 3)
{
int n;
cin >> n;
beta = beta + n;
}
, n &
.
.
.
.
. C++
. ,
. C++
.. .
78
. :
int gama;
// Globalna promenliva
int main ( )
{
gama = 3;
.
.
}
void SomeFunc ( )
{
gama = 5;
.
.
}
, gama &
main SomeFunc.
,
. :
#include <iostream>
using namespace std;
void SomeFunc( float );
const int a = 17;
// Globalna konstanta
int b;
// Globalna promenliva
int c;
// Globalna promenliva
int main ( )
{
b = 4;
c = 6;
SomeFunc(42.8);
return 0;
}
void SomeFunc( float c )
{
float b;
79
b = 2.3;
80
void Block2( );
int a1;
// Globalna promenliva
char a2;
int main( )
{
.
.
}
// **********************************************************************
void Block1( int a1,
char& b2 );
{
int c1;
int d2;
}
// **********************************************************************
void Block2( )
{
int a1;
int b2;
while ()
{
// Block3
int c1;
int b2;
.
.
}
}
81
.
. ,
.
, &
. ,
( )
( ).
C++ & (allocate)
, (deallocate)
.
. ,
static, :
void SomeFunc( )
{
float SomeFloat;
.
.
}
,
. ,
.
,
& .
, ,
& . C++
.
. :
82
int sum = 0;
. :
Void SomeFunc( int someParam )
{
int i = 0;
int n = 2 * someParam+3
.
.
}
, ,
, static,
.
( ).
Void AnotherFunc( int param )
{
static char ch = A;
.
.
}
&
.
(interface)
(
) :
-
- () .
.
( -)
83
.
- .
,
:
( )
( )
- (
, )
- (, cin,
cout) .
,
.
.
,
. ,
, .
.
,
& .
(side effect) ,
,
.
,
(. 8.1).
,
.
84
strani ~ni
({tetni )
ef ekti
Li stanaparametri
ti pi ~naf unkci ja
sl . 8.1
,
. ,
. ,
, .
85
(Value-Returning Functions)
.
C++: sqrt, abs, fabs .
-, void
. void
, .
,
.
1-365 1-366
.
int Day( /* in */ int month,
/* in */ int dayOfMonth
/* in */ int year
{
int correction = 0;
86
correction = correction + 4;
return (month 1) * 30 + correction + dayOFMonth;
}
void , (heading)
( int) void.
return ,
. return
integer return.
, ,
return.
.
(template) :
- ( )
{
.
.
.
}
, ()
, default int.
.
, void . , ,
. .
. x n
x n .
int Power ( /* in */ int x,
/* in */ int n)
// Osnova na stepen
// Eksponent
87
{
int result;
// Cuva rezultat
result = 1;
while (n > 0)
{
result = result * x;
n--;
}
return result;
}
. n! = 1 2 ... n 0! = 1
Int Factorial( /* in */ int n )
// Cuva proizvodi
result = 1;
while (n > 0)
{
result = result * n;
n--;
}
return result;
}
Factorial :
combinations = Factorial (n) / (Factorial (m) * Factorial (n-m));
88
(interface)
,
. .
.
, void .
(stream) . C++ (
) .
,
void . :
1.
(caller) void ;
2. I/O ;
3. bool,
;
4.
, ;
5. , void a;.
6. void a a ,
.
89