Thread: Passing Values From Functions

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    44

    Passing Values From Functions

    I'm pretty new to coding so I have a pretty simple problem but I can't quiet figure it out. I'm writing a program that is suppose to pass values through the main ( ) function to other functions but every time I run it I keep getting an error saying that my variable is trying to be used without being initialized. How do I pass the values to the main ( ) function without actually having them run in the main ( ) function if that makes sense.

    Any feedback would be great

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Post the code that is causing problems, then ask specific questions based on the posted code.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    I'm trying to use the main ( ) function to drive my other functions pretty much. The error is that the "numValues" is being intialized without being defined. How do I tell the program the value of "numValues" comes from the function "int num_fahr_temps (void)"?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int num_fahr_temps (void);
    void input_fahr_temps (double array [], int numValues);
    
    int main (void)
    {
        double array [25];
        int numValues;
    
        input_fahr_temps (array, numValues);
        return 0;
    }
    
    int num_fahr_temps (void)
    {
        int numValues;
    
        printf ("        CIS 231 - Assignment 3 - Brian Avrit \n\n");
    
        printf ("How many fahrenheit tempetures would you like to enter (1-25) : ");
        scanf ("%i", &numValues);
    
        while ((numValues < 1 ) || (numValues > 25))
        {
            printf ("\nValue is out of range, please re-enter another value : ");
            scanf ("%i", &numValues);
        }
    }
    
    void input_fahr_temps (double array[], int numValues)
    {
        int i;
    
        for (i = 0; i < numValues; i++)
        {
            printf ("\nEnter value for fahrenheit %i: ", i);
            scanf ("%lf", &array[i]);
            if ((array[i] < -175.0) || (array[i] > 175.0)) { 
                printf ("\nValue is out of range,  please re-enter: ");
                scanf ("%lf", &array[i]);}
        }    
    }
    Last edited by skmightymouse; 03-24-2012 at 01:38 PM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You will need to initialize this variable in main. You initialize a variable by giving it a value. What is the purpose of this variable? What value do you want this variable to have?

    Jim

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    The "numValues" variable can change depending on what the user inputs so how would I tell the main ( ) function to look at the num_fahr_temps function for the value of "numValues"?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Yes, as long as you return this value from your function. Right now you are not returning anything from this function which is bad. You told the compiler you would be returning something but you are not returning anything. Don't lie to the compiler if you tell it you will return a value return a value. To fix your lie the compiler is taking some semi-random value and returning it to your calling function.

    Jim

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    So how would I return the value to the main function?

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    In main:

    numValues = input_fahr_temps (array);

    And as the last line in num_fahr_temps:

    return numValues;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    I inserted numValues = input_fahr_temps (array); into the main function but I'm getting a couple more errors.

    It gives me red squiggles under the (array) part of the code and says that there are too many arguments in function call.

    Also, is it possible to pass the value of a double variable back to the main function? If so could you hint me in the right direction?

    Thanks

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    At no point, in any of your functions, do you initialise numValues to anything.

    In main(), numValues is uninitialised (meaning that its value could be anything). You then pass that value to input_fahr_temps, and use it to control a loop. If the value happens to be zero, the loop body will never execute. If it happens to by 5000, the code will happily (attempt to) prompt you for 5000 values. Which is a problem as the dimension of the array passed is 25: reading 5000 values into an array with 25 elements will simply overwrite random areas of memory. (Formally, even the act of accessing the value of an uninitialised variable is undefined behaviour, which means ANYTHING is allowed to happen when you do that).

    Note also that the numValues defined inside num_fahr_temps() - which you haven't called at all - has no relationship to the numValues defined in main(). They are in what is called different scopes (aka context where they have meaning): the fact they have the same name does not mean changing one will change the other. So, if you decide to call num_fahr_temps(), you need to change that function so it returns the value it reads in back to main.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by skmightymouse View Post
    I inserted numValues = input_fahr_temps (array);
    Why are you returning numValues from input_farh_temps when you get numValues from
    Code:
    int num_fahr_temps (void)
    and input_farh_temps needs to make use of numValues?

    Perhaps you should post the updated code.

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Yes you are right, I meant to type num_fahr_temps.

    Here is the updated code I have so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int num_fahr_temps (void);
    void input_fahr_temps (double array [], int numValues);
     
    int main (void)
    {
        double array [25];
        int numValues;
    
        numValues = num_fahr_temps (array);
     
        input_fahr_temps (array, numValues);
        return 0;
    }
     
    int num_fahr_temps (void)
    {
        int numValues;
     
        printf ("        CIS 231 - Assignment 3 - Brian Avrit \n\n");
     
        printf ("How many fahrenheit tempetures would you like to enter (1-25) : ");
        scanf ("%i", &numValues);
     
        while ((numValues < 1 ) || (numValues > 25))
        {
            printf ("\nValue is out of range, please re-enter another value : ");
            scanf ("%i", &numValues);
        }
    return numValues;
    
    
    }
     
    void input_fahr_temps (double array[], int numValues)
    {
        int i;
     
        for (i = 0; i < numValues; i++)
        {
            printf ("\nEnter value for fahrenheit %i: ", i);
            scanf ("%lf", &array[i]);
            if ((array[i] < -175.0) || (array[i] > 175.0)) { 
                printf ("\nValue is out of range,  please re-enter: ");
                scanf ("%lf", &array[i]);}
        }    
    }
    Last edited by skmightymouse; 03-25-2012 at 12:48 AM.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Seems basically okay, except that num_far_temps() does not accept an argument, so should not be called with one.

    No need to return an array from intput_fahr_temps ... when you pass a non-const array to a function (as a pointer) any changes made to array elements are visible to the caller.

    In input_fahr_temps() probably want to use a while() loop to ensure elements of the array are valid (although why you've chosen constraits -175 to 175 escapes me).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Ok, thanks that part works perfect.

    Thank you for the suggestion to change my if statement to a while statement.

    Those are the constraints because thats what the instructions say they need to be on my assignment.

    Now I've expanded my code a little bit and I'm having a similar problem as the one before except I'm trying to pass a double instead of a integer.

    I want to pass the value of the average onto another function that will decide how many values are above and below the value.

    I want to pass the value with the function "double avg_function" but how is the function know to look at the array for the values it needs to calculate the average?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int num_fahr_temps (void);
    double avg_function (void);
    void input_fahr_temps (double array [], int numValues);
    void output_temps (double array [], int numValues);
    void avg_high_low (double array [], int numValues);
    
    int main (void)
    {
        double array [25], avg;
        int numValues;
    
        numValues = num_fahr_temps ();
        avg = avg_function ();
    
        input_fahr_temps (array, numValues); 
        output_temps (array, numValues);
        avg_high_low (array, numValues);
        abo_bel_equ (array, numValues, avg);
    
        return 0;
        
    }
    
    int num_fahr_temps (void)
    {
        int numValues;
    
        printf ("\n        CIS 231 - Assignment 3 - Brian Avrit \n\n");
    
        printf ("How many fahrenheit tempetures would you like to enter (1-25) : ");
        scanf ("%i", &numValues);
    
        while ((numValues < 1 ) || (numValues > 25))
        {
            printf ("\nValue is out of range, please re-enter another value : ");
            scanf ("%i", &numValues);
        }
    
        return numValues;
    }
    
    void input_fahr_temps (double array[], int numValues)
    {
        int i;
    
        for (i = 0; i < numValues; i++)
        {
            printf ("\nEnter value for fahrenheit %i: ", i);
            scanf ("%lf", &array[i]);
            while ((array[i] < -175.0) || (array[i] > 175.0)) 
            {
                printf ("\nValue is out of range,  please re-enter: ");
                scanf ("%lf", &array[i]);
             }
        }    
    }
    
    void output_temps (double array[], int numValues)
    {
        int i;
    
        printf ("Fahr\n");
    
        for (i = 0; i <numValues; i++)
            printf ("%.1lf\n", array [i]);
    }
    
    void avg_high_low (double array [], int numValues)
    {
        int i;
        double avg, high, low, sum = 0;
        
        high = array[0];
        low = array[0];
    
        for (i = 0; i < numValues; i++)
            sum = sum + array [i];
            avg = sum / numValues;
    
        for (i = 0; i < numValues; i++)
            if (array[i] < low) 
                low = array[i]; 
        for (i = 0; i < numValues; i++)
            if (array[i] > high)
                high = array[i];
    
        printf ("\nAverage: %.1lf\n", avg);
        printf ("\nHigh: %.1lf\n", high);
        printf ("\nLow: %.1lf\n", low);
    }
    
    double avg_function (void)
    {
        int i;
        double avg;
    
        for (i = 0; i < numValues; i++)
            sum = sum + array [i];
            avg = sum / numValues;
    
        return avg;
    }
    
    void abo_bel_equ (double array[], int numValues, double avg)
    {
        int i, abo = 0, bel = 0, equ = 0;
    
        for (i = 0; i < numValues; i++)
            if (array[i] > avg) 
                abo++;
                printf ("\nAbove Average: %i \n", abo); 
                
        for (i = 0; i < numValues; i++)
            if (array[i] < avg) 
                bel++;
                printf ("Below to Average: %i \n",bel); 
    
        for (i = 0; i < numValues; i++)
            if (array[i] == avg) 
                equ++;
                printf ("Equal to Average: %i \n", equ); 
    }
    Last edited by skmightymouse; 03-25-2012 at 01:15 AM.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by skmightymouse View Post
    Now I've expanded my code a little bit and I'm having a similar problem as the one before except I'm trying to pass a double instead of a integer.
    Code:
    int num_fahr_temps (void);
    double avg_function (void);
    void input_fahr_temps (double array [], int numValues);
    void output_temps (double array [], int numValues);
    void avg_high_low (double array [], int numValues);
    You need to prototype abo_bel_equ so it knows what to expect as parameters and return value.
    Last edited by Cynic; 03-25-2012 at 01:36 AM. Reason: additional info

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  3. Beginner question: passing values between functions
    By merike in forum C Programming
    Replies: 5
    Last Post: 11-18-2006, 08:39 PM
  4. functions passing values
    By srinurocks in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 11:49 PM
  5. Passing Values to Functions
    By shad0w in forum C Programming
    Replies: 2
    Last Post: 12-25-2001, 08:28 PM