# Thread: Why do variables equal functions?

1. ## 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);
} 2. 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. 3. 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>

int GetScores (int);
float Average (float);

int main (void)
{

GetScores();
Average(Score1, Score2, Score3);

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)
{

if Avg >= 90
else if Avg >= 70 && Score3 > 90
else if Avg >= 70 && Score3 < 90
else if Avg >= 50 && Avg2 > 70
else if Avg >= 50 && Avg2 < 70

}

{
} 4. 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. 5. 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. 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;
float avg, avg2;

void get_scores();
float average(int, int, int);
float average2(int, int);

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

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';
}

{
} 7. 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. 8. > 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. 9. 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. 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. 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. 12. ## 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. 13. 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. 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;
}``` Popular pages Recent additions 