1 / 23

CPS120: Introduction to Computer Science

Learn about subprogram statements, invocation, parameters, arguments, value/reference parameters, function flow of control, and structuring functions in C++. Discover the benefits of using functions, guidelines for creating programs with multiple functions, and syntax tips for function organization. Dive into function examples, headers, return types, and variable scoping in C++.

vwalter
Download Presentation

CPS120: Introduction to Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPS120: Introduction to Computer Science Functions

  2. Subprogram Statements • We can give a section of code a name and use that name as a statement in another part of the program • When the name is encountered, the processing in the other part of the program halts while the named code is executed • -- Invocation

  3. Subprogram Statements • There are times when the calling unit needs to give information to the function to use in its processing • A 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

  4. Subprogram / Function Statements Subprogram flow of control

  5. Subprogram Statements Subprogram flow of control

  6. Subprogram Statements • Parameters: The identifiers listed in parentheses beside the function name; sometimes they are called formal parameters • Arguments: The identifiers listed in parentheses on the function call; sometimes they are called actual parameters

  7. Subprogram Statements • Value 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)

  8. Subprogram Statements

  9. Functions • 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 to make up a structured program.

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

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

  12. Function Syntax • Functions are given valid identifier names, just like variables and constants. • A function name must be followed by parentheses • Coded 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.

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

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

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

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

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

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

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

  20. More About Passing Arguments • There are technically three ways to pass data as arguments to functions • 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 • 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

  21. 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 tworeturn statements within a function • It is not possible to return two values in the same return statement

  22. 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 datatypes to use for those functions.

  23. Recursion • Recursion: the ability of a subprogram to call itself • Each 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)!

More Related