coisox 0 Report post Posted September 7, 2007 Using c++:int main(){ cout << " Sum is: " << funct(5);}int funct(int y){ if (y==1 ) return 1; else return y++;}Dari program nih, secara logiknye, bagi aku jawapan ialah "sum is: 6". Tapi yg buat aku tak paham, bila aku compile jawapan ialah "sum is: 5". Boley sesaper tolong explain kat aku, kenapa proses penambahan tu tak berlaku. Tak pahamlah! Proses penambahan akan berlaku sekiranya the last two statement (program kat atas) tukar kepada: else y++; return yTolooooooonggggg....... Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted September 7, 2007 cuba guna,return ++y; Quote Share this post Link to post Share on other sites
Paralys3r 0 Report post Posted September 8, 2007 return y++; basically means return y; y++; //or ++y; it doesn't matter now. Quote Share this post Link to post Share on other sites
mchammer 0 Report post Posted September 8, 2007 Ini dinamakan pre/post increment.Contoh:1) jumlah = y++;Assign value y ke variable jumlah, lepas tu baru lakukan proses increment.2) jumlah = ++y;Lakukan proses increment, lepas tu simpan hasil increment y ke variable jumlahpre/post decrement pun macam tu jugak.So. Kalau buat:return y++;Maknanya kita just return value y sebelum proses increment. Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted September 8, 2007 return y++; basically means return y; y++; //or ++y; it doesn't matter now. it does matter. consider funct(int y) { Â Â Â Â int i = 2; Â Â Â Â int j = 3; Â Â Â Â return y+i+j; // if y = 5, return value should be 10 }compiler akan accumulate hasil tambah semua tu baru return,sebab y++ tak incremented sebab operator tu post-increment. kalau pre-increment, program akan execute increment value dulu baru return call with value in eax (x86). Quote Share this post Link to post Share on other sites
Paralys3r 0 Report post Posted September 8, 2007 (edited) please use the context above.after return statement.. it really doesn't matter whether you use post- or pre- increment. Maknanyareturn y; y++; dan return y; ++y; is just the same thing. Kalau convert directly ke machine code ia akan jadi (secara ringkasnya ) mov eax, [esp+4] cmp eax, 1 jnz __else ret __else: ret inc eaxBermakna inc eax TAK AKAN DILAKSANAKAN. Anything after return statement is useless kalau takde branch pergi ke sana untuk dilaksanakan. Edited September 8, 2007 by Paralys3r Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted September 8, 2007 ok.. aku post tadi sebab salah faham. oleh itu, semua sepatutnya sudah jelas bagi thread starter. Quote Share this post Link to post Share on other sites