# Thread: How to Get a Variable from my MAIN function to another function?

1. ## How to Get a Variable from my MAIN function to another function?

I am defining a variable n using scanf in my main function. will the rest of my program know it?

I am solving an equation and have a square root part of my function that sends back the square root value to my main function.

Code:
```double getSquareRoot ( void );

int main(void)
{
double n_factorial; // powers multipled by the solved square root

printf("ESTIMATE A FACTORIAL NUMBER\n");
printf("This program was written by %s.\n", PROGRAMMER_NAME);

printf("Please enter a positive integer: "):
scanf("%lf", &n);

sqrt_partial = (2.0 * n) + (1.0 + 3.0);

sqrt_solved = getSquareRoot( );

n_factorial = pow(n,n) * exp(-n) * sqrt_solved;
printf("%.0f! equals approximately %.5f \n", n, n_factorial);

return EXIT_SUCCESS;
}

double getSquareRoot (void)
{
double n; // User defines the factorial
double sqrt_partial;
double sqrt_solved;

printf("Please enter a positive integer: "):
scanf("%lf", &n);

sqrt_partial = (2.0 * n) + (1.0 / 3.0);

sqrt_solved = sqrt(sqrt_partial * PI);

retun sqrt_solved;
}```

2. You have to learn what the arguments of a function are.
You wrote
Code:
`double getSquareRoot (void)`
,where the void declares that this function has no arguments at all.When you want to pass an argument you have to write something like this
Code:
`double getSquareRoot (typeOfArgument argumentName)`
In your case you have a variable called n...but oops!You have not declare it!This is not f#,you have to say to the compiler the type of the variable.int is a probably good choice for n in this case.
So you can write
Code:
`double getSquareRoot (int n)`
Now n is visible by the body of getSquareroot.In other words ,n lives now and in the world of the function(not only in the world of main).

You could also set is a global variable ,but this is a really bad practice,especially if you are going to write a large program in the future.

3. > n_factorial = pow(n,n) * exp(-n) * sqrt_solved;
You see how you use n here.

> sqrt_solved = getSquareRoot( );