201Spring08-L21 - CMPSC 201C Spring 2008 Lecture 21 March 3...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 201C Spring 2008 Lecture 21 March 3, 2008 Updating Variables in the Condition Sometimes programmers do not like to use a separate statement to update the loop control variable in a count-controlled loop, but will combine the update with the condition int j = 1; while (j <= 10) { j++; cout << j; } } int j = 1; while (j++ <= 10) { cout << j; Caution Pre and post incrementation behaves in a similar manner with relational operators as with arithmetic operators. The output from the following two loops would be different int j = 1; while (j++ <= 10) { cout << j; } } int j = 1; while (++j <= 10) { cout << j; Caution The following two loops give different output, why? int j; for (j =1; j++ <= 10 ;) { cout << j; } } int j; for (j =1; j <= 10 ; j ++) { cout << j; Questions ???? Control Structures There are 4 basic control structures or ways flow of control of a program may be affected. Sequence one statement executed after another. Decision/Branches statements may or may not be executed depending on specified conditions. Repetition/loops statements are executed over and over. Functions control goes to another block of code designed for a specific purpose. Functions Statement(s) Function call Statement(s) Statement(s) Functions Functions are a collection of statements that perform a specific task. C ++ has built-in functions, e.g. pow( ), abs( ), sin( ), etc C++ also allows programmer-built functions. Using functions helps organize code, make it easier to port from program to program modular programming. Can call the same function from many places in the program. Employing Functions To use functions, you will need to have 3 (2) items: Function Definition code for the specific task. (In this course function definitions should occur after main) Function Call statement that directs control of the program to that function Function Declaration (Prototype) identifies the function name and requirements. Usually occurs before main so the name is globally defined. Recommended for this course! Function Definitions The function definition specifies what the function does Is comprised of a function heading (header) and a function body. return type function name (data type parameter(s)) { statement(s) //function body } void main ( ) { statement(s) } int main ( ) { statement(s) return 0; } Function Definitions Cannot be nested inside on another. Therefore, because main is a function itself, they may be placed before main or after main (requires a function prototype). For this course you are recommended to put the functions after main and to use function prototypes. (You may be asked to write a function prototype for an exam) Examples- Function Definitions //void function void print2lines ( ) { cout<<"**************************\n"; cout<<"**************************\n"; } //value returning function int sum2int (int a, int b) // params are declared { int c; c = a + b; return c; } Function Return Type May be void, indicating that nothing will be returned, or any of the data types discussed in chapter 2. When the return type is any thing other than void, a return statement should be part of the function body. The function is called a value-returning function. When function calls are part of a condition the functions cannot have a return type of void. Questions ???? Function Prototypes Act similar to a variable declaration. Occur before main. Look similar to function heading except that it ends in a semicolon and it does not require the parameter name(s), just the data type(s). void print2lines ( ); int sum2int (int, int); If the parameter name is included, it is ignored. Function prototypes are required for this course. Format //comments # include library files function prototype(s) int main ( ) { statement(s) return 0; } function definition(s) { statement(s) // body } //comments # include library files function definition(s) { statement(s) //body } int main ( ) { statement(s) return 0; } Calling Functions A function call is the statement that tells the function to execute. When execution of the function has been completed, flow of the program returns to immediately after the call. It has the format of function name ( parameter(s)) The parameters (arguments) from the call are transferred to the function heading in the order in which they appear. Example of Function Calls { statement(s) print2lines ( ); // function call statement(s) } { statement(s) y = sum2int(4 , alpha) + sum2int(5, 6); statement(s) } More on Function Calls A function can call another function or even itself. y = sum2ints(5,sum2ints(pow(x,3), j)); A single C++ statement may call more than one function. xyz = cos(theta) + sum2ints(a,b); Function calls can be part of a condition. if (sum2ints(alpha, beta) > 10) if (boolfunction) //function with return type bool Questions ???? ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online