MESSAGE
DATE | 2010-05-05 |
FROM | Ruben Safir
|
SUBJECT | Re: [NYLXS - HANGOUT] C++ Workshop - Syntax Basics -- functions
|
Function Arguments:
We have already noted that the general rule for the input of functions is the argument, or parameter list within the call operator. furthermore, the default rule is that data in variables is copied into the function body, creating a new set of data that is local to the scope of the function. And we also stated that there is important exceptions to this rule.
If you want to affect data that exists outside of your function, there are several important methods to do so. The most basic method is the use of pointers. When a pointer is defined in a scope global to the your function, and when there is a value attached, memory is allocated outside your function for the data and that can be manipulated within the scope of your function. Data that is created within your function, even if assigned to a pointer, still remains within your function only, and disappears when the return value is only the pointer and when your function instance ends. Let us look at this behavior.
_______________________________________________________________________
#include using namespace std;
int global_a, * ptr_a; int scope_data(int); int * scope_pointer(int *);
int scope_data(int in){ in = 1000; int out = 25; return out; }
int * scope_pointer(int *in){ *in = 1000; int * out; int buf = 25; out = &buf; cout << "The value of 'out' within function scope_pointer ==> " << *out << endl; return out; }
int main(int argc, char * argv[]){
int func_return, *func_ptr_return; global_a = 5; ptr_a = &global_a;
func_return = scope_data(global_a); cout << "global_a ==>" << global_a << endl; cout << "ptr_a value==>" << *ptr_a << endl; cout << "function return ==>" << func_return << endl;
func_ptr_return = scope_pointer(ptr_a); cout << "\n\nPointer Behavior\n"; cout << "ptr_a value: Since ptr_a points to global_a, the memory \ \nsegment is retained with the new value ==> " << *ptr_a << endl; cout << "\n\nFunction ptr return value:meaningless garbage \ \nsince buf is out of scope ==> " << *func_ptr_return << endl <
return 0; }
_____________________________________________________________________________
ruben-at-www2:~/cplus/functions> g++ -Wall function_scope.cpp -o scope.bin ruben-at-www2:~/cplus/functions> ./scope.bin global_a ==>5 ptr_a value==>5 function return ==>25 The value of 'out' within function scope_pointer ==> 25
Pointer Behavior ptr_a value: Since ptr_a points to global_a, the memory segment is retained with the new value ==> 1000
Function ptr return value:meaningless garbage since buf is out of scope ==> 108
Function arguments and arrays:
The relationship of implicit conversion of arrays to pointers takes an interesting and important twist when arrays are used as arguments within functions. Arrays are an exception to the rule of the pass by value rule for functions. Arrays, as they are in other situations, are passed by reference with the array symbol being implicitly converted as a pointer to the first element of the array. This is a source of a lot of confusion, and needs to be studied thoroughly.
Let us start out first with the simplest examples, the passing of a common integer array.
func_array.cpp
#include #include "func_array.h"
using namespace std;
int test_array[10] = {0,1,2,3,4,5,6,7,8,9};
void single_array(int my_array[], int size) { //reverse array contents for(int i = 0; i< 10; i++, size--){ my_array[size - 1] = i; }
}
int main(int argc, char * argv[]){
single_array(test_array, 10);
for(int i = 0; i<10; i++) cout << test_array[i] < return 0; }
func_array.h
#ifndef FUNC_ARRAY_H #define FUNC_ARRAY_H #endif /* FUNC_ARRAY_H */
void single_array(int[]);
ruben-at-www2:~/cplus/functions/func_array> ./func_array.bin 9 8 7 6 5 4 3 2 1 0
What you notice with this use of an array as an argument to the function single_array, that there is no return of the array back to main. This is because the array is passed by reference, and it doesn't need to be returned. Changes to the array change the array outside of the scope of the function. In fact, the array is passed as a pointer to the first integer in int test_array[10], and therefor the second argument, the array size, to know the size of the array. Without this information being sent into your function, the function has no way of knowing the size of the array and you can walk right past the end of the array.
Likewise, you must be certain to assign memory to your array, in this case by initializing the array, otherwise when you pass an empty array to your function, the point to the first element, an element which doesn't yet exist, is a random garbage location.
For example if we rewrite our program as such:
#include #include "func_array.h"
using namespace std;
int test_array[];
void single_array(int my_array[], int size) { //reverse array contents for(int i = 0; i< 10; i++, size--){ my_array[size - 1] = i; }
}
int main(int argc, char * argv[]){
single_array(test_array, 10);
for(int i = 0; i<10; i++) cout << test_array[i] < return 0; }
and try to compile we get a compiler error:
ruben-at-www2:~/cplus/functions/func_array> g++ func_array.cpp -o func_array.bin func_array.cpp:6: error: storage size of ‘test_array’ isn't known ruben-at-www2:~/cplus/functions/func_array>
|
|