1 / 26

Introduction to Functions in C++ Programming

This chapter covers the basics of writing functions in C++ programming, including void functions, function arguments and parameters, local variables, and the differences between value and reference parameters.

roymichael
Download Presentation

Introduction to Functions in C++ Programming

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. Chapter 7Functions CS185/09 - Introduction to Programming Caldwell College

  2. Chapter 7 Topics • Writing a program using functional decomposition • Writing a Void function for a task • Using function arguments and parameters • Differences between value parameters and reference parameters • Using local variables in a function Scott Marino - Caldwell College

  3. Functions • Every C++ program must have a function called main • Program execution always begins with function main • Any other functions are subprograms and must be explicitly called Scott Marino - Caldwell College

  4. Function Calls • One function calls another by using the name of the called function next to ( ) enclosing an argument list • A function call temporarily transfers control from the calling function to the called function Scott Marino - Caldwell College

  5. Function Call Syntax • returntype FunctionName(argumentlist) • The argumentlist is a way for functions to communicate with each other by passing information • The argumentlist can contain 0, 1, or more arguments, separated by commas, depending on the function • The returntype is the data type of the returned value if a value is returned • VOID is used as a placeholder when no value is present Scott Marino - Caldwell College

  6. int Cube ( int n )heading { body return n * n * n ; } Two Parts of Function Definition Scott Marino - Caldwell College

  7. What is in a prototype? • int Cube(int); // prototype • A prototype looks like a heading but must end with a semicolon; • Its parameter list just needs to contain the type of each parameter. Scott Marino - Caldwell College

  8. When a function is called • Temporary memory is set up ( for its value parameters and any local variables, and also for the function’s name if the return type is not void) • Then the flow of control passes to the first statement in the function’s body. The called function’s body statements are executed until one of these occurs: • return statement (with or without a return value) • closing brace of function body • Then control goes back to where the function was called. Scott Marino - Caldwell College

  9. #include <iostream> int Cube ( int ) ; // prototype using namespace std; void main ( ) { int yourNumber ; arguments int myNumber ; yourNumber = 14 ; myNumber = 9 ; cout << “My Number = “ << myNumber ; cout << “its cube is “ << Cube (myNumber)<< endl ; cout << “Your Number = “ << yourNumber ; cout << “its cube is “ << Cube (yourNumber)<< endl ; } Scott Marino - Caldwell College

  10. To Compile Successfully • Before a function is called in your program, the compiler must previously process either the function’s prototype, or the function’s definition (heading and body) Scott Marino - Caldwell College

  11. A C++ function can return • Only 1 value of the type which was specified (called the return type) in its heading and prototype • A void-function cannot return any value in its identifier Scott Marino - Caldwell College

  12. A void function void DisplayMessage( int index ) { if ( index < 35 ) cout << “Pleasant”; else if ( index <= 60 ) cout << “Unpleasant”; else cout << “Health Hazard”; } Scott Marino - Caldwell College

  13. return; • Is valid only in the body block of void functions • Causes control to leave the function and immediately return to the calling block leaving any subsequent statements in the function body unexecuted • When in the main function, control is returned to the operating system Scott Marino - Caldwell College

  14. Value-returning Functions #include <iostream> int Square ( int ) ; // prototypes int Cube ( int ) ; using namespace std; int main ( ) { cout << “The square of 27 is “ << Square (27) << endl; // function call cout << “The cube of 27 is “ << Cube (27) << endl; // function call return 0; } Scott Marino - Caldwell College 14

  15. Value-returning Functions int Square ( int n ) // header and body { return n * n; } int Cube ( int n ) // header and body { return n * n * n; } Scott Marino - Caldwell College

  16. A void function call #include <iostream> void DisplayMessage ( int ) ; // prototype using namespace std; int main ( ) { DisplayMessage(15); // function call cout << “Good Bye“ << endl; return 0; } 16 Scott Marino - Caldwell College

  17. A void function call • void DisplayMessage ( int n ) { cout << “I have liked math for “ << n << “ years” << endl; return ; } • Does not return a value Scott Marino - Caldwell College

  18. Parameter List • Is the means used for a function to share information with the block containing the call • Used to pass information is into the function Scott Marino - Caldwell College

  19. Arguments Parameters Classified by Location Always appear in a function call within the calling block. Always appear in the function heading, or function prototype. Scott Marino - Caldwell College

  20. Parameter Types • Value • By default, parameters (of simple types like int, char, float, double) are value parameters, unless you do something to change that • Reference • To get a reference parameter you need to place & after the type in the function heading and prototype. Scott Marino - Caldwell College

  21. When to Use Reference Parameters • Reference parameters should be used when you want your function to give a value to, or change the value of, a variable from the calling block without an assignment statement in the calling block • Also known as • pass-by-address • pass-by-location Scott Marino - Caldwell College

  22. #include <iostream> #include <fstream> #include <cmath> void GetRoots(float, float, float, float&, float&); using namespace std; void main ( ) { ifstream myInfile; ofstream myOutfile; float a, b, c, first, second; int count = 0; ...... // open files while ( count < 5 ) { myInfile >> a >> b >> c; GetRoots(a, b, c, first, second);//call myOutfile << a << b << c << first << second << endl; count++ ; } // close files ...... } 22 Scott Marino - Caldwell College

  23. Questions • Why is a function used for a task? • To cut down on the amount of detail in your main program (encapsulation). • Can one function call another function? • Yes • Can a function even call itself? • Yes, that is called recursion. It is very useful and requires special care in writing. Scott Marino - Caldwell College

  24. More Questions • Does it make any difference what names you use for parameters? • NO. Just use them in function body. • Do parameter names and argument names have to be the same? • NO. Scott Marino - Caldwell College

  25. Functions are written to specifications • The specifications state the return type, the parameter types, whether any parameters are “outgoing,” and what task the function is to perform with its parameters • The advantage is that teamwork can occur without knowing what the argument identifiers (names) will be Scott Marino - Caldwell College

  26. What is a driver? • It is a short main program whose only purpose is to call a function you wrote, so you can determine whether it meets specifications and works as expected Scott Marino - Caldwell College

More Related