In C++, in order to use a function, it is kind of like using a variable, you must
Provide a function definition
Provide a function prototype
Call the function
If you are using a library function, the function has already benn defined and compiled for you. Including the correct header file is enough
Example:
// calling.cpp -- defining, prototyping, and calling a function #include <iostream.h> void simple(void); // function prototype int main(void) { cout << "main() will call the simple() function:\n"; simple(); // function call return 0; } // function definition void simple(void) { cout << "I'm just a simple country function.\n"; }
Syntax:
typename functionName(argumentlist) { statements return value; // value is of type typename, can be void }Example:
int bigger(int a, int b) { if (a > b) return a; else return b; }// One more example double cube(double x) { return x*x*x; }
Function prototyping can be thought as declaring a function
Often hidden in include files
Example:
// protos.cpp -- use prototypes and function calls #include <iostream.h> void cheers(int); // prototype: no return value double cube(double x); // prototype: returns a double int main(void) { cheers(5); // function call cout << "Give me a number: "; double side; cin >> side; double volume = cube(side); // function call cout << "A " << side <<"-foot cube has a volume of "; cout << volume << " cubic feet.\n"; cheers(cube(2)); // prototype protection at work return 0; } void cheers(int n) { for (int i = 0; i < n; i++) cout << "Cheers! "; cout << "\n"; } double cube(double x) { return x * x * x; }
C++ normally passes arguments by value
Numerical value of the argument is passed to the function, where it is assigned to a new variable
(e.g. double volume = cube(side);)double cube(double x) { return x * x * x; }When the function is called, the system creates variable called x and assigns it passed value of 5. So, the value is copied. (passed by value)
Thus, eash function has its own variables with their own values
We can pass as many arguments as we like
// twoarg.cpp -- a function with 2 arguments #include <iostream.h> void n_chars(char, int); int main(void) { int times; char ch; cout << "Enter a character: "; cin >> ch; while (ch != 'q') // q to quit { cout << "Enter an integer: "; cin >> times; n_chars(ch, times); // function with two arguments cout << "\nEnter another character or press the" " q-key to quit: "; cin >> ch; } cout << "The value of times is " << times << ".\n"; cout << "Bye\n"; return 0; } void n_chars(char c, int n) // displays c n times { while (n-- > 0) // continue until n reaches 0 cout << c; }
Function works well with Arrays
Example:
// arrfun1.cpp -- functions with an array argument #include <iostream.h> const int ArSize = 8; int sum_arr(int arr[], int n); // prototype int main(void) { int cookies[ArSize] = {1,2,4,8,16,32,64,128}; // some systems require preceding int with static to // enable array initialization int sum = sum_arr(cookies, ArSize); cout << "Total cookies eaten: " << sum << "\n"; return 0; } // return the sum of an integer array int sum_arr(int arr[], int n) { int total = 0; for (int i = 0; i < n; i++) total = total + arr[i]; return total; }
If we pass by value, a copy of the input parameter is actually made
So, change of the input value is not possible (since changing the copy is different than changing the original input value)
We have to pass by reference if we want to change the original input value
#include <iostream.h> // Pass by Reference Sample Program void testProc(int *); int main(void) { int myInt = 5; cout << "The original value of myInt is " << myInt << endl; // Now, call testProc testProc(&myInt); cout << "Now, after calling the procedure, the value of myInt is " << myInt << endl; return 0; } void testProc(int * x) { // Change the value of the input parameter *x = 99; return; }// The output will be:The original value of myInt is 5 Now, after calling the procedure, the value of myInt is 99
// arrfun2.cpp -- functions with an array argument #include <iostream.h> const int ArSize = 8; int sum_arr(int arr[], int n); int main(void) { int cookies[ArSize] = {1,2,4,8,16,32,64,128}; // some systems require preceding int with static to // enable array initialization cout << cookies << " = array address, "; // some systems require a type cast: unsigned (cookies) cout << sizeof cookies << " = sizeof cookies\n"; int sum = sum_arr(cookies, ArSize); cout << "Total cookies eaten: " << sum << "\n"; sum = sum_arr(cookies, 3); // a lie cout << "First three eaters ate " << sum << " cookies.\n"; sum = sum_arr(cookies + 4, 4); // another lie cout << "Last four eaters ate " << sum << " cookies.\n"; return 0; } // return the sum of an integer array int sum_arr(int arr[], int n) { int total = 0; cout << arr << " = arr, "; // some systems require a type cast: unsigned (arr) cout << sizeof arr << " = sizeof arr\n"; for (int i = 0; i < n; i++) total = total + arr[i]; return total; }
// The output will be:0x8f8cffe6 = array address, 16 = sizeof cookies 0x8f8cffe6 = arr, 2 = sizeof arr Total cookies eaten: 255 0x8f8cffe6 = arr, 2 = sizeof arr First three eaters ate 7 cookies. 0x8f8cffee = arr, 2 = sizeof arr Last four eaters ate 240 cookies
Use the const keyword to make a constant object
Once you have a const keyword, then, whatever variable (pointer or not) associated with it cannot be changed
int age = 39; const int *pt = &age;*pt += 1; // INVALID because pt points to a const int cin >> *pt; // INVALID because for the same reason*pt = 20; // INVALID because pt points to a const int age = 20; // VALID because age is not declared to be constconst float g_earth = 9.80; const float *pe = &g_earth; // VALID, use both ways, you cannot change the valueconst float g_moon = 1.63; float * pm = &g_moon; // INVALID, otherwise you can cheat in changing value
String can be either one of the following:
An array of char
A quoted string constant (also called a string literal)
A pointer-to-char set to the address of a string
Example:char hi[15] = "Richard";char * str = "Richard";int n1 = strlen(hi); // hi is &hi[0]int n2 = strlen(str); // pointer to charint n3 = strlen("Richard"); // address of string
/ strgfun.cpp -- functions with a string argument #include <iostream.h> int c_in_str(const char * str, char ch); int main(void) { char mmm[15] = "minimum"; // string in an array // some systems require preceding char with static to // enable array initialization char *wail = "ululate"; // wail points to string int ms = c_in_str(mmm, 'm'); int us = c_in_str(wail, 'u'); cout << ms << " m characters in " << mmm << "\n"; cout << us << " u characters in " << wail << "\n"; return 0; } // this function counts the number of ch characters // in the string str int c_in_str(const char * str, char ch) { int count = 0; while (*str) // quit when *str is '\0' { if (*str == ch) count++; str++; // move pointer to next char } return count; }
// The output will be3 m characters in minimum2 u characters in ululate
New addition to C
Polymorphism means having many forms, so function polymorphism lets a function to have many forms
The key is the function's signature (argument list)
Example:
void print(const char * str, int width); void print(double d, int width); void print(long l, int width); void print(int i, int width);