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

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    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. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > n_factorial = pow(n,n) * exp(-n) * sqrt_solved;
    You see how you use n here.

    > sqrt_solved = getSquareRoot( );
    How about trying
    sqrt_solved = getSquareRoot( n );
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function with variable function arguments
    By Kyl in forum C Programming
    Replies: 10
    Last Post: 12-21-2011, 02:56 PM
  2. Replies: 35
    Last Post: 12-01-2011, 08:31 PM
  3. Replies: 3
    Last Post: 09-26-2011, 04:44 AM
  4. Replies: 3
    Last Post: 06-01-2011, 03:08 AM
  5. function declaration in Main function
    By filipn in forum C Programming
    Replies: 2
    Last Post: 11-11-2009, 01:31 PM