ankakusu
06/10/2007, 15:15
ben yine kafama takılan sorularla devam ediyorum...
şimdi benim altta yaptığım cinslik şöyle:
fonksiyon prototiplerini main fonksiyonun içinde tanımladım.
Mantıken bu kodun compile etmesi gerekmiyor mu?
Çünkü kod baştan sona doğru işliyor. dolayısıyla
bütün fonksiyon prototiplerini görebiliyor.
bu sebepte main fonksiyonunda her fonksiyonu
çağırabilirim.
ama main içinde değil de alttaki global
fonksiyonlarının içinde bir fonksiyon çağırırsam
sanırım fonksiyon prototiplerinin 'scope' 'u yani
faaliyet alanı ( :) komik bi çeviri oldu sanki ) main
içinde kaldığı için ve biz de alttaki fonksiyonun içinden
main'in faaliyet alanını göremediğimiz için hata verdi
değil mi?
sanırım cevabı kendim buldum ama yine de yollayayım bu
yazıyı belki başka birine faydalı olur...
şimdi kodu hemen altta veriyorum:
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
//function prototypes
void prod(void);
int sum (int n);
int h(void );
int g(void );
int squareByReference(int &a);
int squareByValue ( int a);
void useGlobal();
void useStaticLocal();
void useLocal();
prod();
sum(5);
return 0;
}
// altta fonksiyonların detaylı halleri var:
void prod(void)
{
int a,b,c,result;
cout << "enter 3 int"<<endl;
cin >> a>>b>>c;
result = a*b*c;
cout <<"result : " <<result << endl;
//return result;
}
int sum (int n)
{
//static int x=1;
if( n!=0)
{
// cout <<x<< " ";
// x++;
return n+sum(n-1);
}
else
return 0;
}
int g(void)
{
cout << "inside function g" << endl;
h();
return 1;
}
int h(void )
{
cout << "inside function h" << endl;
return 1;
}
int squareByReference(int &a)
{
a=a*a;
return a;
}
void useLocal()
{
int x = 25;
cout << "burada 25 olacak. " << x <<endl;
x++;
cout << "burada 26 olacak. " << x <<endl;
}
void useStaticLocal()
{
static int x = 50;
cout << "burada 50 olacak. (2.kez oldugunda 51) " << x <<endl;
x++;
cout << "burada 51 olacak. (2.kez oldugunda 52)" << x <<endl;
}
void useGlobal()
{
cout << "burada 1 olacak. (2.kez oldugunda 10) " << x <<endl;
x *= 10;
cout << "burada 10 olacak. (2.kez oldugunda 100)" << x <<endl;
}
int squareByValue ( int a)
{
//see a and b but not any other thing
a = a*a;
return a;
}
verdiği hata:
Compiling...
chap6.cpp
error C2065: 'h' : undeclared identifier
error C2373: 'h' : redefinition; different type modifiers
Error executing cl.exe.
chap6.exe - 2 error(s), 0 warning(s)
şimdi benim altta yaptığım cinslik şöyle:
fonksiyon prototiplerini main fonksiyonun içinde tanımladım.
Mantıken bu kodun compile etmesi gerekmiyor mu?
Çünkü kod baştan sona doğru işliyor. dolayısıyla
bütün fonksiyon prototiplerini görebiliyor.
bu sebepte main fonksiyonunda her fonksiyonu
çağırabilirim.
ama main içinde değil de alttaki global
fonksiyonlarının içinde bir fonksiyon çağırırsam
sanırım fonksiyon prototiplerinin 'scope' 'u yani
faaliyet alanı ( :) komik bi çeviri oldu sanki ) main
içinde kaldığı için ve biz de alttaki fonksiyonun içinden
main'in faaliyet alanını göremediğimiz için hata verdi
değil mi?
sanırım cevabı kendim buldum ama yine de yollayayım bu
yazıyı belki başka birine faydalı olur...
şimdi kodu hemen altta veriyorum:
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
//function prototypes
void prod(void);
int sum (int n);
int h(void );
int g(void );
int squareByReference(int &a);
int squareByValue ( int a);
void useGlobal();
void useStaticLocal();
void useLocal();
prod();
sum(5);
return 0;
}
// altta fonksiyonların detaylı halleri var:
void prod(void)
{
int a,b,c,result;
cout << "enter 3 int"<<endl;
cin >> a>>b>>c;
result = a*b*c;
cout <<"result : " <<result << endl;
//return result;
}
int sum (int n)
{
//static int x=1;
if( n!=0)
{
// cout <<x<< " ";
// x++;
return n+sum(n-1);
}
else
return 0;
}
int g(void)
{
cout << "inside function g" << endl;
h();
return 1;
}
int h(void )
{
cout << "inside function h" << endl;
return 1;
}
int squareByReference(int &a)
{
a=a*a;
return a;
}
void useLocal()
{
int x = 25;
cout << "burada 25 olacak. " << x <<endl;
x++;
cout << "burada 26 olacak. " << x <<endl;
}
void useStaticLocal()
{
static int x = 50;
cout << "burada 50 olacak. (2.kez oldugunda 51) " << x <<endl;
x++;
cout << "burada 51 olacak. (2.kez oldugunda 52)" << x <<endl;
}
void useGlobal()
{
cout << "burada 1 olacak. (2.kez oldugunda 10) " << x <<endl;
x *= 10;
cout << "burada 10 olacak. (2.kez oldugunda 100)" << x <<endl;
}
int squareByValue ( int a)
{
//see a and b but not any other thing
a = a*a;
return a;
}
verdiği hata:
Compiling...
chap6.cpp
error C2065: 'h' : undeclared identifier
error C2373: 'h' : redefinition; different type modifiers
Error executing cl.exe.
chap6.exe - 2 error(s), 0 warning(s)