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)
}