CPS 120 Introduction to Computer Science Lecture 14

  • Slides: 20
Download presentation
CPS 120: Introduction to Computer Science Lecture 14 Functions

CPS 120: Introduction to Computer Science Lecture 14 Functions

Functions n n Every C++ program must have a main function Each of the

Functions n n Every C++ program must have a main function Each of the smaller tasks (let's say, subtasks) can be coded as C++ functions that go together with the main function to make up a structured program.

Functions & Structured Programs n n n Using functions makes it easier to code

Functions & Structured Programs n n n Using functions makes it easier to code programs. Using functions also makes it easier to debug large programs. Using functions makes it easier to maintain and upgrade a program after the first version has been finished

Guidelines for Building Programs With Multiple Functions n n Organization: programs with functions are

Guidelines for Building Programs With Multiple Functions n n Organization: programs with functions are easier to read and modify Autonomy: functions should not depend on data or code outside of the function any more than necessary Encapsulation: functions should keep all of their "details" to themselves Reusability: functions may be reused in other programs or, even, by other programmers

Function Syntax n n Functions are given valid identifier names, just like variables A

Function Syntax n n Functions are given valid identifier names, just like variables A function name must be followed by parentheses Add functions to the bottom of a C++ program, after the main function. If the functions are listed after the main function, then function prototypes must be included at the top of the program, above the main function n An error will definitely result if you call a function from within the main function that has not been prototyped.

Structure of Functions in C++ function-name(argument list) argument declaration; { local variable declarations; executable

Structure of Functions in C++ function-name(argument list) argument declaration; { local variable declarations; executable statement 1; executable statement 2; --------- return (expression); }

An Example of A Function #include <iostream. h> void print. My. Message(int num. Of.

An Example of A Function #include <iostream. h> void print. My. Message(int num. Of. Times); // PROTOTYPE and NAME int main( ) { int user. Input = 0; cout << "Enter a number between 1 and 10 (0 to Exit): " ; cin >> user. Input; if (user. Input != 0) { print. My. Message (user. Input); // CALL STATEMENT WITH ACTUAL PARAMETER } else cout << "Thanks, Bye!"; return 0; } // end of main void print. My. Message(int num. Of. Times) { int i=0; for (i=0; i<= num. Of. Times; i++) {cout << "Let's Go State!!" << endl; } } //end of print. My. Message // FUNCTION HEADER WITH RETURN TYPE AND ACTUAL PARAMETER // LOCAL VARIABLE WITHIN THE FUNCTION // BODY // OF THE // FUNCTION

Describing a Function n n The first line of a function is called the

Describing a Function n n The first line of a function is called the function header Before the name of the function you must specify a "return type. " n n n The return type is the data type of the value that is returned by the function to the calling function If the function does not return any value, you must type the word void as the return type After the name of the function in the function header, you must include a parameter list. n Immediately preceding each parameter, you must identify the data type of that parameter.

Returning Values n If the function is not a void function, there must be

Returning Values n If the function is not a void function, there must be a return statement at the end of the function

Scope of Variables n The scope of a variable is the area in which

Scope of Variables n The scope of a variable is the area in which it can be legally referenced n n Variables are either global or local in nature Global variables are ones that are declared outside and above the main function n They can be used in any function throughout the program. It is not wise to use global variables more than you have to. Local variables are ones that are declared inside of a function, including main. They cannot be used or referred to in other functions

Using Functions n Avoid using cin or cout statements within functions n n Unless

Using Functions n Avoid using cin or cout statements within functions n n Unless the whole purpose of a function is to obtain (and or validate) a user's input, you should not use a cin statement within a function (besides main). Unless the whole purpose of a function is to display something such as a menu, you should avoid using a cout statement within a function besides main.

Passing Data n n Data is passed to functions as arguments When a function

Passing Data n n Data is passed to functions as arguments When a function is "called" by the main function one or more arguments are passed to the function On the receiving end, the function accepts these arguments The variable names of the arguments from the "calling" function do not have to be the same as the names in the "called" function. n The data types of the arguments and the parameters should match exactly

More About Passing Arguments n There are technically three ways to pass data as

More About Passing Arguments n There are technically three ways to pass data as arguments to functions 1. 2. passing by value is the preferred method. You simply use a variable name, an actual numeric literal, or an expression in the parentheses of the call statement passing by reference is to be used when you want the function to actually and permanently change the values of one or more variables n 3. You must use an ampersand (&) before the formal parameter names in the function header (the first line of the function definition) to denote passing by reference passing by address is technically what happens when you pass an array to a function

Another Look at Return Values n n Often, though, you want your function to

Another Look at Return Values n n Often, though, you want your function to return a computed value to the calling function It is not possible in C++ to execute two return statements within a function and since it is not possible to return two values in the same return statement

Library Functions n n There are many functions available to C++ programmers which were

Library Functions n n There are many functions available to C++ programmers which were written by other programmers Use the #include compiler directive at the top of your program to take advantage of these functions. To use the you do not even have to know exactly how they work You do have to know how many arguments to send to the functions and what data types to use for those functions.

Structural Review: Types of Statements n n sequence selection (if and switch structures) iteration

Structural Review: Types of Statements n n sequence selection (if and switch structures) iteration (for and while loops) invocation (functions)

Sequence n Statements that are simply statements that naturally follow one another from the

Sequence n Statements that are simply statements that naturally follow one another from the top of the program to the bottom

Selection n n Statements that include if statements and switch statements Selection statements are

Selection n n Statements that include if statements and switch statements Selection statements are sometimes called conditional or decision statements.

Iteration n n Statements that allow the compiler to return to a point higher

Iteration n n Statements that allow the compiler to return to a point higher in the program in order to continuously repeat one or more statements All loops including while, do/while, and for loops are prime examples of iteration statements

Invocation n Statements that allow you to place a block of statements that you

Invocation n Statements that allow you to place a block of statements that you wish to use several times during a program in one section You then have the ability to "call" those statements whenever you wish to execute them without having to retype the block of statements over and over again within the program In C++, we use "function calls" as invocation statements