1 / 9

Summary

Summary. Assignment: Wednesday, October 29, 2003. Last time we talked about functions: A function consists of: Function Prototype Function Definition Function Call Rules of parameter list. Call by reference & call by value. void changeIt (int, int&, int&); void main() { int i,j,k,l;

carolt
Download Presentation

Summary

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Summary • Assignment: Wednesday, October 29, 2003. • Last time we talked about functions: • A function consists of: • Function Prototype • Function Definition • Function Call • Rules of parameter list. • Call by reference & call by value. CS150 Introduction to Computer Science 1

  2. void changeIt (int, int&, int&); void main() { int i,j,k,l; i = 2; j = 3; k = 4; l = 5; changeIt(i,j,k); cout << i << j << k << endl; changeIt(k,l,i); cout << i << k << l << endl; changeIt(i,j,j); cout << i << j << endl; changeIt(i,i,i); cout << i << endl; } void changeIt(int a, int& b, int& c) { a++; b += 2; c += a; } What is the output? CS150 Introduction to Computer Science 1

  3. #include <iostream> void changeIt(int, int&, int&); void main() { int i,j,k,l; i = 2; j = 3; k = 4; l = 5; changeIt(i, j, k); cout << i << j << k << endl; changeIt(k,l,i); cout << i << k << l << endl; } void changeIt(int j, int& i, int& l) { i++; j += 2; l += i; } What is the output? CS150 Introduction to Computer Science 1

  4. Program • Write a function to compute the sum and average of two integers, and return the values of sum and average. • An example function call would look like: compute (4, 5, sum, average); CS150 Introduction to Computer Science 1

  5. Scope • Variables have scope - places in the program where they can be referenced. • Local scope - valid only in function or main program. • Global scope - valid anywhere in program. • We will use local variables most of the time. CS150 Introduction to Computer Science 1

  6. Example void computeSum(int, int); int sum; void main() { int i,j; cin >> i >> j; sum = 0; computeSum(i,j); cout << sum << endl; } void computeSum(int num1, int num2) { sum = num1 + num2; } CS150 Introduction to Computer Science 1

  7. Example int computesum(int, int); void main() { int i,j; cin >> i >> j; computesum(i,j); cout << sum << endl; } int computesum(int num1, int num2) { int sum; sum = num1 + num2; return sum; } CS150 Introduction to Computer Science 1

  8. Example int computesum(int, int); void main() { int i,j,sum; cin >> i >> j; sum = computesum(i,j); cout << i << j << sum << endl; } int computesum(int num1, int num2) { int i; i = num1 + num2; return i; } CS150 Introduction to Computer Science 1

  9. Example void silly(float); void main() { float x, y; x = 23; y = 5; silly(x); cout << x << y << endl; } void silly(float x) { float y; y = 25.0; x = y; } CS150 Introduction to Computer Science 1

More Related