Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.

Similar presentations


Presentation on theme: "Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store."— Presentation transcript:

1 Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store a new number on your phone, view a text message or get cash at an ATM. In the case of storing a new number on the phone, you must provide the information to be stored (inputs). The phone then stores that information so that it can be retrieved at another time. When you view a text message you request that a specific message be displayed (input) and the phone presents the message to you on the screen (output). When you get cash at an ATM you have to tell the machine how much you want and may need to provide it with information about which account you want to draw from (inputs). The machine then performs some tasks that you don’t see such as making sure the account has sufficient funds and subtracting the withdrawal from the account to create a new balance. It then either gives you an error message (in the case of insufficient funds) or dispenses the requested amount of cash (outputs).

2 C++ Functions Like the previous examples, functions in C++ may or may not have inputs and outputs but they all perform a very specific task or tasks. Syntax for function definition: function_return_type function_name (parameter list) { C++ code needed to implement the task the function is to perform return return_value; } The parameter list indicates the types that must be used when calling the function and the order of the variables. Note-1: The parameter list may be empty or contain multiple parameter. Note-2: The first line above is called the function ‘header’

3 Functions You have already been exposed to some functions in the ‘cmath’ library (#include : double pow(double base, double exponent) where x is the number being raised to the y power double sqrt(double x) where x is the number for which the square root is to be calculated The red ‘pow’ and ‘sqrt’ are the names of the functions base, exponent and x in the above statements are called the parameters of the functions. (Note: Sometimes they may be called arguments. In this class we will distinguish between the term argument and parameter) The blue double indicates the data type of the parameters. The purple double is the data type of the value that is returned by the functions (the return type).

4 Calling Functions Examples: You ‘called’ these functions in a fashion similar to that below double p, q, r, s; p = 10; q = 3; r = 25; s = pow(p,q); cout << “s “ << s; // outputs s = 10 3 or 1000 s = sqrt(r); cout << “s “ << s; // outputs s = 5 Note: p and q above are the arguments substituted for the corresponding location of the parameters in the function definition.

5 What is Not Seen? What you don’t see is the code that is executed when you invoke (call) the library functions. This code is stored in the cmath library that you include via the #include preprocessor directive Note that each of these two functions perform a single task. These functions are in a library because they perform tasks commonly used by many programs. Someone had to write the code as they are not a part of the core C++ language. Programmers can define their own functions that perform a task that the program they are writing can use.

6 Defining a Function Syntax/Structure (revisited) return_type function_name(parameter1_type arg1, parameter2_type parameter2, …) { code to perform the task of the function (This includes any variable definitions that it may need) return value (not needed for all functions) } Note: If the task a function performs does not need to return a value (like pow and sqrt) it still has to have a defined ‘return type’. The return type of a function that simply performs a task but does not return a value is of type ‘ void ’.

7 Function CS1428 Style Requirements Function names should provide an indication of the task the function performs Function names should have each word start with an upper case letter and the rest lower case. Words should NOT be separated with an underscore Example: For a function that accepts data in its parameters and returns ‘true’ if the data is valid and ‘false otherwise, you might have a name like bool IsValid(int number) where IsValid is the function name and number is a parameter of IsValid. Each function definition will have a header comment with ** that go entirely across the page

8 Where Does the Function Definition Go? The compiler has to know a few things about a function before it can compile (translate) a piece of code that calls the function. There are two ways this can be accomplished. 1.The function must defined in the source code file prior to any other piece of code calling that function. or 2.A function prototype can be used to bypass the above requirement. Style Note: Each function should have a header comment with ****** all the way across the top of the comment to visually separate the function definitions. The header should contain the function name, its inputs (parameters) and output (return value) as well as a description of the task(s) it is to perform.

9 What Is a Function Prototype? A function prototype is like a function declaration statement. It includes the return type of the function, the function name and the types of any parameters. Ex. void displaymenu (); //Note: no parameters int biggest (int, int, int); //Note: three parameters The prototype (function declaration) statements must be ABOVE any other function that calls that function.

10 Functions Returning a Value Functions with a return type other than void can be used to provide data to the calling function. Let’s look at our void function DisplayMenu and extend the task of the code in the function to include getting the user’s menu choice and returning it to the calling function.

11 Functions with Parameters *Sending Information TO a Function* Data can be sent to a function so that the function can use the data to complete its task. Such data are called parameters/arguments. In the function definition we refer to them as parameters. In the function call we refer to the values inserted in place of the parameters as arguments. Functions can have multiple parameters In the function definition the data types of the parameters must be declared When the function is called, only the name of the argument being sent is included. Its data type of an argument must match the type of the corresponding parameter in the function definition. The scope of variables defined within a function are limited to that function. Only the value of the argument is passed, not its memory location (Called ‘Pass by Value’)

12 Example: Parameters vs. Arguments /****************************************************************************** * AreaTriangle – given the base and height of a triangle, this function * returns the area */ parameters double AreaTriangle(double base, double height) { double calculated_area; calculated_area =.5 * base * height; return calculated_area } corresponds to CALLING CODE SNIPPET: double tri_area, tri_base, tri_hgt; tri_base = 24.6; tri_hgt = 7.8; tri_area = AreaTriangle(tri_base, tri_hgt); argument s


Download ppt "Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store."

Similar presentations


Ads by Google