Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u 3.4-5 Writing C++ functions  Prototype  Definition.

Similar presentations


Presentation on theme: "Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u 3.4-5 Writing C++ functions  Prototype  Definition."— Presentation transcript:

1 Chapter 3 Functions

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

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

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

5 5 What does the function do? u The function uses it actual arguments (inputs)  to return a calculation  To produce a side-effect u The function's return value is substituted for the function call in the expression u 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 6 Example of calling a function #include 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 7 Using library functions u 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 u 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 u #include  Refers to the header file for the iostream library needed for use of cout and endl

8 8 Some Mathematical Library Functions

9 9

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

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

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

13 13 What is in a prototype? u 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 14 #include 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 15 A program with several functions Square function Cube function function prototypes main function

16 16 A complete program: prototypes #include 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 17 A complete program: definitions int Square (int n) { return n * n; } int Cube (int n) { return n * n * n; }

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

19 19 Classified by location 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. Arguments Parameters

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

21 21 Scope u 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 u 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 u You may use global constants, but no CS201 program should use global variables

22 22 Drivers and Stubs u 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 u The function to be tested must be called  A simple main function, called a stub, can call the function with appropriate arguments u 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 23 Strings u C++ allows programmers to create new data types, called classes u Many classes have been implemented in the standard libraries  The library implements the string class, which is used to store a sequence of characters

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

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

26 26 String member functions u There are several functions designed for strings  Called member functions, they are invoked differently int 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 string cin >> my_string; //reads one word getline(cin, my_string); //reads whole line


Download ppt "Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u 3.4-5 Writing C++ functions  Prototype  Definition."

Similar presentations


Ads by Google