PF Lecture 13 Functions Unit 1
PF Lecture 13 Functions Unit 1
Lecture 13
Functions Unit 1
Sameer Akram
PF – Lecture 13
Functions Unit 1
Sameer Akram Mirza
Functions
Previously, we learned that a C++ program is a collection of
functions.
One such function is main().
The programs in previous lectures, we used only the function
main(); the programming instructions are packed into one
function.
This technique, however, is good only for short programs.
For large programs, it is not practical (although it is possible)
to put the entire programming instructions into one function.
We must learn to break the problem into manageable pieces.
Coming lectures discuss the functions previously defined and
then discusses user-defined functions.
Functions Explanation
Let us imagine an automobile factory.
When an automobile is manufactured, it is not made from
basic raw materials; it is put together from previously
manufactured parts. Some parts are made by the company
itself; others, by different companies.
Functions are like building blocks.
They let you divide complicated programs into manageable
pieces. They have other advantages, too:
While working on one function, you 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.
Functions Explanation (Contd.)
Functions
User-defined Functions
Predefined Functions
void main()
{
double x;
cout << “The ceil ("<< x <<") = " << ceil(x) << endl;
cout << “The floor ("<< x <<") = " << floor(x) << endl;
system (“pause”);
}
11
Example L13E2: Standard C++ Character Functions *
#include <iostream>
#include <cctype> //character type functions
using namespace std;
void main()
{
char ch;
cout << "Enter a character: ";
cin >> ch;
12
Example L13E2: Standard C++ Character Functions *
if ( isdigit(ch) )
cout << "'" << ch <<"' is a digit!\n";
else
cout << "'" << ch <<"' is NOT a digit!\n";
}
13
Example 6-1: How to use predefined functions *
#include <iostream>
#include <cmath>
#include <cctype>
using namespace std;
int main()
{
int x;
double u, v;
cout << "Line 1: Uppercase a is “ //Line 1
<< static_cast<char>(toupper('a')) << endl;
u = 4.2;
v = 3.0;
cout << "Line 4: " << u << " to the power of "
<< v << " = " << pow(u, v) << endl; //Line 4
Example 6-1: How to use predefined functions (Contd.)
cout << "Line 5: 5.0 to the power of 4 = "
<< pow(5.0, 4) << endl; //Line 5
x = -15; //Line 8
cout << "Line 9: Absolute value of " << x
<< " = " << abs(x) << endl; //Line 9
system("pause");
return 0;
}
Example 6-1: Output ...
User-Defined Functions
18
Value-Returning Functions
The previous section introduced some predefined C++
functions such as pow(), abs(), islower(), and
toupper().
These are examples of value-returning functions
To use these functions in your programs, you must know the
name of the header file that contains the functions’
specification.
You need to include this header file in your program using the
include statement and know the depicted items:
Items needed to call any function in main()
Formal Parameter:
A variable declared in the function heading while defining the
function.
Actual Parameter:
A variable or expression listed in a call to a function.
Actual parameter is placed during the function call.
Value-Returning Functions – Parameters (Contd.)
Various parts of function larger()
Various calls to function larger()
Suppose that num, num1, and num2 are double variables.
Also suppose that num1 = 45.75 and num2 = 35.50.
Figure 6-2 shows various calls to the function larger.
Function Prototype
The function heading without the body of the function.
Program: FunctionAvgOf3Num
#include <iostream>
using namespace std;
int main ( )
{
int var1, var2, var3;
cout << "\n The average of " << var1 << ", " << var2
<< ", and " << var3 << " is = "
<< average ( var1, var2, var3 ); //function call
Program: FunctionAvgOf3Num (Contd.)
cout << endl;
system ( "pause" );
return 0;
}
//function definition
int average ( int x, int y, int z )
{
return ( x + y + z ) / 3;
}
35