Why does the output value always equal 1?

Hello, I am sorry for a basic question, however, I can not figure out why this function obtains the value of 1 for n. Here is the function:

int a = 0;
int b = 0;
int f(int c)
{
int n = 0;
a = c;
if (n < c)
n = a + b;
return n;
}

If the parameter "int c" did not have a declared value I assumed 'c' would become any random number. Even if I give c a value such as 10 or 15, n will still return 1. Same thing if I were to change "int a" or "int b" values. I don't understand how the function always results in 1.
Please help me understand what goes on behind the scenes to get this value.
Last edited on
You don't show how you do call this function, nor what you do with the returned value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>

using namespace std;

int a{ 0 } , b{ 0 } ;

int f( int c ) ;

int main()
{
	cout << f( 10 ) <<'\n' ;
	return 0;
}

int f( int c )
{
	int n = 0;
	a = c;
	if (n < c)
		n = a + b;
	return n;
}



10
Program ended with exit code: 0
Hello urby,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I am guessing the program might look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int a = 0;
int b = 0;

int f(int c)
{
	int n = 0;
	a = c;
	if (n < c)
		n = a + b;
	return n;
}

int main()
{
	int c{ 2 };

	std::cout<<f(c);

	return 0;
}


This returns and prints the value I give to "c".

If the parameter "int c" did not have a declared value I assumed 'c' would become any random number.

You assume wrong.

Let me say:

It is ALWAYS a good practice and programming to initialize your variables. If your compiler is using the C++11 standards or after the easiest way to initialize variables is with empty {}s, e.g., int num{};. This will initialize the variable to 0 (zero) or 0.0 for a double. A "char" will be initialized to "\0". "std::string"s are empty to start with an do not need initialized. Should you need to you can put a number between the {}s.You can also Initialize an array, e.g., int aNumbers[10]{};. This will initialize all elements of the array to 0 (zero). A use of int aNumbers[10]{ 1 }; will initial the first element to "1" and the rest of the array to "0".. Following the "1" with ", 2 ..." will initialize the array with the numbers that you have used up to the entire array.

In the case of the above program if "c" was not initialized it would contain whatever was at the memory location when the variable was defined. On my computer this number for an "int" is usually "-858993460". Not what you want. You could call it a random number, but it is actually garbage because this number could change. And different variable types would interpret the information differently do to the number of bytes of the variable type.

With out seeing your whole code I can not say why it always returns 1 since I do not know what was done with "c" before it is sent to the function. This tends to go with what keskiverto said.

Hope that helps,

Andy
Hello urby,

Tried your program and it works. Still do not see what the problem is?

Unless you use a negative number you will always end up with n = a + b; or n = c + 0 which will be the value of "c" that came from the function call. Otherwise it will return "n" which is set to zero.

Andy
Thank you all for your reply.
This was the code for my problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;
int a = 0;   
int b = 0;  
int f(int c) 
{
int n = 0;
a = c;
if (n < c)
n = a + b;
return n;
}
int g(int c)
{
int n = 0;
int a = c;
if (n < f(c))
n = a + b;
return n;
}
int main()
{
int i = 1;
int b = g(i);
cout << a + b + i << "\n";
return 0;
}

I understand that i =1 and is then inserted to the function for parameter c.
I was just messing with the code and chopped the entire code into a snip-it like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int a = 0;   
int b = 0;  
int f(int c) 
{
int n = 0;
a = c;
if (n < c)
n = a + b;
return n;
}

int main()
{
cout << f << endl;
return 0;
}

when I run this on my compiler f returns 1, which is what lost me. It has both variables declared as 0 and c is undeclared.
Does this value come from the predicate "1 is true" and "0 meaning false"?
Thank you for finally showing the relevant code.

A compiler says:
 In function 'int main()':
18:9: warning: the address of 'int f(int)' will always evaluate as 'true' [-Waddress]


Lets make it simpler:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int foo( int ) 
{
  return 42;
}

int main()
{
  std::cout << foo << std::endl;
  std::cout << std::boolalpha << foo << std::endl;
  std::cout << foo( 7 ) << std::endl;
  return 0;
}









1
true
42

The plain foo is a name of function and is a pointer to that function. An address. The compiler says that it will always convert address of a function into bool value true.

The output of bool does print out by default as 0 or 1.
The output can be formatted to words 'false' and 'true'.

The foo( 7 ) is a function call. The function is called and the value returned by the function is then passed to the ostream::operator<<.
Topic archived. No new replies allowed.