Computer Programming 05
Computer Programming 05
PROGRAMMING
Lecture 05
do while, for, and nested loops
functions
Initialization action
Done only once at the start
Initialization expression initialize and declare the loop’s control
variable e.g. int counter = 1;
Notice that there is a semi colon after initialization statement
Example
for( int counter = 1; counter <= 10; counter++ )
cout << counter << endl;
Prints integers from one to ten
for keyword
Control variable name Final value of control variable for which the
condition is true
Loop-continuation condition
initialization;
while ( loop Continuation Test)
{ statement
increment;}
1 // example program using for loop <math.h> file tells the compiler
to convert the value of year to a
2 // Calculating compound interest.
temporary double
3 #include <iostream> representation before calling
4 the function pow (functions
10 #include <iomanip> will be discussed latter)
11
15 #include <math.h> // enables program to use function pow
16 using namespace std;
17 // function main begins program execution
18 int main()
19 {
20 double amount; // amount on deposit
21 double principal = 1000.0; // starting principal
22 double rate = .05; // interest rate
Or
true do
condition
{
false statement block
} while (condition);
break statement
Causes immediate exit from while, for, do/while or
switch structure
Program execution continues with first statement after
structure
Common uses
Escape early from a loop
Skip the remainder of switch structure
Example
Variables
sqrt( x );
Expressions
sqrt( sqrt( x ) ) ;
sqrt( 3y + 6 );
13. return 0;
14. }
41
FUNCTION DEFINITION
Describes how the function does its task
Can appear before or after the function is called
Format
return-value-type function-name( parameter-list )
{
declarations and statements
}
Function-name
is any valid identifier e.g. square
Return-value-type
is the data type of the result returned from the function to the caller
Return value type void indicated that the function does not return a vlaue
Parameter-list
Is a comma-separated list of the arguments
containing the declarations (data type needed for each argument) of the
parameters received by the function when it is called
If the function does not receive any values, parameter-list is void or simply
left empty
42
FUNCTION DEFINITION
Example function
int square( int y )
{
return y * y;
}
return keyword
Format return expression;
Returns the value calculated by the function
Returns data, and control goes to function’s caller
If no data to return, use return;
43
FUNCTION PROTOTYPE
Must appear in the code before the function can be called
The compiler use the function prototypes to validate function
call
Format /syntax
Return-value-type Function_Name(Parameter_List);
44
EXAMPLE PROGRAM
1 // program calculate the square of integers 1 to 10
2 // Creating and using a programmer-defined function.
3 #include <iostream> Function prototype: specifies
4 data types of arguments and
5 using std::cout;
6 using std::endl;
return values. square
7 expects and int, and returns
8 int square( int ); // function prototype
an int.
9
Parentheses () cause function
10 int main()
11 { to be called. When done, it
12 // loop 10 times and calculate and output returns the result.
13 // square of x each time
14 for ( int x = 1; x <= 10; x++ )
15 cout << square( x ) << " "; // function call
16
17 cout << endl;
18
19 return 0; // indicates successful termination
20
21 } // end main
23 // square function definition returns square of an integer
24 int square( int y ) // y is a copy of argument to function
25 {
Definition of square. y is a
26 return y * y; // returns square of y as an int
27 copy of the argument passed.
28 } // end function square Returns y * y, or y squared.
45
OUTPUT
1 4 9 16 25 36 49 64 81 100
46
FUNCTION PROTOTYPE
Prototype must match function definition
Function prototype
double maximum( double, double, double );
Definition
double maximum( double x, double y, double z )
{
…
}
observe this in the following program example
This example program uses a programmer-defined function
maximum to determine and return the largest of three integers
47
EXAMPLE PROGRAM
1 // using programmer defined function
2 // Finding the maximum of three floating-point numbers.
3 #include <iostream>
4
5 using std::cout;
6 using std::cin;
7 using std::endl;
8
9 double maximum( double, double, double ); // function prototype
10
11 int main()
12 {
13 double number1;
14 double number2;
Function maximum takes 3
15 double number3;
16 arguments (all double) and
17 cout << "Enter three floating-point numbers: "; returns a double.
18 cin >> number1 >> number2 >> number3;
19
20 // number1, number2 and number3 are arguments to
21 // the maximum function call
22 cout << "Maximum is: "
23 << maximum( number1, number2, number3 ) << endl;
24
25 return 0; // indicates successful termination
48
EXAMPLE PROGRAM
26
27 } // end main Comma separated list for
28 multiple parameters.
29 // function maximum definition;
30 // x, y and z are parameters
31 double maximum( double x, double y, double z )
32 {
33 double max = x; // assume x is largest
34
35 if ( y > max ) // if y is larger,
36 max = y; // assign y to max
37
38 if ( z > max ) // if z is larger,
39 max = z; // assign z to max
40
41 return max; // max is largest value
42
43 } // end function maximum
output
49