I need some help in building a function for my program. My program has a number of routines that are the same, so I want to remove the redundant code and make a callable function to replace many lines of code.
What I need to know is how are values passed back and forth between a function.
My function, let's call it getAddr does not receive any values, but must return an array called Arr.
How is this function called?
getAddr (char[]); //Is this how the function is called?
myNewArray [] = char[] ///Is this how to get the array out?
Then what is the format for the function?
int getAddr (Arr[]){
do something and build the Arr array;
When finished return the Arr array;
Since I am building an array is the 'int' the correct type to place before the function name? Is the Arr[] the correct name for the passed value? Or should it be char[]? Then ate the end do I place a return statement?
I'm still rather new to Arduino and C++. I would like to find a good language book that I can study, seems most internet stuff is not specific enough or organized. Thanks Mike
Thanks for the example. Why do you use 'void' , the void changeFoo(int foofoo[], int fooLen) line? You are passing the array foo and the number of elements used in the array. In the function, the array is changed. My next question is what is passed back to the void loop? is the new array foofoo, put into the variable foo?
Also, in the function changeFoo, is the int foofoo[] just a container to recieve and send the array? What is the difference between int foofoo[] and char foofoo[]. Thanks for the help, Mike
mike_zahorik:
Thanks for the example. Why do you use 'void' , the void changeFoo(int foofoo[], int fooLen) line? You are passing the array foo and the number of elements used in the array. In the function, the array is changed. My next question is what is passed back to the void loop? is the new array foofoo, put into the variable foo?
Also, in the function changeFoo, is the int foofoo[] just a container to recieve and send the array? What is the difference between int foofoo[] and char foofoo[]. Thanks for the help, Mike
foo is an int array. I just used that for an example. If you need a char array then use a char array.
Arrays are always passed by reference so when foofoo is modified in the function it modifies the global array foo. I pass the length because if you just pass the array the function does not know how long it is. You need to know that so you don't write past the end of the array and corrupt memory. I used void because there is no reason for a return value since the foo array is being modified.
Thanks, good information. The foo array is global, because it is defined prior to the main function. Passed by reference means the foo array is copied into the local array foofoo and once the function is complete whatever happened to foofoo appears in foo. The array's are 'linked' by reference? So actually the array foofoo is not returned at all. I need to work with this some to better understand it. Thanks Mike
mike_zahorik:
Thanks, good information. The foo array is global, because it is defined prior to the main function. Passed by reference means the foo array is copied into the local array foofoo and once the function is complete whatever happened to foofoo appears in foo. The array's are 'linked' by reference? So actually the array foofoo is not returned at all. I need to work with this some to better understand it. Thanks Mike
You should go through the tutorials to learn about pass by value and pass by reference.
Pass by reference means the actual address of the array foo is passed. There is no copying. The function actually modifies the array itself.