Download presentation
Presentation is loading. Please wait.
Published byCharlotte Gibbs Modified over 9 years ago
1
111/15/2015CS150 Introduction to Computer Science 1 Summary Exam: Friday, October 17, 2003. Assignment: Wednesday, October 15, 2003 We have completed chapters 1, 2, 4 and 5. We have covered basic C++ expressions, arithmetic expressions, selection structures and repetition structures. We will now look at functions.
2
211/15/2015CS150 Introduction to Computer Science 1 Functions Functions are a way of building modules in your program. Encapsulate some calculation. Less repetitive code. Examples: x = sqrt(y); cout << x << endl;
3
311/15/2015CS150 Introduction to Computer Science 1 Library functions Code reuse. Why reinvent the wheel? Libraries are collections of often used functions. Math library is an example. #include List of functions given on p. 116. Examples: abs(x) pow(x,y) sin(x)
4
411/15/2015CS150 Introduction to Computer Science 1 Problem How would we rewrite the powers of 2 problem using the cmath library? (Output first 10 powers of 2)
5
511/15/2015CS150 Introduction to Computer Science 1 User defined functions We can write our own functions. Implement top down design Determine major steps of program. Write functions to implement each step. Program is more modular--each step is clearly defined. Details of steps are hidden.
6
611/15/2015CS150 Introduction to Computer Science 1 Problem Write a program that outputs the cube of the first ten integers.
7
711/15/2015CS150 Introduction to Computer Science 1 Example #include int cube(int); void main() { int i, cubeOfI; for (i = 1; i<= 10; i++) { cubeofi = cube(i); cout << “Cube of “ << i << “ is “ << cubeofi << endl; } int cube (int k) { return k*k*k; }
8
811/15/2015CS150 Introduction to Computer Science 1 Working with Functions 1. Function prototypes Function must be declared before it is referenced. General form: type fname(datatypes of formal_arguments); Example: int cube(int);
9
911/15/2015CS150 Introduction to Computer Science 1 Working with Functions 2. Function definitions General form: type fname(formal arguments) { local declarations; statements; } Example: int cube(int k) { return k*k*k; }
10
1011/15/2015CS150 Introduction to Computer Science 1 Working with Functions 3. Function call General form: fname(actual arguments); Example: cubeofi = cube(i);
11
1111/15/2015CS150 Introduction to Computer Science 1 Things to remember Formal arguments are the arguments in the function definition. Actual arguments are the values or variables in the function call. Order of the arguments is very important! Make sure types match.
12
1211/15/2015CS150 Introduction to Computer Science 1 Problem Write a function that sums integers in a particular range. Write a program that uses the function.
13
1311/15/2015CS150 Introduction to Computer Science 1 Example #include char isEven(int); int getVal(); int main(void) { int num; while ((num = getVal()) != -999) { if (isEven(num) == ‘E’) cout << “EVEN” << endl; else cout << “ODD” << endl; } return 0; }
14
1411/15/2015CS150 Introduction to Computer Science 1 Example (cont.) char isEven(int number) { const char even = ‘E’; const char odd = ‘O’; char answer; if (number % 2 == 0) answer = even; else answer = odd; return answer; }
15
1511/15/2015CS150 Introduction to Computer Science 1 Example (cont.) int getVal() { int number; cout << “Enter an integer number: “; cin >> number; return number; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.