Thread: Why do variables equal functions?

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    Why do variables equal functions?

    I tried searching, and while I found some good info, it did not answer my question. I am fairly new in programming, so forgive me if my question is simplistic.

    We are writing a program that the only thing that main can do is call functions. I am not completely sure how to structure my functions when calling them.

    This code works, but I do not understand why I need to put "Temp=GetTemp();", and the two following lines in the function Celsius.

    Any help is appreciated.

    # include <stdio.h>

    void Celsius(void);
    float GetTemp(void);
    float CentToFaren(float);
    void PrintTemp(float);


    int main (void)
    {
    Celsius ();

    return 0;
    }


    void Celsius (void)
    {
    float Convert, Temp;

    printf("Convert Celsius to Farenheit: \n\n");

    Temp = GetTemp ();
    Convert = CentToFaren (Temp);
    PrintTemp (Convert);
    }


    float GetTemp (void)
    {
    float Temp;

    printf("Please enter the temperature to be converted: ");
    scanf("%f", &Temp);

    return Temp;
    }


    float CentToFaren (float Temp)
    {
    float Convert;

    Convert = 32 + (Temp * 180/100);

    return Convert;
    }


    void PrintTemp (float Convert)
    {
    printf("\n\nThe converted temperature is: %4.2f", Convert);
    }
    Last edited by Griffin2020; 03-22-2002 at 05:02 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They don't equal a function. They equal what the function returns.

    int myfunction( void ) { return 5; }

    int x;

    x = myfunction( );

    'myfunction' "return"s the value 5.

    It is up to you to decide what to do with the return value. We can ignore it:

    myfunction( );

    In this case, there is no harm in doing so. Or we can use it:

    x = myfunction( );

    This puts the value returned by the function into the variable named 'x'.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    That makes sense, but I guess I do not undestand what makes it tick completely.

    In this program, I am lost. it is a similar situation, main can only call other functions, and the other frunctions perform the actions. What I cannot get to work is passing my variables from one function to another.

    /*Determine a student's letter grade based on three input number grades,and calculating average and other factors */

    #include <stdio.h>

    void PrintHeading (void);
    int GetScores (int);
    float Average (float);
    int GetGrade (char);
    int PrintGrade (void);

    int main (void)
    {


    GetScores();
    Average(Score1, Score2, Score3);
    GetGrade(Avg);
    PrintGrade(Grade);

    return 0;
    }

    int GetScores (void)
    {
    int Score1, Score2, Score3;

    printf("Intput three scores, seperated by a space,then press [Enter].");
    scanf("%d %d %d", &Score1, &Score2, &Score3);

    return 0;
    }

    float Average (int Score1, int Score2, int Score3)
    {
    float Avg, Avg2;

    Avg = (Score1 + Score2 + Score3)/3;
    Avg2 = (Score2 + Score3)/2;

    return Avg;
    }

    int GetGrade (float Avg, Avg2, int Score3)
    {
    char Grade;

    if Avg >= 90
    Grade = A
    else if Avg >= 70 && Score3 > 90
    Grade = A
    else if Avg >= 70 && Score3 < 90
    Grade = B
    else if Avg >= 50 && Avg2 > 70
    Grade = C
    else if Avg >= 50 && Avg2 < 70
    Grade = D
    else Grade = F

    return Grade;
    }

    int PrintGrade (char Grade)
    {
    printf ("The students grade is: %s", Grade);
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to understand "scope".
    A variable is given a scope when created.
    Any variables defined in a fuction have a "local scope", meaning that they are local to that function. This means that when that function ends, (unless you're using 'static', which is another topic) then the variables are destroyed, and they're gone. Poof.

    Since that happens:
    Code:
    int GetScores (void) 
    { 
        int Score1, Score2, Score3; 
    
        printf("Intput three scores, seperated by a space,then press [Enter]."); 
        scanf("%d %d %d", &Score1, &Score2, &Score3); 
    
        return 0; 
    }
    When this funciton finishes, all of the 'Score' variables vanish. They're gone. Since they're gone, you can't use them in other functions.

    Youre 'getgrade' function is just entirely wrong. you have to use ( ) with an if statement:

    if( a < b )

    Furthermore, if you are returning a letter, or rather, if you're using just one single character, and you're not using the numerical value of that character, then you must enclose it in single quotes:

    return 'F';

    Not:

    return F; //because F could be a variable name.

    The single quotes let the compiler know you're using a "character constant" and not some variable name.

    Quah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    Thanks, I understand the scope, but if I declare

    int Score1, Score2, Score3 in main, then it tells me that I am not using the declared variables.

    I am more worried about the other functions right now, I will debug GetGrade next, but I think that I can fix the issues with it.

  6. #6
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33
    I sense you haven't learned passsing by reference yet.
    Here's something more manageable, I followed your semantics :


    #include <stdio.h>

    int score1, score2, score3;
    char grade;
    float avg, avg2;

    void get_scores();
    float average(int, int, int);
    float average2(int, int);
    char get_grade(float, float, int);
    void print_grade(char);

    int main (void)
    {
    get_scores();
    avg = average(score1, score2, score3);
    avg2 = average2(score1, score2);
    grade = get_grade(avg, avg2, score3);
    print_grade(grade);

    return 0;
    }

    void get_scores(void)
    {
    printf("Intput three scores, seperated by a space, then press [Enter].");
    scanf("%d %d %d", &score1, &score2, &score3);
    }

    float average(int score1, int score2, int score3)
    {
    return (score1 + score2 + score3) / 3.0;
    }

    float average2(int score2, int score3)
    {
    return (score2 + score3) / 2.0;
    }

    char get_grade(float avg, float avg2, int score3)
    {
    if (avg >= 90) return 'A';
    else if (avg >= 70 && score3 > 90) return 'A';
    else if (avg >= 70 && score3 < 90) return 'B';
    else if (avg >= 50 && avg2 > 70) return 'C';
    else if (avg >= 50 && avg2 < 70) return 'D';
    else return 'F';
    }

    void print_grade (char grade)
    {
    printf ("The students grade is: %c", grade);
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'll probably have to use pointers then. If you're having trouble with passing variables by value, then pointers are going to kill you.

    You don't fully understand scope:
    Code:
    fun1( void )
    {
        int var1;
    }
    
    fun2( void )
    {
        int var2;
    }
    Now then, fun1 can only see var1. It cannot see var2.
    The same goes for fun2: It cannot see var1, it can see var2.

    If we pass it a copy of this variable:
    Code:
    fun1( int someVar )
    {
        int var1;
    
        ...do stuff...
    
        fun2( var1 );
    }
    
    fun2( int someVar )
    {
        int var2;
    }
    Then the value of 'someVar' is whatever 'var1' was when we called the 'fun2' function from within 'fun1'. Now, keep in mind, this does NOT change the value of var1.

    Quzah.
    Last edited by quzah; 03-22-2002 at 06:17 PM.
    Hope is the first step on the road to disappointment.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I sense you haven't learned passsing by reference yet.

    Passing by "reference" is NOT a feature in C. Again:

    void myfunction( int &var );

    This is NOT valid C code. This is C++ code. You have to use pointers if you want to emulate this behaviour.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    What I have a problem with is passign multiple variables. If it is just one, I am okay, but when it comes to passing two or three, I get compile errors.

    I do not know when to actually declare the variable, as we have been told to NOT use global variables. Also, can you return more than one value, or just one?


    Thanks, this is really helping me. I want to learn, and the explanations are great.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    Originally posted by quzah
    > I sense you haven't learned passsing by reference yet.

    Passing by "reference" is NOT a feature in C. Again:

    void myfunction( int &var );

    This is NOT valid C code. This is C++ code. You have to use pointers if you want to emulate this behaviour.

    Quzah.
    While we are supposed to be using C, we build as .CPP, and compile as C++. (My professor has been programming in Cand C++ since their inception).

    However, we are not using pass by reference, but we can use pointers or pass by value (pointers were briefly touched upon, not fully explained, but that is not the topic of this thread).

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is how you design a function so that it takes multiple values.
    Remember that when you normally declare variables, you can do it all on the same line?

    int x,y,z;
    float a,b,c;

    Well you can't do this in functions. You have to do them seperately:

    void myfunction( int x, int y, int z, float a, float b, float c );

    See how that is different? Ok, just pretend that the , operator in your function declaration is like the ; operator. What you have to do when declaring functions is seperate each new argument by a comma, and then, for the next new one, you have to give it a type and a variable name. (There are a few execptions to this, but for now, just go by what I said, and you should be ok.)

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33

    For Quzah

    You misunderstood me. I was simply implying "call by reference" by passing a pointer to an argument.

    -as in swap(&i, &j);
    void swap(int *x, int *y);

    You looked too deeply.

    Dave


    > I sense you haven't learned passsing by reference yet.

    >Passing by "reference" is NOT a feature in C. Again:

    >void myfunction( int &var );

    >This is NOT valid C code. This is C++ code. You have to use >pointers if you want to emulate this behaviour.
    Last edited by Dave Jay; 03-22-2002 at 06:48 PM.

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    Originally posted by quzah
    This is how you design a function so that it takes multiple values.
    Remember that when you normally declare variables, you can do it all on the same line?

    int x,y,z;
    float a,b,c;

    Well you can't do this in functions. You have to do them seperately:

    void myfunction( int x, int y, int z, float a, float b, float c );

    See how that is different? Ok, just pretend that the , operator in your function declaration is like the ; operator. What you have to do when declaring functions is seperate each new argument by a comma, and then, for the next new one, you have to give it a type and a variable name. (There are a few execptions to this, but for now, just go by what I said, and you should be ok.)

    Quzah.
    That makes sense. But how do I know where to initialize the variable in the first place? If I initialize it in main, then pass it to the called function, the value will be as set in main, right? So I would have to initialize it in the function that actually processes it, and uses another variable to return a value.

    If I want to return more than one value, do I have to use a pointer? or is there an easier way?

  14. #14
    Registered User
    Join Date
    Aug 2001
    Posts
    46
    Well if u initialialize a variable in main which u want to keep using and keep updating with the function then assign the return value of the function to it.

    Lets say:

    Code:
    void main()
    {
       int a=1;// a is 1 right now
     a=callfunction(a); //when calling the function a is stll 1;
    
     //now after the a is returned as 2 and assigned to 'a'. Now 'a' is 2
    
    }
    int callfunction(int a)
    {
      a=2;
    return a;
    }
    For your second question, seethis

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write functions with pointers variables
    By noob programmer in forum C Programming
    Replies: 5
    Last Post: 10-25-2008, 05:48 AM
  2. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  3. Replies: 3
    Last Post: 10-23-2006, 06:37 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. passing variables to functions?
    By aoe in forum C Programming
    Replies: 12
    Last Post: 06-02-2002, 04:19 PM