Functions
A function is a way of encapsulating code for reuse. The proper use of functions can make modifying your code much easier.
A Basic Function
You call this function like this:Code:void out(int v) { v++; cout << v << endl; }
Output:Code:out(6);
Now, whenever you want to output an integer. All you need to do is call the out function and pass the integer.7
When you declare a function you use this syntax:
return type name(parameters) {
body
}
Arrays by Reference? Passing Arrays
When you pass parameters they are passed by copy. However, arrays and objects are a little different. When you pass an array to a function, you pass the memory address of the array to the function. This is how it appears to be passed by reference.
Passing by reference
Output:Code:void update(int& v) { v++; } int main() { int v = 5; update(v); cout << v << endl; return 0; }
6
When you want things updated in the function, passing by reference is useful. You might want this behaviour when you are sorting arrays.
Example:
Even though, the function is not indicating the array is being passed by reference, it is being passed by a reference as you can tell when you look at the output. The array has been sorted.Code:void sort(int v[], int size) { for (int i=0;i<size-1;i++) { for (int x=i+1;x<size;x++) { if (v[x] < v[i]) { v[x] ^= v[i]; v[i] ^= v[x]; } } } } int arn[] = {4,3,2,5,1,7}; sort(arn,6); for (int i=0;i<6;i++) { cout << arn[i] << " "; } cout << endl;
Output:
Constant parameters1 2 3 4 5 7
Passing a value as a constant ensures that you can never modify the variable in the function.
This might be useful in a search function.
Example:
This code returns i which is the index of the target in the array or -1 if it is not found.Code:int search(const int v[], int size, int target) { for (int i=0;i<size;i++) { if (v[i] == target) { return i; } } return -1; }
If I try to modify v in the function, I get an error saying trying to modify a read only parameter.
Return values
All methods return a value which is indicated with a return statement. There may be several return statements in a function but a function can onyl return 1 value. Unless the function is a void function in which case it does not return any value.
Recursive functions
A function that calls itself is said to be a recursive function. These are complicated beasts and are a topic for their own tutorial.
Example:
Code:int fib(int n) { if (n <= 1) return n; return fib(n-1) + fib(n-2); }
Last edited by chili5; 08-28-2009 at 05:29 AM.
Sorry, chili, but this isn't accurate.When you pass parameters they are passed by copy except with arrays, and objects. Arrays and objects are passed b y revference which means that if they are modified in the function then they are modified outside of the function. Otherwise, the variable is passed by copy and changes made in the function are not made outside of the function.
All variables are passed as a local copy unless explicitly passed by reference. This includes objects and arrays. However (!) when passing an array into a function, you actually use the name of the array which is a pointer. That, combined with the fact that use can use the [] operator with pointers makes it appear that you've passed it by reference.
![]()
Didn't know that, I was assuming that they were passed by reference.![]()
Does this make more sense?
When you pass parameters they are passed by copy. However, arrays and objects are a little different. When you pass an array to a function, you pass the memory address of the array to the function. This is how it appears to be passed by reference.
More importantly, it's accurateTypically, large objects are passed by reference or a pointer to the object is passed. It takes a conscious choice, however.
Don't worry about it too much, it's those little things that can be "gotcha" moments for C/C++![]()
well written thx
Tyvm for making this.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks