Presentation is loading. Please wait.

Presentation is loading. Please wait.

Handout on Functions Passing Values to Functions #include int mult(int, int); int main() { int a = 10, b = 20; cout << mult(a, b); //cout << x << y; //

Similar presentations


Presentation on theme: "Handout on Functions Passing Values to Functions #include int mult(int, int); int main() { int a = 10, b = 20; cout << mult(a, b); //cout << x << y; //"— Presentation transcript:

1 Handout on Functions Passing Values to Functions #include int mult(int, int); int main() { int a = 10, b = 20; cout << mult(a, b); //cout << x << y; // *** Error *** --unknown identifiers x, y return 0; } int mult(int x, int y)// can have different names here { return x*y; }

2 Handout on Functions Passing Pointers to Functions #include void f(int *j);//or void f(int *); int main() { int i; int *p; p = &i; // p now points to i f(p); cout << i; // i is now 100 return 0; } void f(int *j) { *j = 100; // var pointed to by j is assigned 100 }

3 Handout on Functions Passing Pointers to Functions #include void f(int *j); int main() { int i; f(&i); cout << i; return 0; } void f(int *j) { *j = 100; // var pointed to by j // is assigned 100 } #include int sqr_it(int x); int main() { int t=10; cout << sqr_it(t) << ' ' << t; //output? } int sqr_it(int x) { x = x*x; return x; }

4 Handout on Functions Passing Arrays to Functions #include void display(int num[10]); //or display(int [10]); int main() { int t[10],i; for(i=0; i<10; ++i) t[i]=i; display(t); // pass array t to a function cout <<endl; for(i=0; i<10; i++) cout << t[i] << ' '; //output? } // Print some numbers. void display(int num[10]) { int i; for(i=0; i<10; i++) cout << num[i] << ' '; //output? for(i=0; i<10; i++) (num[i] = num[i] + 1); }

5 Handout on Functions Passing Arrays to Functions #include void cube(int *n, int num); int main() { int i, nums[10]; for(i=0; i<10; i++) nums[i] = i+1; cout << "Original contents: "; for(i=0; i<10; i++) cout << nums[i] << ' '; cout << '\n'; cube(nums, 10); // compute cubes cout << "Altered contents: "; for(i=0; i<10; i++) cout << nums[i] << ' '; return 0; } void cube(int *n, int num) { while(num) { *n = *n * *n * *n; num--; n++; } Original contents: 1 2 3 4 5 6 7 8 9 10 Altered contents: 1 8 27 64 125 216 343 512 729 1000

6 Handout on Functions Passing Strings to Functions #include void stringupper(char *str); int main() { char str[80]; strcpy(str, "this is a test"); stringupper(str); cout << str; // display uppercase string return 0; } void stringupper(char *str) { while(*str) { *str = toupper(*str); // uppercase one char str++; // move on to next char }

7 Handout on Functions Returning Pointers #include char *get_substr(char *sub, char *str); int main() { char *substr; substr = get_substr("three", "one two three four"); cout << "substring found: " << substr; return 0; } char *get_substr(char *sub, char *str) { int t; char *p, *p2, *start; for(t=0; str[t]; t++) { p = &str[t]; // reset pointers start = p; p2 = sub; while(*p2 && *p2==*p) { //check for substring p++; p2++; } /* If at end of p2 (i.e., substring), then a match has been found. */ if(!*p2) return start; /* return pointer to beginning of substring */ } return 0; // no match found }

8 Handout on Functions Command Line Arguments #include int main(int argc, char *argv[]) { if(argc!=2) { cout << "You forgot to type your name.\n"; return 1; } cout << "Hello " << argv[1] << '\n'; return 0; } #include int main(int argc, char *argv[]) { int t, i; for(t=0; t<argc; ++t) {// t denotes the t th string i = 0; while(argv[t][i]) {// t[i] accesses the i th character of t cout << argv[t][i]; ++i; cout << ' '; } cout << ' '; } return 0; }

9 Handout on Functions Command Line Arguments #include int main(int argc, char *argv[]) { double a, b; a = atof(argv[1]); b = atof(argv[2]); cout << a + b; return 0; } #include int main(int argc, char *argv[]) { while(--argc > 0) cout << *++argv << endl; return 0; } #include int main(int argc, char *argv[]) { for(int j=0; j<argc; j++) cout << argv[j] << endl; return 0; } #include int main(int argc, char *argv[]) { char **argvector = argv; double a, b; a = atof(*++argvector); b = atof(*++argvector); cout << a + b; return 0; }

10 Handout on Functions Passing Reference to Functions #include void sqr_it(int &x); int main() { int t = 10; cout << "Old value for t: " << t << '\n'; sqr_it(t); // pass address of t to sqr_it() cout << "New value for t: " << t << '\n'; return 0; } void sqr_it(int &x) { x *= x; // this modifies calling argument t }

11 Handout on Functions Returning References from Functions #include double &f(); double val = 100.0; int main() { double newval; cout << f() << '\n'; // display val's value newval = f(); // assign value of val to newval cout << newval << '\n'; // display newval's value f() = 99.1; // change val's value cout << f() << '\n'; // display val's new value return 0; } double &f() { return val; // return reference to val }

12 Handout on Functions Returning References from Functions #include double &change_it(int i); // return a reference double vals[] = {1.1, 2.2, 3.3, 4.4, 5.5}; int main() { int i; cout << "Here are the original values: "; for(i=0; i<5; i++) cout << vals[i] << ' '; cout << '\n'; change_it(1) = 5298.23; // change 2nd element change_it(3) = -98.8; // change 4th element cout << "Here are the changed values: "; for(i=0; i<5; i++) cout << vals[i] << ' '; cout << '\n'; return 0; } double &change_it(int i) { return vals[i]; // return a reference to the ith element } Output: 1.1 5298.23 3.3 -98.8 5.5 (the changed values)


Download ppt "Handout on Functions Passing Values to Functions #include int mult(int, int); int main() { int a = 10, b = 20; cout << mult(a, b); //cout << x << y; //"

Similar presentations


Ads by Google