Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS120: Introduction to Computer Science Lecture 14 Functions.

Similar presentations


Presentation on theme: "CPS120: Introduction to Computer Science Lecture 14 Functions."— Presentation transcript:

1 CPS120: Introduction to Computer Science Lecture 14 Functions

2 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.

3 Functions & Structured Programs 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

4 Guidelines for Building Programs With Multiple Functions 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

5 Function Syntax 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 An error will definitely result if you call a function from within the main function that has not been prototyped.

6 Structure of Functions in C++ function-name(argument list) argument declaration; { local variable declarations; executable statement1; executable statement2; ---------- ---------- ---------- return (expression); }

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

8 Describing a Function The first line of a function is called the function header Before the name of the function you must specify a "return type." 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. Immediately preceding each parameter, you must identify the data type of that parameter.

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

10 Scope of Variables The scope of a variable is the area in which it can be legally referenced Variables are either global or local in nature Global variables are ones that are declared outside and above the main function 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

11 Using Functions Avoid using cin or cout statements within functions 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.

12 Passing Data 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. The data types of the arguments and the parameters should match exactly

13 More About Passing Arguments There are technically three ways to pass data as arguments to functions 1. 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 2. passing by reference is to be used when you want the function to actually and permanently change the values of one or more variables 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 3. passing by address is technically what happens when you pass an array to a function

14 Another Look at Return Values 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

15 Library Functions 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.

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

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

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

19 Iteration 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

20 Invocation 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


Download ppt "CPS120: Introduction to Computer Science Lecture 14 Functions."

Similar presentations


Ads by Google