1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Posts
23
Rep Power
0

Quick question about functions returning arguments

Code:
int square(x)
{
int result;
result=x*x;
printf("The square of %d is: %d",x,result);\

return result;
}

int main()
{
int x,final;
printf("Enter a number to find it's ****ing square!: "); scanf("%d", &x);
square(x);
final=result+1;
In the code above I defined a function that calculates the square of a number stored in the result variable. Then I returned it as shown on the last line in the function declaration.
Then I wanted to use it in the int function to calculate another variable final which is the result returned by the "square" function plus one. But I can't do it apparently it says it's an undeclared variable. Why is this happening? Shouldn't the square function return the value of result which I could use after? Sorry if I did not explain it very well :D and thanks for reading.
2. > Shouldn't the square function return the value of result which I could use after?
It does return a result.

But you ignore it.
> square(x);

So try something like
final = square(x) + 1;

> final=result+1;
You should follow the data path. You don't get the result simply by naming a variable with the same name used in the called function.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Posts
23
Rep Power
0
Than what is the diffrence if I don't include "return result; " in the function? Final has the same result for me.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
1
Rep Power
0
The function square() returns the value of result not the location of the variable. Upon exiting(returning) from the square() function, all local, automatic variables are destroyed. The only thing which remains is the value stored in the general registers. To use the value returned by function square() simply assign it to a variable as such:

int result = square(x);

*PS: the function return type of int is only capable of returning ints. to return floats simply change int square(){} to float square(){}.

Also just for aesthetic reasons move square to after the main function, and place a prototype of the function before main.
5. Result is local variable and it's scope is for square function only so you cant access that in main function.
and when you return something from function at that time only value is returned from function.
you have to collect that value in another variable.
6. From these responses you can distill a set of general rules to follow:

1. Local variables are only known inside the function in which they are declared. They are not known outside of that function.

2. If you name local variables in two different functions the same, then Rule #1 still applies. Those same-named variables in different functions have absolutely nothing whatsoever to do with each other.

3. In order to get a value out of a function you must do one of two things:
a. Return the value with a return statement.
or
b. Pass in a pointer as a parameter and assign the value to where that pointer is pointing.

4. The return value of a function is only available to the calling function within the expression in which the function call was made. Therefore, in order to use the return value of a function, you must either:
a. Use that value immediately.
or
b. Assign it to a variable that you can then use in subsequent statements.

So if you didn't understand the explanations, follow these rules until you do understand why these are the rules.