1 / 26

Chapter 3

Chapter 3. Functions. Overview. 3.2 Using C++ functions Passing arguments Header files & libraries 3.4-5 Writing C++ functions Prototype Definition Parameters. Functions. Every C++ program must have a main function execution starts by looking for a “main”

Download Presentation

Chapter 3

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 3 Functions

  2. Overview • 3.2 Using C++ functions • Passing arguments • Header files & libraries • 3.4-5 Writing C++ functions • Prototype • Definition • Parameters

  3. Functions • Every C++ program must have a main function • execution starts by looking for a “main” • All other functions are called directly from main, or indirectly through a chain of function called from main • Function Calls • One function calls another by using the name of the called function next to ( ) enclosing an argument list ex. strlen(FirstName) • A function call temporarily transfers control from the calling function to the called function

  4. Function call syntax FunctionName(Argument List) • The argument list is a way for functions to communicate with each other by passing information • The argument list can contain 0 or more actual arguments, separated by commas

  5. What does the function do? • The function uses it actual arguments (inputs) • to return a calculation • To produce a side-effect • The function's return value is substituted for the function call in the expression • If the function does things other than a calculation (such as print a message to the screen), it is said to produce side-effects

  6. Example of calling a function #include <iostream> #include <cmath> using namespace std; int main() { float x, root; cout << "Enter a number: "; cin >> x; root = sqrt(x); cout << endl << "The square root of " << x << " is " << root << "." << endl; return 0; }

  7. Using library functions • We've already seen how to use library-provided objects like cin and cout to manage I/O • Libraries also contain the implementation of functions • A library has 2 parts • Interface(stored in a header file) tells what items are in the library and how to use them • Implementation (stored in another file) contains the definitions of the items in the library • #include <iostream> • Refers to the header file for the iostream library needed for use of cout and endl

  8. Some Mathematical Library Functions

  9. Some Mathematical Library Functions

  10. User Defined Functions • In addition to the functions in libraries, programmers can use functions that they write on their own • Using functions for common code segments can improve your program in several ways: • more readable • is easier to update • modular code

  11. Two parts of a function definition int Cube ( int n ) { return n * n * n; } • The heading declares the function’s name, specifying its return type and the name and type of each of its parameters • The body is a compound statement (block) which defines the behavior of the function Heading Body

  12. Name of function Type of return value Parameter list What is in a heading? int Cube ( int n )

  13. What is in a prototype? • A prototype looks like the heading, followed by a semicolon • parameter list must contain the type of each parameter, but names are optional • Prototypes are used to declare functions before they are defined float volume(float, float); //C++ does require parameter names in prototypes float volume(float height, float radius); //it is good style to include them

  14. #include <iostream> int GCD (int n1, int n2); // prototype using namespace std; int main() { int x, y; cout << "enter 2 positive integers: "; cin >> x >> y; cout << "The GCD of " << x << " and " << y << " is " << GCD( x, y) << endl; cout << "The GCD of " << x << " and 100 " << " is " << GCD( x, 100) << endl; cout << "The GCD of " << y << " and " << x*x - 1 << " is " << GCD( y, x*x -1) << endl; return 0; }

  15. A program with several functions function prototypes main function Square function Cube function

  16. A complete program: prototypes #include <iostream> int Square (int) ; // prototypes int Cube (int) ; using namespace std; int main() { cout << "The square of 27 is " << Square (27) << endl; cout << "The cube of 27 is " << Cube (27)<< endl; return 0; }

  17. A complete program: definitions int Square (int n) { return n * n; } int Cube (int n) { return n * n * n; }

  18. voidfunction has no return value void DisplayMessage(int n) { cout << "I have liked math for " << n << " years" << endl; return ; }

  19. Classified by location ArgumentsParameters Always appear in a function call, using variables whose scope is within the calling statement. Always appear in the function heading, or function prototype. Their scope is only within the function definition.

  20. Equivalent terms • The term "argument", as used in this slideshow, is equivalent to • Actual argument  used in our text • Actual parameter (confusing, but used by others) • The term "parameter", as used in this slideshow, is equivalent to • Formal parameter  used in our text

  21. Scope • A variable or constant can be declared outside of any function • Such variables or constants are known as globals • Globals can be accessed from anywhere in a program • Variables declared in a function are known as local variables • Local variables can only be referenced from within the function in which they are declared • You may use global constants, but no CS201 program should use global variables

  22. Drivers and Stubs • When building a large project, it is sometimes helpful to test each function in isolation • This allows the programmer to test a single function before the entire program is written • The function to be tested must be called • A simple main function, called a stub, can call the function with appropriate arguments • The function to be tested might call other functions which have not yet been implemented • Use functions which match the signature of these, but which do no real work, are called stubs

  23. Strings • C++ allows programmers to create new data types, called classes • Many classes have been implemented in the standard libraries • The <string> library implements the string class, which is used to store a sequence of characters

  24. Declaring a string • If you include the string library in your program, you can declare string variables #include <string> string first_name, last_name; • You can assign string literals to a string variable last_name = "Doe";first_name = "John";

  25. String operations • Mathematical operations, such as multiplication and division, have no meaning for strings • But there are operations that do make sense • String concatenationfull_name = first_name + last_name; • Nth position char first_initial = first_name[0];

  26. String member functions • There are several functions designed for strings • Called member functions, they are invoked differentlyint size = first_name.length(); int location = first_name.find("o"); • There is also a function that allows a programmer to read an entire line of input into a stringcin >> my_string; //reads one word getline(cin, my_string); //reads whole line

More Related