Presentation is loading. Please wait.

Presentation is loading. Please wait.

16/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Functions A function consists of: Name Name Arguments (also called parameters) Arguments.

Similar presentations


Presentation on theme: "16/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Functions A function consists of: Name Name Arguments (also called parameters) Arguments."— Presentation transcript:

1 16/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Functions A function consists of: Name Name Arguments (also called parameters) Arguments (also called parameters)NumberType Return value Return valueType The function is called by its name. Arguments are passed to the function. It executes, and returns a value.

2 26/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Why use Functions? Three important concepts in software development: Managing complexity. Managing complexity. Managing change. Managing change. Maximizing reusability. Maximizing reusability. A program can be broken down into manageable pieces. A function can be tested independently. A function can be modified in isolation. A function can be reused.

3 36/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Top-Down Design A development method where a problem is broken down into steps. These steps are in turn broken down into steps. The process stops when a step is readily understood, and then it is coded.

4 46/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Using Libraries Libraries are collections of commonly-used functions that can be used in a program. This makes development much quicker, because someone else has already implemented and tested the functionality. There are libraries for input/output, strings, math, and others. There are language libraries, operating system libraries, and possibly application libraries. See Appendix C in your textbook for a list.

5 56/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Function Calls in C++ Format: ( ) Examples: double sqrt(double) double sqrt(double) double pow(double, double) double pow(double, double) int abs(int) int abs(int) bool islower(char) bool islower(char) Header Files: the required header file for the library for that function needs to be included in the program before that function can be used. Examples: #include #include

6 66/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Creating Functions A C++ program consists of a set of user- defined functions. Format of a function: ( ) ( ){…}

7 76/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Function Prototypes The compiler needs to know that a function exists, and what its arguments and return type is, BEFORE you use the function. A function prototype is a definition of the function for the compiler. They are put in the program global area before the function is called. Format of a prototype: ( ); ( );

8 86/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Return Statements The return statement is used to exit a function and return a value from that function. It is typically the last statement in a function. Regular functions must have a return statement. Format for regular functions: return ; Format for void functions: return;

9 96/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Calling Regular Functions Use the name of the function followed by the arguments, matching by number and type. Examples: sqrt(4.0) sqrt(4.0) pow(m, n) pow(m, n)

10 106/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Calling Void Functions A void function is a function that does not return a value. It has a return type of void. It is called as an executable statement. Examples: get_parameters(); get_parameters(); display_formatted_value(weight); display_formatted_value(weight); display_decimal_value(salary, 2); display_decimal_value(salary, 2);

11 116/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Types of Parameters Value Parameters: The parameter is an expression that is evaluated, and the value is passed to the function. The parameter is an expression that is evaluated, and the value is passed to the function. It is not possible to modify the state of the calling function from the called function. It is not possible to modify the state of the calling function from the called function. Reference Parameters: The parameter is a variable, and its address is passed to the function. The parameter is a variable, and its address is passed to the function. The called function can modify the contents of the parameter, and therefore modify the variable in the calling function. The called function can modify the contents of the parameter, and therefore modify the variable in the calling function. This is the method to use if multiple values need to be returned from the function. This is the method to use if multiple values need to be returned from the function. Syntax change: put a “&” char after the data type of the parameter, eg. double& speed. Syntax change: put a “&” char after the data type of the parameter, eg. double& speed.

12 126/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Scope of Identifiers Refers to where in a program an identifier is recognized. Global: the identifier is defined outside of any function, and is visible from that point onwards. Local: the identifier is defined inside a function, and is visible from that point to the end of the function. Global variables are one method for passing data between functions. It is considered poor programming practice, but for a simple program it may work well. It is poor practice because it is difficult to determine or control where that variable is modified.

13 136/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Example Programs ch02pp01.cppch02pp01.cppch02pp01.cpp ch02pp02.cppch02pp02.cppch02pp02.cpp ch02pp05.cppch02pp05.cppch02pp05.cpp ch02pp11.cppch02pp11.cppch02pp11.cpp

14 146/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Textbook Reading Chapter 3 & 6 cover these same concepts. Reviewing them may help clarify the material.


Download ppt "16/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Functions A function consists of: Name Name Arguments (also called parameters) Arguments."

Similar presentations


Ads by Google