C++: Functions

Author: Ronald S. Holland
Total Application Works


To contact us

HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

© 2002 - 2016 All Rights Reserved Total Application Works

  • Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)
    <title>Example of a link </title>
    <a href="http://sumtotalz.com/TotalAppsWorks/CPP/CPP_Functions.html">
    Functions </a>


The purpose of this tutorial is to briefly introduce you to c++ functions. c++, as a programming language, is widespread and can be found on nearly all computers. This tutorial assumes that you have some prior programming experience. It is not intended to be a comprehensive discussion on c++ or programming. As we continue to ease into c++, we will briefly discuss the construction and use of functions in the next section.

Partial list of C++ functions

Have we ever used functions before? You should have an idea of C++ functions uses since we have already used them and defined one in the guise of main(void).

/** *********************************************************
 * The main() function is the entry point that is called 
 * when of the program is loaded into memory.
int main() {

   Bin_to_Dec() ;

   return 0;
Figure 1: main() function example

cin.get() is another example of a function. In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive. Below is a partial list of c++ built-in functions.

  1. abs
  2. acos
  3. ceil
  4. fgets
  5. floor
  6. fopen
  7. fputs
  8. log
  9. max
  10. min
  11. power
  12. sqrt
When you use a c++ function in your program, it is included in your program at compile-time. Functions that a programmer writes will generally require a prototype. Just like a template, the prototype informs the compiler what the function:
  • will return,
  • will be called,
  • the arguments the function can be passed.

The general format for a prototype is

What is the general format for a prototype? Consider the following examples:

return-type function_name ( arg_type arg1, ..., arg_type argN ); 
The following code snippet is an example of a function prototype.
void Bin_to_Dec() ;
In contrast, a function definition declares the body of the function, in a manner similar to a variable declaration.

Requirements for a user defined sqrt() function

Following are the requirements for a user defined sqrt() function:

  1. Write a c++ For_13 program that contains a:
    • main() function that is used as a entry point when the program is loaded;
    • void calcSqrt( double n ) function that calculates the square root of the passed number;
      • Prints to screen the result of the square root calculation;
    • double sqrt_2( double m ) function that calculates the square root of the passed number;
      • Prints to screen the result of the square root calculation;
      • Prints to screen the result of a call to the compiler library sqrt() calculation;
  2. This program should iterate until the user enters a -1.

/** *****************************************************
 * CPP_For_13.cpp - this program that takes user input to
       calculate the sqrt of that entered number..

#include <iostream>
using namespace std;

#include <math.h>

void calcSqrt( double num ) {
   double a =  num;
   double x = 1;
		// For loop to get the square root  
		//value of the entered number.
   for( int i = 0 ; i < num ; i++) {
      x = 0.5 * ( x + a / x );
   cout << "calcSqrt: sqrt of " << num << " is " << x << endl ;
double sqrt_2( double m ) {
   double ii = 0 , x1, x2 ;

   while( (ii * ii) <= m )
      ii += 0.1 ;
      x1 = ii ;

      for( int  iii = 0 ; iii < 10 ; iii++) {
         x2  = m ;
         x2 /= x1 ;
         x2 += x1 ;
         x2 /= 2 ;
         x1  = x2 ;

      return x2;
/** **************************************************
 * The main() function is the entry point that called 
 * when this program is loaded into memory.
int main() {
   float num ;

   for( int ii = 0 ; ii < 100 ; ii++ ) {
      cout<<"Enter a Number: or -1 to quit " ;
      cin >> num;

      if(  num < 0 )
         break ;

      cout <<"Square Root using sqrt_2()= " << sqrt_2(num) << endl
	   << "Square Root using the compiler supplied sqrt() function is: " 
           << sqrt(num) << endl ;

	  calcSqrt( num ) ;

   return 0;

Figure 2: user created sqrt() function example



    In logic, mathematics, and computer science, the arity of a function or operation is the number of arguments or operands that the function takes.


    Computer scientists usually credit the Structured program theorem to a 1966 paper by Corrado Böhm and Giuseppe Jacopini.

Boolean expression

    is a programming language expression that when evaluated produces a Boolean value, i.e. one that is either true or false. A Boolean expression may be composed of a combination of:
    1. the Boolean constants true or false;
    2. Boolean-typed variables;
    3. Boolean-valued operators; and
    4. Boolean-valued functions.

conditional operator ? :

    The conditional operator ? : is a ternary operator (it takes three operands). It is of the form:
    (condition ? expression 1 : expression 2 ) 

  • If the condition evaluates to true then expression 1 is executed; otherwise, expression 2 is executed.

control structure

    allows for a change of linear sequence/flow of program execution, based on some condition evaluating to true.


    A flowchart is a method of diagraming the layout and/or flow of a program or algorith. This flow is shown using figures of various kinds, and their order by connecting them with arrows.

Function Definition

    they supply function bodies — the code that makes up the function. For example:
    void Bin_to_Dec() {   
       int  bin, ii, iii, num ;
       int  len, total = 0 ;
       char binary[ 33 ] ;
       cout << "Enter a binary number (10 digits maximum): ";
       gets( binary ) ; 

Function prototype

    is a declaration of a function that omits the function body but does specify the function's return type, name, arity and argument types.


    is a compact and informal high-level design description (usually written in a spoken language) of a computer programming algorithm that adheres to the structural conventions of a programming language. Pseudocode is intended to be read by humans rather than by machine.

HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

To contact us

© 2002 - 2016 All Rights Reserved Total Application Works