CPS120: Introduction to Computer Science Functions.

23
CPS120: Introduction to Computer Science Functions

Transcript of CPS120: Introduction to Computer Science Functions.

Page 1: CPS120: Introduction to Computer Science Functions.

CPS120: Introduction to Computer Science

Functions

Page 2: CPS120: Introduction to Computer Science Functions.

Subprogram StatementsWe can give a section of code a name and use that name as a statement in another part of the programWhen the name is encountered, the processing in the other part of the program halts while the named code is executed

-- Invocation

Page 3: CPS120: Introduction to Computer Science Functions.

Subprogram StatementsThere are times when the calling unit needs to give information to the function to use in its processingA parameter list is a list of the identifiers with which the subprogram is to work, along with the types of each identifier placed in parentheses beside the function name

Page 4: CPS120: Introduction to Computer Science Functions.

Subprogram / Function Statements

Subprogram flow of control

Page 5: CPS120: Introduction to Computer Science Functions.

Subprogram Statements

Subprogram flow of control

Page 6: CPS120: Introduction to Computer Science Functions.

Subprogram StatementsParameters: The identifiers listed in parentheses beside the function name; sometimes they are called formal parametersArguments: The identifiers listed in parentheses on the function call; sometimes they are called actual parameters

Page 7: CPS120: Introduction to Computer Science Functions.

Subprogram StatementsValue parameter: a parameter that expects a copy of its argument to be passed by the calling unit (put on the message board)Reference parameter: a parameter that expects the address of its argument to be passed by the calling unit (put on the message board)

Page 8: CPS120: Introduction to Computer Science Functions.

Subprogram Statements

Page 9: CPS120: Introduction to Computer Science Functions.

FunctionsEvery 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 to make up a structured program.

Page 10: CPS120: Introduction to Computer Science Functions.

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

Page 11: CPS120: Introduction to Computer Science Functions.

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

Page 12: CPS120: Introduction to Computer Science Functions.

Function SyntaxFunctions are given valid identifier names, just like variables and constants. A function name must be followed by parenthesesCoded 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 functionAn error will definitely result if you call a function from within the main function that has not been prototyped.

Page 13: CPS120: Introduction to Computer Science Functions.

Structure of Functions in C++

function-name(argument list)argument declaration;{ local variable declarations; executable statement1; executable statement2; ---------- ---------- return (expression);}             

Page 14: CPS120: Introduction to Computer Science Functions.

An Example of A Function#include <iostream.h> 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

Page 15: CPS120: Introduction to Computer Science Functions.

Describing a FunctionThe first line of a function is called the function headerBefore 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.

Page 16: CPS120: Introduction to Computer Science Functions.

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

Page 17: CPS120: Introduction to Computer Science Functions.

Scope of VariablesThe scope of a variable is the area in which it can be legally referenced

Variables are either global or local in natureGlobal variables are ones that are declared outside and above the main functionThey can be used in any function throughout the program.

It is not wise to use global variables any 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

Page 18: CPS120: Introduction to Computer Science Functions.

Using FunctionsAvoid 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.

Page 19: CPS120: Introduction to Computer Science Functions.

Passing DataData is passed to functions as arguments When a function is "called" by the main function one or more arguments are passed to the functionOn 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

Page 20: CPS120: Introduction to Computer Science Functions.

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

Page 21: CPS120: Introduction to Computer Science Functions.

Another Look at Return Values

Often, though, you want your function to return a computed value to the calling functionIt is not possible in C++ to execute two return statements within a function It is not possible to return two values in the same return statement

Page 22: CPS120: Introduction to Computer Science Functions.

Library FunctionsThere are many functions available to C++ programmers which were written by other programmersUse 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 workYou do have to know how many arguments to send to the functions and what datatypes to use for those functions.

Page 23: CPS120: Introduction to Computer Science Functions.

Recursion Recursion: the ability of a subprogram to call itselfEach recursive solution has at least two cases

base case: the one to which we have an answer general case: expresses the solution in terms of a call to itself with a smaller version of the problem

For example, the factorial of a number is defined as the number times the product of all the numbers between itself and 0:

N! = N * (N 1)!