hello,
is there any way to call a specific variable from a function ?
see the following program.

float abc(void);
float main(void)
{
float a,b,y,z;
a=abc();
b=
printf ("enter value for z");
scanf ("%f",&z);
y=(a-(a*y))/(b*z);
printf ("%f",y);
}
float abc(void)
{
float x,p;
scanf ("%f",&x);
p=x*x;
return(p);
}

in this program i want to assign the value of b as same as value of x , ( the value of x that is assigned in line 16 should be equal to b at line 6)
how can i call the value of x from the function ?

You can do a = b = abc(); Or change the code so that it reads a = abc( &b );

when i tried your way, it is printing the value of p and p is equal to square of x, i want to print the value of x for b that i've assigned in line 15 ( sorry , in my first post i wrote line number 16.)

>>float main(void)

main() does NOT return float -- only returns int

float abc(float *x)
{
   float p;
   scanf ("%f",x);
   p=*x* *x;
   return(p);
}

int main()
{
   float a,b;
   a = abc(&b);
   printf("a = %f\nb(x) = %f\n", a, b);
}

i didn't understand your program because we are at initial level of learning functions

The variable b in main() is passed to function abc() by reference, which means that any changes made to it in abc() will also be made in main(). The parameter x in abc() is a pointer to the variable b in main().

So if you want to print the value of x that is in abc() just print the value of b that is in main(). Even though they have different names, since x is a pointer, printing the value os *x is the same as printing the value of b. Clear as mud?

Add a line of code in abc() to print the value of x and you will see what I mean: printf("x = %d\n", *x); Note the star is required because x is a pointer.

commented: great :) +0

i've whatever you said but we haven't learn more than the basics of the functions (only learned how to create a function)
your program is really complicated because we haven't learn pointers ..and i really don't know where in your program you declare a function..because we've learned to use statement terminator at the end of line where we declare a function. i.e

int abc(void);

this is the declaration but in your program where is the declaration ?? sorry but we did'nt study more than that that is why i am asking these silly questions.

see this program..

int abc(void);
int main(void)
{
float a,b;
a=abc();
b=
printf ("%f",b);
}
int abc(void)
{
float x,p;
scanf ("%f",&x);
p=x*x;
return(p);
}

this is really simple code..how can i implement pointer in this code without any complications ?
when i am writing.

int abc(void);
int main(void)
{
float a,b;
a=abc(&b);
printf ("%f",b);
}
int abc(void)
{
float *x,p;
scanf ("%f",&x);
p=*x* *x;
return(p);
}

it is not working.

>>int abc(void);

That is called a function prototype, which is used to forward declare the function so that the compiler knows what it is, it's return value, and its parameters.

Function prototypes are unnecessary if the function actually appears before it is used, as in the example I posted. You may code the prototype, but it is not required in that case.

The code you posted does not compile because you did not code the function prototype correctly. Change it to this: int abc(float *b); Now change the actual function like this:

int abc(float *x)
{
float p;
scanf ("%f",x); // NO & symbol here because x is already a pointer
// Here is another way to code it
// maybe it makes more sense to you
// what it is doing.
p = *x;
return p * p;
}

Hey thanks alot for the help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.