Difference between revisions of "ES101 - Lesson 8 : Functions with Pass-By-Reference"
(Created page with "== Objective == You will learn the basics of pointers, which are used in combination with the functions. Pass-by-value type of functions pass just the value of the variables ...") |
|||
Line 6: | Line 6: | ||
== Pass By Reference == | == Pass By Reference == | ||
Pass by reference provides a mean to provide input to functions by specifying a memory location, which allows the function to modify caller's variables. In other words, instead of just merely giving a value, you pass the <b>pointer</b> of variable(s), and the pointer points to the memory address where the data is stored. The values are then accessed by using the * operator inside the function, which is called the <b>dereference</b> operator. Students often get confused with & and * symbols. & refers to the memory address whereas as * refers to a value stored at this memory address. | Pass by reference provides a mean to provide input to functions by specifying a memory location, which allows the function to modify caller's variables. In other words, instead of just merely giving a value, you pass the <b>pointer</b> of variable(s), and the pointer points to the memory address where the data is stored. The values are then accessed by using the * operator inside the function, which is called the <b>dereference</b> operator. Students often get confused with & and * symbols. & refers to the memory address whereas as * refers to a value stored at this memory address. | ||
+ | |||
+ | <syntaxhighlight lang="c"> | ||
+ | // Declare foobar() that takes pointers of int and float | ||
+ | void foobar(int *intInput, float *fpInput); | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | // We can no longer do this since we have to provide | ||
+ | // addresses of some variable | ||
+ | foobar(1, 1.2); // ERROR! | ||
+ | foobar(anInt, aFloat); // ERROR! | ||
+ | |||
+ | // Pass variable addresses to foobar() | ||
+ | int anInt = 1; | ||
+ | float aFloat = 2.3; | ||
+ | foobar(&anInt, &aFloat); | ||
+ | |||
+ | // After function call to foobar(): anInt and aFloat are zero | ||
+ | printf("Variables now: %i, %f\n", anInt, aFloat); | ||
+ | } | ||
+ | |||
+ | void foobar(int *intInput, float *fpInput) | ||
+ | { | ||
+ | // intInput and fpInput are addresses, so to read/write | ||
+ | // the values at these addresses, we must use * (dereference) | ||
+ | // operator | ||
+ | printf("Input was: %i, %f\n", *intInput, *fpInput); | ||
+ | |||
+ | // Modify the inputs: | ||
+ | *intInput = 0; | ||
+ | *fpInput = 0; | ||
+ | } | ||
+ | </syntaxhighlight> |
Revision as of 18:17, 16 October 2012
Objective
You will learn the basics of pointers, which are used in combination with the functions.
Pass-by-value type of functions pass just the value of the variables into a function, but with a simple change, you can use functions that accept pass-by-reference type of variables. This is a very powerful concept that will allow your functions to change the values outside of its scope.
Pass By Reference
Pass by reference provides a mean to provide input to functions by specifying a memory location, which allows the function to modify caller's variables. In other words, instead of just merely giving a value, you pass the pointer of variable(s), and the pointer points to the memory address where the data is stored. The values are then accessed by using the * operator inside the function, which is called the dereference operator. Students often get confused with & and * symbols. & refers to the memory address whereas as * refers to a value stored at this memory address.
// Declare foobar() that takes pointers of int and float
void foobar(int *intInput, float *fpInput);
int main(void)
{
// We can no longer do this since we have to provide
// addresses of some variable
foobar(1, 1.2); // ERROR!
foobar(anInt, aFloat); // ERROR!
// Pass variable addresses to foobar()
int anInt = 1;
float aFloat = 2.3;
foobar(&anInt, &aFloat);
// After function call to foobar(): anInt and aFloat are zero
printf("Variables now: %i, %f\n", anInt, aFloat);
}
void foobar(int *intInput, float *fpInput)
{
// intInput and fpInput are addresses, so to read/write
// the values at these addresses, we must use * (dereference)
// operator
printf("Input was: %i, %f\n", *intInput, *fpInput);
// Modify the inputs:
*intInput = 0;
*fpInput = 0;
}