CS 150 Introduction to Computer Science I Functions

















- Slides: 17

CS 150 Introduction to Computer Science I Functions Divide and Conquer October 25, 2005 CS 150 Introduction to Computer Science I 1

Last Time S We o S Completed discussing reading from and writing to files Today we will o Begin learning about functions and modularity in C++ October 25, 2005 CS 150 Introduction to Computer Science I 2

Functions S Functions are a way of building modules in your program S Encapsulate some calculation S Less repetitive code S Example: o x = sqrt(y); o cout << x << endl; October 25, 2005 CS 150 Introduction to Computer Science I 3

Library functions S Code reuse. Why reinvent the wheel? S Libraries are collections of often used functions S Math library is an example o #include <cmath> S List of functions given on p. 173 S We can call (invoke) the functions as follows: o abs( x ) o pow( x, y ) o sin( x ) October 25, 2005 CS 150 Introduction to Computer Science I 4

Problem S 14. 1: How would we rewrite the powers of 2 problem using the cmath library? (Output first 10 powers of 2) October 25, 2005 CS 150 Introduction to Computer Science I 5

Programmer Defined Functions S We can write our own functions S Implement top down design o Determine major steps of program o Write functions to implement each step o Program is more modular--each step is clearly defined o Details of steps are hidden October 25, 2005 CS 150 Introduction to Computer Science I 6

Problem S Write a program that outputs the cube of the first ten integers that includes a function cube. S The solution follows. October 25, 2005 CS 150 Introduction to Computer Science I 7

Example #include <iostream> int cube(int); void main() { int i, cube. Of. I; for (i = 1; i <= 10; i++) { cubeofi = cube(i); cout << “Cube of “ << i << “ is “ << cubeofi << endl; } } int cube (int k) { return k * k; } October 25, 2005 CS 150 Introduction to Computer Science I 8

Working with Functions S 1. Function prototypes S Function must be declared before it is referenced S General form: o S type fname(datatypes of formal_arguments); Example: o int cube(int); October 25, 2005 CS 150 Introduction to Computer Science I 9

Working with Functions S 2. Function definitions S General form: type fname(formal arguments) { local declarations; statements; } S Example: int cube(int k) { return k * k; } October 25, 2005 CS 150 Introduction to Computer Science I 10

Working with Functions S 3. Function call S General form: o S fname(actual arguments); Example: o cubeofi = cube(i); October 25, 2005 CS 150 Introduction to Computer Science I 11

Things to remember S Formal arguments are the arguments in the function definition S Actual arguments are the values or variables in the function call S Order of the arguments is very important! S Make sure types match October 25, 2005 CS 150 Introduction to Computer Science I 12

Problem S 14. 2: Write a function that sums integers in a particular range S 14. 3: Write a program that uses the above function October 25, 2005 CS 150 Introduction to Computer Science I 13

Example #include <iostream> char is. Even(int); int get. Val(); int main(void) { int num; while ((num = get. Val()) != -999) { if (is. Even(num) == ‘E’) cout << “EVEN” << endl; else cout << “ODD” << endl; } return 0; } October 25, 2005 CS 150 Introduction to Computer Science I 14

Example (cont. ) char is. Even(int number) { const char even = ‘E’; const char odd = ‘O’; char answer; if (number % 2 == 0) answer = even; else answer = odd; return answer; } October 25, 2005 CS 150 Introduction to Computer Science I 15

Example (cont. ) int get. Val() { int number; cout << “Enter an integer number: “; cin >> number; return number; } October 25, 2005 CS 150 Introduction to Computer Science I 16

Summary S S In today’s lecture we covered o Library functions o Programmer defined functions Readings o P. 170 - 180 October 25, 2005 CS 150 Introduction to Computer Science I 17