0

Is there any way that I can access function parameters after returning from the function (i.e. in the calling function)?

Example:

void add (float a , float b) {
    int c;
    c=a+b;
    return c;
}

void main () {

    add (3,4);

    if (a <4 && b<3) // a and b are variable in add functions
        printf("a is 4 and b is 3");
}
godel9
  • 7,340
  • 1
  • 33
  • 53
user2828488
  • 65
  • 1
  • 8

6 Answers6

2

Once execution leaves the function, parameters are no longer accessible. You'll need to store them temporarily in the calling function to be able to access them again:

int main ()
{

    float a = 3.0f, b = 4.0f;
    add (a, b);

    if (fabs(a - 4.0f) < 0.005f && fabs(b - 3.0f) < 0.005f) // a and b are now local variables
        printf("a is 4 and b is 3");
}

See Scope vs. Lifetime of Variable for more information.

A few other things:

  1. The return type of main should be int.
  2. You're checking for a < 4 && b < 3 to see if a is equal to 4 and b is equal to 3, which isn't right.
  3. You should get in the habit of not checking floating-point values for equality. Better to check if they're near each other.
  4. Your add function should have a non-void return type, probably float. (Hat tip: @ajp15243)
  5. Although not illegal, it's normal to use the return value of a function somehow.
Community
  • 1
  • 1
godel9
  • 7,340
  • 1
  • 33
  • 53
1

Local variables a and b are destroyed when you exit the function, so - no, you cannot do this.

Also, you defined add as void, while it should be int (because it obviously returns c).

Eutherpy
  • 4,471
  • 7
  • 40
  • 64
0

No.

You need to understand scope and storage.

  • Scope of a variable defined in a function is restricted to that function.
  • Variable declared in a function is stored on a stack frame corresponding to the function call, and all these variables are destroyed as the function returns.
HAL
  • 3,888
  • 3
  • 19
  • 28
0

You can have functions return variables, you just got to change the word "void" to the variable you want to return. So for your case:

void add (float a, float b)

Will be

int add (float a, float b)

Since it looks like you want to return an int variable, but I think it would be better if you just have the return variable be a float.

John Odom
  • 1,189
  • 2
  • 20
  • 35
  • can you think of a _better_ option for what OP should use as a return type for `add()`? (adding two floats) – ryyker Nov 13 '13 at 15:50
  • I was only staying with his flow since he did have the variable c declared as an int. Of course returning float would be better, but it doesn't mean you have to do that :P – John Odom Nov 15 '13 at 21:20
0

Im not a C programmer but regardless of the language I think your current efforts are a little misguided. Dont get too caught up on what language youre using focus on writing well structured code because many concepts transcend the specific language. Most languages today are considered "Turning Complete" basically means what you can write in one you can write in another.

With that being said you have an add function that takes two arguments and then adds those two arguments and returns the value. Those arguments thatyou are passing are scoped within the add function block. And they are only reference for the parameters that you pass in when you actually call the function like you did add(4, 3).

So you can move your if conditional statement within you add function cause like I said when you call your add function the arguments will then have value and you can check it.

That is why you are able to say a + b. Cause when you call add those variables are what you pass into the function. Hope this makes sense. Happy coding!

elrick
  • 681
  • 4
  • 5
0

is there any way that I can use the values of the variables that are defined in function to be call in a program

No, Not the way you have it written

Would it not be more meaningful if your existing code (in original post) were modified in the following ways:
1) Return type must be float for add() function
2) In main(), there is no reason to test a and b, they are not in scope outside of add() function.
3) Use results of add() function in a printf statement to see results.
4) Not an absolute requirement, but recommended that minimum prototype for main() be int main(void)
5) IF you had created a copy of floats somewhere outside of any function, toward the top of the file, then their scope would have allowed you to use them in main(). But it is not good practice to create global variables having the same name as argument names used in functions within the same file.

(see in-line comments for explanations)

float add (float a , float b) {//must return float, you already have a return statement
    float c;//from int to float (because that matches the argument types being evaluated)
    c=a+b;
    return c;
}

float x = 5.0; //these are variables with global scope, meaning they exist and can be  used in every function
float y = 7.2;

int main (void) {//change from void main() to int main(void)
    float z=0;

    z = x + y;//legal statement summing globals x & y and storing in local z 

    //add (3,4);//used in printf below

    //if (a <4 && b<3) // a and b are variable in add functions (this is meaningless in context of an add function

    //call the add() function, with float arguments, from here, and print out the results in one step.   
    printf("results of add are: %f", add(3.0,4.0));//change from 3 to 3.0 (just to be anal)
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • Sorry for the bad example , I just want to ask that any way of calling a floating value 'a' and 'b' some where in main program .As I have assigned those values in separate function so can I call those values in a main program ? – user2828488 Nov 13 '13 at 18:55