2. 2
C++ Programming
Objectives
• Learn about standard (predefined) functions and
discover how to use them in a program
• Learn about user-defined functions
• Examine value-returning functions, including actual
and formal parameters
• Explore how to construct and use a value-returning,
user-defined function in a program
6. 6
C++ Programming
•Functions are like building blocks
•They allow complicated programs to be
divided into manageable pieces
•Some advantages of functions:
– A programmer can focus on just that part of
the program and construct it, debug it, and
perfect it
– Different people can work on different
functions simultaneously
– Can be used in more than one place in a
program or in different programs
Functions (Continued)
7. 7
C++ Programming
• A function has a name.
•A function may has argument(s).
•A function may produce a result.
Functions (Continued)
8. 8
C++ Programming
Predefined Functions
• In algebra, a function is defined as a rule
or correspondence between values, called
the function’s arguments, and the unique
value of the function associated with the
arguments
• If f(x) = 2x + 5, then f(1) = 7, f(2) = 9, and
f(3) = 11
• 1, 2, and 3 are arguments
• 7, 9, and 11 are the corresponding values
11. 11
C++ Programming
Predefined Functions (Continued)
• Predefined functions are used in a
program as if they are single variables.
• When a function is used, the compiler
uses to the value that the function
produces and returns.
• A function value can be printed, used in
an assignment statement, or used as part
of an expression.
12. 12
C++ Programming
Predefined Functions (Continued)
• cout << cos(3.1); print the value of
cos(3.1).
• y = 5*cos(3.1)–7; multiply cos(3.1) by 5
and subtract 7 to, then assign the result
to y.
• You cannot extract / assign a value to a
function.
cin >> cos(3.1); is illegal.
cos(3.1) = 5 * x + 2; is illegal.
13. 13
C++ Programming
Predefined Functions (Continued)
• Predefined functions are organized into separate
libraries.
• To use a predefined function, the program must
include the header file that contains the
function’s specifications.
• To use the predefined math functions sqrt(),
pow(), … must have #include <cmath>
• To use the I/O functions, must have
#include <iostream>
• Example of another header file that you have
seen so far is ….
14. 14
C++ Programming
Predefined Functions (Continued)
• The function rand() generates random one of the
numbers 0, 1, 2, 3, 4, …
• The function rand() %2 generates 0 or 1
• The function 1 + rand() %6 generates 1, 2, 3, 4, 5, 6
• The function m + rand() % (n-m+1) generates a
random number in the interval [m, n]
• To use rand() use the library #include<cstdlib>
• To generate different random numbers use
srand(time(0)) on a line by itself
• To use time(0) use the library #include<ctime>
15. 15
C++ Programming
Write a program that reads the (x, y) coordinates of a point in the Cartesian
coordinates, converts to polar coordinates (r, ), and prints the polar
coordinates.
𝑟 = 𝑥2 + 𝑦2, 𝜃 = 𝑡𝑎𝑛−1
𝑦
𝑥
Program: toPolar.cpp
Program: toPolar.exe
Program: CartesiantoPolar()
16. 16
C++ Programming
User-Defined Functions
• User defined functions are like predefined
functions but the programmer has to write
them.
• Example:
– Find the max of two numbers.
17. 17
C++ Programming
#include <iostream>
using namespace std;
double larger(double x, double y);
int main()
{
double num1, num2, maximum;
cin >> num1 >> num2;
maximum = larger(num1, num2);
cout << "The maiximum is " << maximum << endl;
return 0;
}
double larger(double x, double y)
{
double max;
if (x >= y)
max = x;
else
max = y;
return max;
}
18. 18
C++ Programming
#include <iostream>
using namespace std;
double larger(double x, double y);
int main()
{
double num1, num2, largest;
cin >> num1 >> num2;
largest = larger(num1, num2);
cout << "The largest is " << largest << endl;
return 0;
}
double larger(double x, double y)
{ double max;
if (x >= y)
max = x;
else
max = y;
return max;
}
Function
header/heading
Function body
Function formal
parameters
Function actual
parameters
Function
type
Function prototype
Function definition
19. 19
C++ Programming
User-Defined Functions (Continued)
• The number of formal parameter must
match the number of actual parameters
• The data type of each actual parameter
must match its corresponding formal
parameter.
• A function call in a program results in
the execution of the body of the called
function.
20. 20
C++ Programming
User-Defined Functions (Continued)
• The function syntax:
functionType functionName(formal parameters)
{
statements
}
• Some functions have no parameters,
such as int main().
• Some functions return nothing, so the
return value is void.
21. 21
C++ Programming
User-Defined Functions (Continued)
• A C++ program is made out of a set of
functions.
• main() is just a user defined function.
• Execution always begins at the first statement
in the function main.
• Other functions are executed only when they
are called.
22. 22
C++ Programming
User-Defined Functions (Continued)
• When a return statement executes
– Function immediately terminates.
– Control goes back to the caller.
– The function call statement is replaced by
the returned value.
• When a return statement executes in the
function main(), the program terminates.
23. 23
C++ Programming
User-Defined Functions (Continued)
• A user-defined function can be placed before
the function main. In that case the function
prototype is not necessary.
• A function that returns a value is called a value-
returning functions and it must have a data
type.
• A function that does not return a value (i.e.
does not have a return statement) is called a
void function and it does not have a type (its
type is void).
void funtionName(formal parameters)
24. 24
C++ Programming
Write a program that reads a positive integer num, calls the function
isPerfect() which checks, and prints whether num is a perfect number or not.
A perfect number is a positive integer that is equal to the sum of its proper
divisors. For example, 6 is a perfect number because 6 = 1 + 2 + 3, also 28
is a perfect number because 28 = 1 + 2 + 4 + 7 + 14.
Program: isPerfect.cpp
Program: isPerfect.exe
Program: isPerfect()
25. 25
C++ Programming
Write a program that reads a positive integer num, calls the function
isPrime() which checks, and prints whether num is a prime number or not.
A prime number is a positive integer that is divisible by 1 and itself only. For
example, 7, 11, 13, 17, … are prime numbers, while 15 = 3 * 5 is not a prime
number.
Program: isPrime.cpp
Program: isPrime.exe
Program: isPrime()
26. 26
C++ Programming
Write a program that reads a positive integer N, calls the function Fibonacci()
which calculates, and prints the Nth Fibonacci number.
Fibonacci numbers satisfy the following recurrence relation
𝑎0 = 1, 𝑎1 = 1, 𝑎𝑛+2 = 𝑎𝑛+1 + 𝑎𝑛, 𝑛 0
Each Fibonacci number is the sum of the two previous Fibonacci numbers.
Program: Fibonacci.cpp
Program: Fibonacci.exe
Program: Fibonacci()
27. 27
C++ Programming
Write a program that reads two positive integers n and k, calls the function
binomial() which calculates, and prints the binomial coefficient of n and k.
Fibonacci numbers satisfy the following recurrence relation
𝐶𝑘
𝑛
=
𝑛
𝑘
=
𝑛!
𝑛 − 𝑘 ! 𝑘!
Program: binomial.cpp
Program: binomial.exe
Program: binomial()
28. 28
C++ Programming
Write a program that prints a menu and based on the user choice, the
program preforms a job.
Program: menu.cpp
Program: myLibrary.h
Program: menu.exe
Program: Menu