Road Map for Today
• Functions
A Quick Recap
Various constructs to help us write useful programs
• Variables & Input/output statements
• Arithmetic & Assignment statements
• Sequential and conditional statements
• Iteration/looping constructs
• array
        All encapsulated within “main( )”
Overview of This Lecture
• Break away from the monopoly of “main()” Have other sub-units of a
  program that can compute Reduce the burden of programming each
  and everything in “main()”
• A complex problem is often easier to solve by dividing it into several
smaller parts, each of which can be solved by itself. (divide and
conquer)
Introduction to Functions
• Functions are the building blocks of C++
programs.
• A function is a block of code that performs
some ACTION.
Introduction to Functions
• The statements written in a function are executed when it is called by
its name.
• Each function has a UNIQUE NAME.
• One function can be called MULTIPLE TIMES in a single program.
Advantages of Functions
• Easier to Understand
• Easier to Modify
• Easier to Maintain
• Provide Program Reusability
• Separate the Concept (what is done) from the Implementation (how
it is done)
C++ Function – syntax
• Function Prototypes
• Function Definitions
• Function Call
Function Prototypes
• A function prototype is a declaration of a
  function that specifies the function's name but
  omits the function body.
Function Definition.
• A set of statements that explains what a function does is called
  Function Definition.
function definition
In an program, a function definition can be written at:
• Before main() function
• After main() function
• In a separate file
Function Prototype - Example
Function Definition - Example
Function Call
• A statement that activates a function is known as a Function Call.
Function Call
The following steps take place
when a function is called:
1. The control moves to the
function that is called.
2. All statements in function
body are executed.
3. Control returns back to calling
function.
Function Call – Syntax
C++ Functions
C++ allows the use of both internal and external functions.
• Internal Functions – User defined
• External functions – Pre-defined (e.g rand, sqrt, etc.) are usually
grouped into specialized libraries (e.g., iostream, stdlib, math, etc.)
Internal Functions
C++ programs usually have the following form:
• // include statements
• // function prototypes
• // main() function – has the function call
• // function definitions
Internal Functions
C++ programs usually have the following form:
• // include statements
• // function definitions
• // main() function – has the function call
External Functions
C++ Function – Example (Cube)
#include <iostream>
using namespace std;
int Cube (int n);
int main()
{
int yourNumber = 14;
int myNumber = 9;
cout << "My Number =" << myNumber ;
cout << " its cube is" << Cube (myNumber) << endl ;
cout << "Your Number = " << yourNumber ;
cout << " its cube is " << Cube (yourNumber) << endl ;
}
int Cube (int n)
{
return n * n * n ;
}
C++ Function – Example (AddNumbers)
#include <iostream>
using namespace std;
int addition (int a, int b)
{
int r;
r = a + b;
return r;
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
}
Lab task1
Write a program which calls a function which prints ten asterisks (*) in
line. (**********).
In this example main function just calls the function named asterisks
which just prints the line of ten asterisks.
Solution
• #include <iostream>
• using namespace std;
• void asterisks ()
•{
• for (int i=0; i<10; i++)
• cout << "*";
•}
• int main ()
•{
• asterisks ();
•}
Lab task2-
• Write a function largerDistance() that takes two
  distance values as arguments and returns the
  larger one. Write a main() program that takes
  two distance values, calls the largerDistance()
  function to find out the larger value, and prints
  the larger value.