Autor Beitrag
Katzenstreu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39

Windows XP Professional

BeitragVerfasst: So 04.06.06 16:00 
Hallo,

ich habe in den folgenden Code (markiert durch Schrägstriche) meine zwei Kommentare geschrieben.

Warum gibt diese Funktion (result:=s*s) immer 0 aus?

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
 TQuadrat1 = class
 x,y,s:integer;
 Quad:TRect;
 constructor Create (xx,yy,ss:Integer);
 procedure Erscheinen;
 procedure Bewegen;
 procedure Verschwinden;
 end;




  TForm1 = class(TForm)
    B_Erscheinen: TButton;
    B_Bewegen: TButton;
    B_Verschwinden: TButton;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure B_ErscheinenClick(Sender: TObject);
    procedure B_BewegenClick(Sender: TObject);
    procedure B_VerschwindenClick(Sender: TObject);



  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  x,xx,y,yy,s,ss:Integer;
  Quadrat1:TQuadrat1;
implementation



{$R *.dfm}

constructor TQuadrat1.Create(xx,yy,ss:Integer);
begin
x:=xx;
y:=yy;
s:=ss;
end;

procedure TQuadrat1.Erscheinen;
begin

 Quad:=Rect(x,y,x+s,y+s);
 Form1.Canvas.Pen.Color:=ClBlack;
 Form1.Canvas.Rectangle(Quad);
end;

procedure TQuadrat1.Bewegen;
var i:Integer;
begin
 for i:=x to Form1.ClientWidth-s-x do
  begin
   Quad:=Rect(i-1,y,i-1+s,y+s);

   Form1.Canvas.Pen.Color:=Form1.Color;
   Form1.Canvas.Rectangle(Quad);

   Quad:=Rect(i,y,i+s,y+s);

   Form1.Canvas.Pen.Color:=ClBlack;
   Form1.Canvas.Rectangle(Quad);

   Sleep(2);
  end;
end;

procedure TQuadrat1.Verschwinden;
begin
 Form1.Canvas.Pen.Color:=Form1.Color;
 Form1.Canvas.Rectangle(Quad);
end;


function a:real;
begin
result:=s*s;                        // a = s * s
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 Quadrat1:=TQuadrat1.Create(90,10,50);
 Form1.Edit1.Text:=FloatToStr(a);   // a soll ins Edit-Fenster geschrieben werden!
end;




procedure TForm1.B_ErscheinenClick(Sender: TObject);
begin
 Quadrat1.Erscheinen;
end;

procedure TForm1.B_BewegenClick(Sender: TObject);
begin
 Quadrat1.Bewegen;
 end;

procedure TForm1.B_VerschwindenClick(Sender: TObject);
begin
 Quadrat1.Verschwinden;
end;

end.


Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt
Einloggen, um Attachments anzusehen!
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: So 04.06.06 16:09 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
function (a:real) : real;
begin
result:=s*s;                        // a = s * s
end

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Katzenstreu Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39

Windows XP Professional

BeitragVerfasst: So 04.06.06 16:16 
user profile iconMarco D. hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
function (a:real) : real;
begin
result:=s*s;                        // a = s * s
end


habst nun genau so gemacht.

[Fehler] Unit1.pas(91): Bezeichner erwartet, aber '(' gefunden
[Fataler Fehler] Project1.dpr(5): Verwendete Unit 'Unit1.pas' kann nicht compiliert werden


Aber dann kommt der obige Fehler :(. Da muss noch was falsch sein :(.
Ironwulf
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 733
Erhaltene Danke: 2



BeitragVerfasst: So 04.06.06 16:19 
du musst deine function noch irgendwo in den interface teil reinsetzen
ausblenden Delphi-Quelltext
1:
function a:real					
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: So 04.06.06 16:19 
:autsch: 'a' soll der Name sein? :gruebel: Ja stimmt, ich dachte das wäre ein Parameter :autsch:

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Katzenstreu Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39

Windows XP Professional

BeitragVerfasst: So 04.06.06 16:23 
Danke dir :).

So gehts teilweise:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function Flaeche(a:real):real;
begin
result:=s*s;
end;

procedure TForm1.FormCreate(Sender: TObject);
var a:real;
begin
 Quadrat1:=TQuadrat1.Create(90,10,50);
 Form1.Edit1.Text:=FloatToStr(a);
end;


Aber nur wenn ich a lokal als Variable festlege. Außerdem ist s*s (hier 50*50) nicht 1,19964452980341E-315 sondern eher 2500!

MfG
Tim ;)
Ironwulf
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 733
Erhaltene Danke: 2



BeitragVerfasst: So 04.06.06 16:23 
a ist der funktionsname, es werden keine parameter mit an die funktion übergeben :P
Katzenstreu Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39

Windows XP Professional

BeitragVerfasst: So 04.06.06 16:29 
Auch wenn ich für result:=0 setzte ist das Ergebis irgendetwas hoch -315!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
function Flaeche(a:real) : real;
begin
result:=0;                      
end


Zuletzt bearbeitet von Katzenstreu am So 04.06.06 16:51, insgesamt 1-mal bearbeitet
Born-to-Frag
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1094

Win XP SP2, Win 2000 SP4
Delphi 7, 2k5
BeitragVerfasst: So 04.06.06 16:50 
und wie heißt deine funktion jetzt :shock:

_________________
Theorie ist wenn man alles weiß, aber nichts funktioniert. Praxis ist wenn alles funktioniert, aber niemand weiß warum.
Microsoft vereint Theorie und Praxis: Nichts funktioniert und niemand weiß warum.
Katzenstreu Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39

Windows XP Professional

BeitragVerfasst: So 04.06.06 16:50 
user profile iconBorn-to-Frag hat folgendes geschrieben:
und wie heißt deine funktion jetzt :shock:

Sorry. Oben hab ichs gemacht, beim letzten Post nicht.

Korregiere gelich den oberen Post.
Born-to-Frag
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1094

Win XP SP2, Win 2000 SP4
Delphi 7, 2k5
BeitragVerfasst: So 04.06.06 17:04 
also bei mir funktionierts..

zeig doch mal den kompletten source wenn es nicht zu viel ist :?

_________________
Theorie ist wenn man alles weiß, aber nichts funktioniert. Praxis ist wenn alles funktioniert, aber niemand weiß warum.
Microsoft vereint Theorie und Praxis: Nichts funktioniert und niemand weiß warum.
Katzenstreu Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39

Windows XP Professional

BeitragVerfasst: So 04.06.06 17:08 
user profile iconBorn-to-Frag hat folgendes geschrieben:
also bei mir funktionierts..

zeig doch mal den kompletten source wenn es nicht zu viel ist :?

Bitteschön: www.delphi-forum.de/download.php?id=3484
Einloggen, um Attachments anzusehen!
Born-to-Frag
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1094

Win XP SP2, Win 2000 SP4
Delphi 7, 2k5
BeitragVerfasst: So 04.06.06 17:12 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
var
  x,xx,y,yy,s,ss:Integer;


implementation

..

constructor TQuadrat1.Create(xx,yy,ss:Integer);


Naja, du hast einmal local xx,yy,ss und global. das ist schonmal schlecht

und dann sollte es auch klar sein, warum ein falsches ergebnis herrauskommt

_________________
Theorie ist wenn man alles weiß, aber nichts funktioniert. Praxis ist wenn alles funktioniert, aber niemand weiß warum.
Microsoft vereint Theorie und Praxis: Nichts funktioniert und niemand weiß warum.
Katzenstreu Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39

Windows XP Professional

BeitragVerfasst: So 04.06.06 17:19 
user profile iconBorn-to-Frag hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
var
  x,xx,y,yy,s,ss:Integer;


implementation

..

constructor TQuadrat1.Create(xx,yy,ss:Integer);


Naja, du hast einmal local xx,yy,ss und global. das ist schonmal schlecht

und dann sollte es auch klar sein, warum ein falsches ergebnis herrauskommt


Okay. Habe nun eineige doppelte Deklarationen entfernt. Trotzdem gehts noch nicht! :(

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
unit Unit1;

interface


uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
 TQuadrat1 = class
 Quad:TRect;
 constructor Create (xx,yy,ss:Integer);
 procedure Erscheinen;
 procedure Bewegen;
 procedure Verschwinden;
 end;




  TForm1 = class(TForm)
    B_Erscheinen: TButton;
    B_Bewegen: TButton;
    B_Verschwinden: TButton;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure B_ErscheinenClick(Sender: TObject);
    procedure B_BewegenClick(Sender: TObject);
    procedure B_VerschwindenClick(Sender: TObject);



  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  x,y,s:Integer;
  Quadrat1:TQuadrat1;
implementation



{$R *.dfm}

constructor TQuadrat1.Create(xx,yy,ss:Integer);
begin
x:=xx;
y:=yy;
s:=ss;
end;

procedure TQuadrat1.Erscheinen;
begin

 Quad:=Rect(x,y,x+s,y+s);
 Form1.Canvas.Pen.Color:=ClBlack;
 Form1.Canvas.Rectangle(Quad);
end;

procedure TQuadrat1.Bewegen;
var i:Integer;
begin
 for i:=x to Form1.ClientWidth-s-x do
  begin
   Quad:=Rect(i-1,y,i-1+s,y+s);

   Form1.Canvas.Pen.Color:=Form1.Color;
   Form1.Canvas.Rectangle(Quad);

   Quad:=Rect(i,y,i+s,y+s);

   Form1.Canvas.Pen.Color:=ClBlack;
   Form1.Canvas.Rectangle(Quad);

   Sleep(2);
  end;
end;

procedure TQuadrat1.Verschwinden;
begin
 Form1.Canvas.Pen.Color:=Form1.Color;
 Form1.Canvas.Rectangle(Quad);
end;


function Flaeche(a:real):real;
begin
result:=s*s;
end;

procedure TForm1.FormCreate(Sender: TObject);
var a:real;
begin
 Quadrat1:=TQuadrat1.Create(90,10,50);
 Form1.Edit1.Text:=FloatToStr(a);
end;




procedure TForm1.B_ErscheinenClick(Sender: TObject);
begin
 Quadrat1.Erscheinen;
end;

procedure TForm1.B_BewegenClick(Sender: TObject);
begin
 Quadrat1.Bewegen;
 end;

procedure TForm1.B_VerschwindenClick(Sender: TObject);
begin
 Quadrat1.Verschwinden;
end;






end.
Born-to-Frag
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1094

Win XP SP2, Win 2000 SP4
Delphi 7, 2k5
BeitragVerfasst: So 04.06.06 17:29 
also ich versteh jetzt nicht genau was die Funktion Flaeche() soll. Man muss einen Parameter angeben der aber in der Funktion nicht benutzt wird? Warum?

_________________
Theorie ist wenn man alles weiß, aber nichts funktioniert. Praxis ist wenn alles funktioniert, aber niemand weiß warum.
Microsoft vereint Theorie und Praxis: Nichts funktioniert und niemand weiß warum.
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: So 04.06.06 17:30 
Ein Funktion sprichst du über ihren Namen an.
Also muss es lauten Form1.Edit1.Text:=FloatToStr(flaeche(s)); ;)
Katzenstreu Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39

Windows XP Professional

BeitragVerfasst: So 04.06.06 17:31 
user profile iconNoTVerYEvilPzYchO hat folgendes geschrieben:
Ein Funktion sprichst du über ihren Namen an.
Also muss es lauten Form1.Edit1.Text:=FloatToStr(flaeche(s)); ;)


Gleichmal testen :? :o
Katzenstreu Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39

Windows XP Professional

BeitragVerfasst: So 04.06.06 17:38 
Warum muss hinter funcrtion z.B. (a:real) stehen?

Ich benutze a doch garnicht. Das sagt mit Delphi auch. Aber nun läuft das Programm!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function Flaeche(a:real):real;
begin
result:=s*s;
end;

procedure TForm1.FormCreate(Sender: TObject);
var a:real;
begin
 Quadrat1:=TQuadrat1.Create(90,10,50);
 Form1.Edit1.Text:=FloatToStr(Flaeche(s*s));
end
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: So 04.06.06 17:41 
wenn du keine parameter angibst, kannst du dir in dem Fall theoretisch die Funktion auch sparen und
ausblenden Delphi-Quelltext
1:
Form1.Edit1.Text:=FloatToStr(s*s);					
schreiben ;)
Katzenstreu Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39

Windows XP Professional

BeitragVerfasst: So 04.06.06 17:43 
user profile iconNoTVerYEvilPzYchO hat folgendes geschrieben:
wenn du keine parameter angibst, kannst du dir in dem Fall theoretisch die Funktion auch sparen und
ausblenden Delphi-Quelltext
1:
Form1.Edit1.Text:=FloatToStr(s*s);					
schreiben ;)


Ja, könnte ich. Aber unser Lehrer möchte die Funktionen einführen. Auch wenns in diesem Beispiel sinnlos ist.