Lecture 15: ... of variables and functions |
Global variables are variables that can be used everywhere in the program |
Local variables are only defined inside the function where they were declared. |
With this new information, it is much more clear which variable we can use when. Inside functions we can use the global and the local variables but functions cannot use eachothers variables. The function main cannot use the variable of the function module1, int y. Module1 cannot use the variable of function main, int z. Both functions can use the global variable x, however. |
Warning: try to avoid the use of global variables in functions as much as possible. The reason why is simple. If we want to copy the function for another program this will be more difficult, becuase the new program probably will not have the same global variables. Using only local variables in functions is therefore much better. If you want to use the global variables, pass them as parameters to the functions. Ideally, a function is a stand-alone unit. |
One more rule, typically for C and languages alike (single-pass
compilers): variables can only be used in places AFTER their declaration
in the program, so, if we put the declaration of a variable after a function,
this function cannot use the variable, even if the variable is global.
Let's take a look at some examples. First a program of lecture 13: |
#include <stdio.h>
/* declare global variables x and y
*/
double x, y;
double square(double r)
{
/* declare local variable localr */
double localr;
/* using the local variable localr */
localr = r*r;
/* using the global variable x */
x = localr;
return(x);
}
void main()
{
x = 4.0;
y = square(x);
}
Variable x is a local and a global variable. Inside the function, the local variable will be used. |
When local and global variables exist with the same name, the local
variable has higher priority and will therefore be used inside the function.
Anyway, this is confusing, so always try to avoid using the same identifier
again!
Some languages do not have the difference between local and global variables (for example BASIC). This will mean that we cannot use the same name for a variable twice. |
x = 2.0;
write_square(x);
printf("%f", x);
}
After returning from the function, the value of x hasn't changed. The
total output of the program above will therefore be
4.0
2.0
Passing by reference:
On the other hand, if we do want to change the value of the variable
used in calling the function, we can do this by passing the address of
the variable to the function. All changes to the contents of this address
are therefore permanent
void write_square(double *p)
/* p is a pointer-to-double
*/
{
/* change
the contents of address p: */
*p = *p * *p;
/* show the
contents of address p: */
printf("%f", *p);
}
void main()
{
double x;
x = 2.0;
/* now we have to pass
the address (&) of the variable x: */
write_square(&x);
printf("%f", x);
}
If we now run the program, the output will be
4.0
4.0
because the value of x has changed simultaneously with the
the contents of address p.
|
|
#include <stdio.h>
#include <math.h> void convert_to_polar(float x, float y, float
*r, float *theta)
void main()
convert_to_polar(1.0, 1.0, &r1,
&theta1);
|
|
When passing information to functions we have to take even more care
that we don't do any mixing of types, especially when we use pointers.
As a (bad) example:
void double10(double *dp) { *dp = 10.0; } void main()
double10(&x);
|