Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
August 11th, 2013, 05:21 AM
 Cantafford
Registered User

Join Date: Jun 2013
Posts: 23
Time spent in forums: 1 h 35 m 16 sec
Reputation 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 and thanks for reading.

#2
August 11th, 2013, 05:25 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,254
Time spent in forums: 2 Months 4 Weeks 1 Day 13 h 11 m 30 sec
Reputation Power: 1809
> 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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#3
August 21st, 2013, 04:37 AM
 Cantafford
Registered User

Join Date: Jun 2013
Posts: 23
Time spent in forums: 1 h 35 m 16 sec
Reputation 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
August 21st, 2013, 04:59 AM
 ZachMathews
Registered User

Join Date: Aug 2013
Posts: 1
Time spent in forums: 46 m 10 sec
Reputation 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
August 21st, 2013, 05:35 AM
 eramit2010
Contributing User

Join Date: Mar 2013
Location: India
Posts: 95
Time spent in forums: 2 Days 18 h 10 m
Reputation Power: 3
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
August 21st, 2013, 11:21 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,849
Time spent in forums: 3 Months 1 Day 1 h 57 m 35 sec
Reputation Power: 2199
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.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Quick question about functions returning arguments