Baljeet
Member
- Jan 31, 2011
- 76
- 0
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:
Output:
Now, whenever you want to output an integer. All you need to do is call the out function and pass the integer.
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:
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.
Output:
Constant parameters
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.
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:
All Credits goes to one who really made this...
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
Code:
void out(int v) {
v++;
cout << v << endl;
}
You call this function like this:
Code:
out(6);
Output:
Now, whenever you want to output an integer. All you need to do is call the out function and pass the integer.
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
Code:
void update(int& v) {
v++;
}
int main() {
int v = 5;
update(v);
cout << v << endl;
return 0;
}
Output:
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:
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;
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.
Output:
Constant parameters
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:
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;
}
This code returns i which is the index of the target in the array or -1 if it is not found.
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);
}
All Credits goes to one who really made this...