Tracing output confused at some points!

i am having some real difficulties in finding out the output in the following code i tried tracing it however i kept getting the wrong output at the second time i call a function. i added where i am facing difficulties as comments in the program to further elaborate!



#include <iostream>
#include<cmath>
#include<fstream>
using namespace std;
int x = 3;
void f1(int, int &);
int f2(int); // function f2 job is to just assign x to 6
int f3(int);
int f4(int);

int main()

{
int x = 5; int y = 10;
f1(x, y);
cout << x << "\t" << y << endl;
x = 15; y = 20;
f1(x++, x); // for example what will x++ increase? and what will happen
// to the second (x) shouldnt it only accept (y)?

cout << x << "\t" << y << endl;
x = 3;
cout << f4(x) << endl;
cout << f3(f2(5)) << endl; //from here i really got lost
cout << f2(f3(5)) << endl;
{
int x = 50;
cout << ::x << endl;
}


system("pause");
return 0;
}

void f1(int a, int &b)
{
a *= 2; b += x;
cout << a << "\t" << b << endl;
}
int f2(int x)
{
static int z = 5;
x = z++;
return x;
}

int f3(int y)
{
y += x;
return x * y;
}
int f4(int n) {
if (n == 1 || n == 0) // if its not equal to 1 and 0 it will skip to
//else?
return n;
else
return n + f4(n - 1);
}de]
Put the code you need help with here.
[/code]
f1(x++, x); // for example what will x++ increase? and what will happen
Pre-C++17, this was undefined behavior. Post-C++17, I don't know what the rules are but it's probably at least unspecified behavior.
https://stackoverflow.com/questions/11060968/using-the-post-increment-in-function-arguments

In other words, don't do this. Whoever assigned this is trying to trick you for no good reason. Get clarification from your instructor for what he expects here, I guess.

// to the second (x) shouldnt it only accept (y)?
Not sure what you mean by this. Note that the name of the variable on the calling side can be different from the name of the variable inside the function itself.

if (n == 1 || n == 0) // if its not equal to 1 and 0 it will skip to
//else?
Yes.
Last edited on
" // to the second (x) shouldnt it only accept (y)?
Not sure what you mean by this. Note that the name of the variable on the calling side can be different from the name of the variable inside the function itself."

sorry i am slightly confused what will happen inside the function?

why when call f4 for the first time its true?
Last edited on
Topic archived. No new replies allowed.