C++ Functions

Introduction
A function is a sub program that is designed to perform a specific task and return a value.

There are two types of c++ functions: built in functions, user defined functions.

Built in functions
These are functions that already declared and are available in the programming language library. The user only needs to specify the file containing them then use the function.

Examples of built in functions: sqrt
 * 1)  or  file contains:

Returns the square root of a number //Example of program applying the sqrt function int main {  int number; cout<<"Enter a number \n"; cin>>number; cout<
 * 2) include

pow

Returns the result after raising a number to the specified power //Example of program applying the pow function int main {  int number; cout<<"Enter a number \n"; cin>>number; cout<
 * 2) include

Computes the hypotenuse of a right angled triangle //Example of program applying the hypot function int main {  int base, height; cout<<"Enter the value of base\n"; cin>>base; cout<<"Enter the value of height\n"; cin>>height; cout<<"The hypotenuse = "<
 * 2) include
 * 1)  file contains


 * 1)  file contains

User defined functions
These are modules of code that are declared and coded by the user to perform a specific task. Each C++ program must contain a main function (a function named main), whose main purpose is to control the execution of other functions that follow it. The main function is always the first to execute. By breaking the program into several smaller routines / modules (functions), it becomes easier to isolate problems, write correct programs faster, and come up with programs that are easier to maintain. C++ functions generally adhere to the following rules: 1. Every function must have a name. 2. The programmer comes up and assigns function names. 3. The names must follow the same rules that apply to naming variables: They can contain up to 32 characters, they must begin with a letter, and they can consist of letters, numbers, and the underscore (_) character. 4. All function names have one set of parentheses immediately following them. This helps C++ to differentiate them from variables. These parentheses may or may not contain something (parameters). 5. The body of each function (it starts immediately after the closing parenthesis of the function name, must be enclosed by braces. This means a block containing one or more statements makes up the body of each function.

Syntax: return type   functionname ( optional parameters) {            statements; return value; } Functions whose return type is void do not return a value and therefore do not require a return statement in their body.

Example: void details ( char initial, int score) {      cout<<"Students initial  "<  double rectanglearea  { int length, width, area; area = length * width; return area; }   int main {      cout<< rectanglearea; return 0; } 2. Writing the function below the main function. These option requires you to declare a function prototype. A prototype is a model. When prototyping, you inform C++ of the function’s parameter types  and its return value, if any. To prototype a function, copy the function’s definition line to the top of your program immediately before or after the #include  line). Place a semicolon at the end of the function definition line, and you have the prototype. The definition line (the function’s first line) contains the return type, the function name, and the type of each argument, so the function prototype serves as a model of the function that follows.

Example: #include using namespace std; double volumecylinder ( int radius, int height); //function prototype int main {     int r, h;      cout<<volumecylinder(r, h); return 0; }  double  volumecylinder ( int radius, int height) {         const double pie = 3.141592; return (pie * radius * radius * height); } NB: Function prototypes can also be declared locally e.g within the main function.

Overloaded functions
C++ enables you to have more than one function with the same name (polymorphism). In other words, you can have three functions called abs in the same program. Functions with the same names are called overloaded functions. Each overloaded function should differ in its argument list. Overloaded functions enable a program to have similar functions, but that work on different types of data. For example, the absolute value of a number is its positive equivalent. For instance, the absolute value of 10.65 is 10.65 and the absolute value of –10.65 is 10.65.

Example: int intabs(int n) // Returns the absolute value of an integer. {         if (n < 0) { return (n * -1); } // Makes positive. else { return (n); } // Already positive. }   float fltabs(float m) // Returns the absolute value of a float. {        if (m < 0.0) { return (m * -1.0); } // Makes positive. else { return (m); } // Already positive. }

Program Example: // Overloads two absolute value functions. #include  // Prototype cout and cin. #include  // Prototype setprecision(2). int abs(int i); // abs is overloaded twice float abs(float x); // as shown by these prototypes. void main {    int iabs; // To hold return values. float fabs; int i = -15; // To pass to the two overloaded functions. float x = -64.53; iabs = ltabs(i); // C++ calls the integer abs. cout << “Integer absolute value of -15 is “ << iabs << “\n”; fabs = ltabs(x); // C++ calls the floating-point abs. cout << “Float absolute value of -64.53 is “ << setprecision(2) << fabs << “\n”; /* C++ calls the appropriate function that matches the parameters. */    return; } int ltabs(int n) // Returns the absolute value of an integer. {     if (n < 0) { return (n * -1); } // Makes positive. else { return (n); } // Already positive. } float ltabs(float m) // Returns the absolute value of a float. {     if (m < 0.0) { return (m * -1.0); } // Makes positive. else { return (m); } // Already positive. }

Recursive function
A recursive function is a function that calls itself.

Example: int factorial ( int n)  { return (n * (factorial(n -1));  }

Program Example: #include   int factorial (int n); //prototype int main {         int number; cout<<"Enter a number "<>number; if (number == 1) cout<<"The factorial of "<<n<<"! =  "<<number; else cout<<endl<<factorial(number); return 0; }    int factorial (int n)      { return (n * (factorial(n -1));     }

Linked pages

 * 1) C++ Data Types
 * 2) C++ Variables & Constants
 * 3) C++ Operators & Operator Precedence
 * 4) C++ Preprocessor Directives
 * 5) C++ Input/Output
 * 6) C++ Control Structures
 * 7) C++ Jump Statements
 * 8) C++ Data Conversion and Formatting
 * 9) C++ Arrays
 * 10) C++ Pointers
 * 11) C++ Strings
 * 12) C++ Classes and Objects
 * 13) C++ Struct
 * 14) C++ Exceptions
 * 15) C++ Namespaces
 * 16) C++ Type Casting
 * 17) C++ Sample Codes
 * 18) Free C++ Projects