-
New To Programming
Hello my name is Javier. Ive been going at this topic and my teacher always said to call her but she isn't responding so im asking you all. Im a beginner on programming. As of now I have an assignment that im not asking to help me cheat on or anything. Just a simple question, how do I make a function return integer values back to the main() function. I have tried much and it isn't working. I called the function and I thought the problem was the fact that I first called it void so nothing would return. I changed it to int and still nothing works in this situation. Can anyone help me. First post so any critisizm would benefit as well.
-
Show the code that is your best attempt.
-
Perhaps you are trying to pass the value back through the parameter list. By default, C and C++ always pass the function parameters by value, meaning that if they are modified within the function, those modifications are local. For instance, if you have the function foo:
Code:
void foo(int arg1, int arg2) {
arg1=2;
...
}
And you call it from the main routine like so:
Code:
int a=3;
foo(a, 5);
printf("%d\n", a);
you will get 3, not 2, as the result. There are two ways to pass by reference, one of which is a C++ extension. The old, C way of doing things, is:
Code:
void foo(int *arg1, int arg2) ...
Now you have to call it like this:
Code:
int a=3;
foo (&a, 5);
The newer, C++ way is:
Code:
void foo(int &arg1, int arg2) ...
in which case you can continue to call it in the usual way as in the very first example.
Hope this helps.