ITC Lect 12 [Functions-I]
ITC Lect 12 [Functions-I]
Functions - I
Introduction
• Suppose we have a program that calculates and displays the
area of a rectangle.
#include <iostream>
using std::cout, std::cin, std::endl;
int main() {
// Calculate the area of a rectangle
double length = 5.0;
double width = 3.0;
double area = length * width;
return 0;
}
Introduction
Introduction
return 0;
}
Introduction
• How about
#include <iostream>
using std::cout, std::cin, std::endl;
int main() {
// Use module to do the task
return 0;
}
Introduction
• Function definitions
– Only written once
– These statements are hidden from other functions.
– Boss to worker analogy:
The boss does not know how the worker gets the job done; he just
wants it done
• Example
cout << sqrt( 900.0 );
– Calls the sqrt (square root) function. The preceding statement
would print 30
– The sqrt function takes an argument of type double and returns
a result of type double, as do all functions in the math library
Predefined Functions
Functions
• Functions
– Allow the programmer to modularize a program
• Local variables
– Known only in the function in which they are defined
– All variables declared in function definitions are local variables
• Parameters
– Local variables passed when the function is called that provide the
function with outside information
Functions
• Function Prototypes
• Function Calling
• Function Definition
Function Prototypes
• Function prototype
– Function name
– Parameters
• Information the function takes in
– Return type
• Type of information the function passes back to caller (default int)
• void signifies the function returns nothing
– Only needed if function definition comes after the function call in
the program
• Example:
int maximum( int, int, int );
– Takes in 3 ints
– Returns an int
Function Definitions
• Create customized functions to
– Take in data
– Perform operations
– Return the result
7
return value are declared in
8 int square( int ); // function the function prototype
prototype
2. Loop
9
10 int main() 3. Function
11 { definition
12 for ( int x = 1; x <= 10; x++ )
13 cout << square( x ) << " ";
14
15 cout << endl;
16 return 0; The function square is
17 } called for x = 1, 2, … 10
18
19 // Function definition
20 int square( int y )
Function definition
21 {
22 return y * y;
23 }
1 4 9 16 25 36 49 64 81 100
3 #include <iostream>
5 using std::cout;
6 using std::cin;
7 using std::endl;
10
11 int main()
12 {
13 int a, b, c;
14
17
22 return 0;
23 }
24
29 {
30 int max = x;
31
32 if ( y > max )
33 max = y;
34
35 if ( z > max )
36 max = z;
37
38 return max;
39 }
26 if ( y > max )
If y is greater than max,
27 max = y;
28
y becomes the new max
29 if ( z > max )
now
Finally, if z is greater than
30 max = z;
31
max,
32 return max;
z becomes the new max
33 }
Header Files
• Header files
– Contain function prototypes for library functions
– <cstdlib> , <cmath>, etc.
– Load with #include <filename>
• Example:
#include <cmath>
• Scaling
– Reduces random number to a certain range
– Modulus ( % ) operator
• Reduces number between 0 and RAND_MAX to a number between 0
and the scaling factor
– Example
i = rand() % 6 + 1;
• Generates a number between 1 and 6
3 #include <iostream>
5 using std::cout;
6 using std::endl;
8 #include <iomanip>
10 using std::setw;
11
12 #include <cstdlib>
13
14 int main()
15 {
Notice rand() % 6 . This returns a
16 for ( int i = 1; i <= 20; i++ ) { number between 0 and 5 (scaling).
17 cout << setw( 10 ) << ( 1 + rand() % 6 ); Add 1 to get a number between 1 and
18
6.
19 if ( i % 5 == 0 )
21 }
22
23 return 0;
Executing the program again
24 }
gives the same "random" dice
rolls.
5 5 3 5 5
2 4 2 5 5
5 3 2 2 1
5 1 4 6 4
1 // Fig. 3.9: fig03_09.cpp
3 #include <iostream>
5 using std::cout;
6 using std::cin;
7 using std::endl;
9 #include <iomanip>
10
11 using std::setw;
12
13 #include <cstdlib>
14
15 int main()
16 {
17 unsigned seed;
18
21 srand( seed );
22
25
26 if ( i % 5 == 0 )
28 }
29
30 return 0;
Enter seed: 67
1 6 5 1 4
5 6 3 1 2
References
Dietal and Dietal : How to Program C++
3rd Edition