260 likes | 391 Views
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”
E N D
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” • 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
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
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
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; }
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
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
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
Name of function Type of return value Parameter list What is in a heading? int Cube ( int n )
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
#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; }
A program with several functions function prototypes main function Square function Cube function
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; }
A complete program: definitions int Square (int n) { return n * n; } int Cube (int n) { return n * n * n; }
voidfunction has no return value void DisplayMessage(int n) { cout << "I have liked math for " << n << " years" << endl; return ; }
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.
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
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
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
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
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";
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];
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