What is the output of the following.....

func(const int var)
{var++}
int main()
{
int x=10;
func(x);
cout<<x;
}
several errors.
the output is:
if you wanna change your argument inside a function, don't use const for passing the argument. and use int & instead of int
1
2
3
4
5
6
7
8
9
10
func(int var)//Constants can not be changed so to attempt to will create some sort of error or compiler problem. Remove the const.
{var++;
return var;}//return is necessary

int main()
{int x;//I am a noob at c++ so I'm not sure if you can declare and assign a variable at the same time in this language. To be safe I split it into 2 steps.
x=10;
x=func(x);//you need to use the func return somewhere
cout<<x;
return 0;}// Again return is necessary. Also without any method of pausing before the return it will immediately close the program so you won't get to see the output. 


[code]<The way to create a code box is to use these>[/code]

program
creates x
assigns 10 to x
sends x to the fun
increases var by 1
returns variable
assigns funct return to x
outputs x
ends program

Output
11
Last edited on
1
2
3
4
5
6
7
int func(int x){return x++;}

int main(){
    int x = 10;
    cout << func(x);
    return 0;
}


This should return 11 every time.
Topic archived. No new replies allowed.