Return X

+1 Erjon Morina · January 24, 2015
I'm a starter at c++ and tonight I started working with functions and somehow I can't understand how this function works, exactly how  the return x works because when I debug the program the results is y=4 and I don't know how it is calcuted.

thanks a lot to anyone who replies...


/images/forum/upload/2015-01-24/b33e570ed6ab4b939cb3951c8dfbd8b4.jpg

Post a Reply

Replies

Oldest  Newest  Rating
0 Savad Dekrup · January 25, 2015
#include <iostream>
using namespace std;

double shuma(int a, int b);
int main(){
double y;
y=shuma(2,3) + shuma(1,3);
cout << "y=" << y << "\n";
}
double shuma(int a, int b){
double x = 2;
double y = 0;
y = a + b;

return x;
}

Like what  Krootushas Gesu said, 
shuma(int a, int b); //returns 2


because you are returning x not the y.

double shuma(int a, int b){
double x = 2;
double y = 0;
y = a + b;

return x;
}

y = a + b;

return x; // x = 2

y=shuma(2,3) + shuma(1,2);

basically means
y = 2 + 2;

So, the result is 4
  • 1

C++

107,126 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator