Gamestudio Links
Zorro Links
Newest Posts
Purchase A8 full licence version
by ukgamer. 04/29/26 18:09
Z9 getting Error 058
by k_ivan. 04/25/26 19:13
ZorroGPT
by TipmyPip. 04/25/26 16:09
Stooq now requires an API key
by jcl. 04/13/26 09:42
Strange "Alien" Skull created with >Knubber<
by NeoDumont. 04/10/26 18:58
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
0 registered members (), 5,733 guests, and 39 spiders.
Key: Admin, Global Mod, Mod
Newest Members
ukgamer, valino, juergenwue, VladMak, Geir
19210 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
int function() returns wrong value #264772
05/08/09 04:16
05/08/09 04:16
Joined: Apr 2009
Posts: 248
Philippines
seecah Offline OP
Member
seecah  Offline OP
Member

Joined: Apr 2009
Posts: 248
Philippines
Hi Guys,

Could anyone please answer my question..

I have these codes that return a very unexpected result.

For example:
Code:
int i;
void main()
{
  i = sampleFunction();
}

int sampleFunction()
{
  int nReturn;
  if (blahahahaha)
    nReturn++;
  
  return nReturn;

}


I can't get the expected value of the return variable "nReturn", As I debug the problem, can get the correct incrementation of nReturn but when I tend to assign the value to i it will give a value of 68096..

What's wrong with this..



Can't is not an option™
Re: int function() returns wrong value [Re: seecah] #264777
05/08/09 05:38
05/08/09 05:38
Joined: Jul 2008
Posts: 553
Singapore
delinkx Offline
User
delinkx  Offline
User

Joined: Jul 2008
Posts: 553
Singapore
u need to assign an initial value to nReturn first before incrementing it. wat is happening here is a random value is getting assigned to nReturn.

Code:
int i;

int sampleFunction()
{
  int nReturn = 0;
  if (blahahahaha)
    nReturn++;
  
  return nReturn;

}

void main()
{
  i = sampleFunction();
}



A7 commercial Team / VC++ 2008 Pro
homepage | twitter | facebook
Re: int function() returns wrong value [Re: delinkx] #264784
05/08/09 06:19
05/08/09 06:19
Joined: Apr 2009
Posts: 248
Philippines
seecah Offline OP
Member
seecah  Offline OP
Member

Joined: Apr 2009
Posts: 248
Philippines
Actually 0 is being assigned on declaration.. this is the actual code:

Code:

[color:#FF0000]int nMaxObjects = 0;[/color]
int ReadLevelBricks()
{
	var fileHandle, strLen, nCounter, layerValue;
	VECTOR brickPos;
	PANEL* panBrick;
	fileHandle = file_open_read("Level1.txt");
	brickPos.x = initBrickX;
	brickPos.y = initBrickY;
	[color:#CC0000]int nBrickCounter = 0;[/color]
	wait(3);
        if (fileHandle)
	{
		while(file_str_read(fileHandle, strNumBricks) != -1)
		{
			strLen = str_len(strNumBricks);
			layerValue = 1;
			
			for (nCounter=0; nCounter< strLen; nCounter++)
			{
				str_cpy (strTemp, strNumBricks);					//store the data being read to a temporary string
				str_clip(strTemp, nCounter);						//trim left
				str_trunc(strTemp, strLen - nCounter - 1);	//trim right
//if statetements here
	}
			
			brickPos.y += brickSpacing;
			brickPos.x = initBrickX;
			brickPos.y += brickHeight;	//still need to add vertical space
		}
	}
	return nBrickCounter;
	file_close(fileHandle);
void main()
{
	video_set(1024, 768, 32, 2);	
	fps_max=80;	
	mouse_pointer=0;	
	vec_set(screen_color, vector(178, 153, 128));
	nMaxObjects = ReadLevelBricks();
	
}


nMaxObjects here is not what is expected..



Can't is not an option™
Re: int function() returns wrong value [Re: seecah] #264785
05/08/09 06:42
05/08/09 06:42
Joined: Jul 2008
Posts: 553
Singapore
delinkx Offline
User
delinkx  Offline
User

Joined: Jul 2008
Posts: 553
Singapore
post the whole code of the ReadLevelBricks() function so tat i can trace through. here some codes are missing.


A7 commercial Team / VC++ 2008 Pro
homepage | twitter | facebook
Re: int function() returns wrong value [Re: delinkx] #264937
05/08/09 21:27
05/08/09 21:27
Joined: Dec 2008
Posts: 271
Saturnus Offline
Member
Saturnus  Offline
Member

Joined: Dec 2008
Posts: 271
Code:
int nBrickCounter = 0;
wait(3);
...

You may not call wait() before returning a value.
The calling function will _not_ wait until a value is returned, but receive a wrong (probably undefined) return value instead.

BTW:
Code:
return nBrickCounter;
file_close(fileHandle);

The file should be closed before returning anything.
Obviously the closing brace of the function misses here, too (perhaps due to copy & paste?).


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1