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