It's possible to use pointers inside functions. In this example for two arguments our function demand we will have two corresponding pointers.
Operation done is simple addition.
Until now we know that functions must be declared/defined outside main() section and call of function must be inside it:
#include <iostream>
using namespace std;
void simpleAdd(int x, int y) {
int *ptrX = &x;
int *ptrY = &y;
cout << "Result of Add: " << (*ptrX) + (*ptrY) << endl;
}
int main() {
simpleAdd(5, 8);
return 0;
}
Result:
Result of Add: 13
Process returned 0 (0x0) execution time : 0.054 s
Press any key to continue.
Sure, our function can return a result to the external world:
#include <iostream>
using namespace std;
int simpleAdd(int x, int y) {
int *ptrX = &x;
int *ptrY = &y;
int Result = (*ptrX) + (*ptrY);
return Result;
}
int main() {
cout << "Result of Addition: " << simpleAdd(2, 5) << endl;
return 0;
}
Result:
Result of Addition: 7
Process returned 0 (0x0) execution time : 0.055 s
Press any key to continue.
No comments:
Post a Comment