0% found this document useful (0 votes)
7 views

Lesson 8 - Functions

Uploaded by

pascalotieno1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lesson 8 - Functions

Uploaded by

pascalotieno1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

ICS 2104

OBJECT ORIENTED PROGRAMMING


LESSON 8

Functions
OBJECTIVE
8.1 Library Function
8.1.1 Standard Library Header Files
8.1.2 Mathematical Functions
8.1.3 Character Functions
8.2 User Defined Functions
8.2.1 Declaring the function.
8.2.2 Defining the function.
8.2.3 Inserting Argument To A Function
8.2.4 Declaring Return Type Of A Function
8.1 Library Functions
• C++ Standard Library Function
• The C++ Standard Library provides a rich
collection of functions for performing common
mathematical calculations, string
manipulations, character manipulations,
input/output, error checking and many other
useful operations.
• This makes the programmer's job easier,
because these functions provide many of the
capabilities programmers need.
8.1 Library Functions
• C++ Standard Library Function
• The C++ Standard Library functions are
provided as part of the C++ programming
environment.
• Header file names ending in .h are "old-style"
header files that have been superseded by the
C++ Standard Library header files
8.1.1 Standard Library Header Files

C++ Standard
Explanation
Library header file

<iostream> Contains function prototypes for


the C++ standard input and
standard output functions. This
header file replaces header file
<iostream.h>.
8.1.1 Standard Library Header Files

<iomanip Contains function prototypes for stream


> manipulators that format streams of
data. This header file replaces header
file <iomanip.h>.

<cmath> Contains function prototypes for math


library functions. This header file
replaces header file <math.h>.
8.1.1 Standard Library Header Files
<cstdlib> Contains function prototypes for
conversions of numbers to text, text to
numbers, memory allocation, random
numbers and various other utility
functions. This header file replaces header
file <stdlib.h>.
<ctime> Contains function prototypes and types for
manipulating the time and date. This
header file replaces header file <time.h>.
8.1.1 Standard Library Header Files

<cctype> Contains function prototypes for functions


that test characters for certain properties
(such as whether the character is a digit or
a punctuation), and function prototypes
for functions that can be used to convert
lowercase letters to uppercase letters and
vice versa. This
header file replaces header file <ctype.h>
8.1.1 Standard Library Header Files

<cstring> Contains function prototypes for C-style


string-processing functions. This header
file replaces header file <string.h>.
<cstdio> Contains function prototypes for the C-
style standard input/output library
functions and information used by them.
This header file replaces header file
<stdio.h>.
8.1.1 Standard Library Header Files

<fstream> Contains function prototypes for


functions that perform input from files
on disk and output to files on disk. This
header file replaces header file
<fstream.h>.
<climits> Contains the integral size limits of the
system. This header file replaces header
file <limits.h>.
8.1.1 Standard Library Header Files

<cassert> Contains macros for adding diagnostics


that aid program debugging. This
replaces header file <assert.h> from pre-
standard C++.
<cfloat> Contains the floating-point size limits of
the system. This header file replaces
header file <float.h>.
8.1.1 Standard Library Header Files

<string> Contains the definition of class string


from the C++ Standard Library
<vector>, These header files contain classes that
<list>, implement the C++ Standard Library
<deque>, containers. Containers store data during a
<queue>, program's execution.
<stack>,
<map>,
<set>,
<bitset>
8.1.1 Standard Library Header Files

<typeinfo> Contains classes for runtime type


identification
(determining data types at execution
time).
<exception>, These header files contain classes that
<stdexcept> are used for exception handling.
8.1.1 Standard Library Header Files
<memory> Contains classes and functions used by
the C++ Standard Library to allocate
memory to the C++ Standard Library
containers.
<sstream> Contains function prototypes for
functions that perform input from strings
in memory and output to strings in
memory.
8.1.1 Standard Library Header Files
<functional> Contains classes and functions used by C++ Standard
Library algorithms.
<iterator> Contains classes for accessing data in the C++ Standard
Library containers.
<algorithm> Contains functions for manipulating data in C++ Standard
Library containers.
<locale> Contains classes and functions normally used by stream
processing to process data in the natural form for
different languages (e.g., monetary formats, sorting
strings, character presentation, etc.).
<limits> Contains classes for defining the numerical data type
limits on each computer platform.
<utility> Contains classes and functions that are used by many C++
Standard Library header files.
8.1.1 Standard Library Header Files
Exercise 1
What is the output of the following program?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout <<"Enter your name:";
cin >> name;
cout<<"Your name is: "; cout<<name;
}
8.1.1 Standard Library Header Files
Solution 1
What is the output of the following program?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout <<"Enter your name:";
cin >> name;
cout<<"Your name is: "; cout<<name;
}
8.1.2 Mathematical Functions
• Mathematical Functions
• Some of the important mathematical functions in the header file
<cmath> are:
Function Meaning
sin(x) Sine of an angle x (measured in radians)
cos(x) Cosine of an angle x (measured in radians)
tan(x) Tangent of an angle x (measured in radians)
asin(x) Sin-1 (x) where x (measured in radians)
8.1.2 Mathematical Functions
• Mathematical Functions
• Some of the important mathematical functions in the header file
<cmath> are:
acos(x) Cos-1 (x) where x (measured in radians)
exp(x) Exponential function of x (ex)
log(x) logarithm of x
log 10(x) Logarithm of number x to the base 10
sqrt(x) Square root of x
pow(x, y) x raised to the power y
abs(x) Absolute value of integer number x
fabs(x) Absolute value of real number x
8.1.2 Mathematical Functions
Exercise 2
What is the output of the following program?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x = 25;cout<<"The value of x = 25";cout<<"\n";
cout <<"The square root of x = "; cout <<sqrt(x); cout<<"\n";
cout <<"The logarithm of x = "; cout <<log(x);cout<<"\n";
cout <<"The sin of angle x = "; cout <<sin(x);cout<<"\n";
cout <<"The absolute value of integer number x = "; cout <<abs(x);
}
8.1.2 Mathematical Functions
Solution 2
What is the output of the following program?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x = 25;cout<<"The value of x = 25";cout<<"\n";
cout <<"The square root of x = "; cout <<sqrt(x); cout<<"\n";
cout <<"The logarithm of x = "; cout <<log(x);cout<<"\n";
cout <<"The sin of angle x = "; cout <<sin(x);cout<<"\n";
cout <<"The absolute value of integer number x = "; cout <<abs(x);
}
8.1.3 Character Functions
8.1.3 Character Functions
All the character functions require <cctype> header
file. The following table lists the functions

Function Meaning
isalpha(c) It returns True if C is an uppercase letter and False if c is lowercase.
isdigit(c) It returns True if c is a digit (0 through 9) otherwise False.

isalnum(c) It returns True if c is a digit from 0 through 9 or an alphabetic


character (either uppercase or lowercase) otherwise False.
8.1.3 Character Functions
8.1.3 Character Functions
All the character functions require <cctype> header
file. The following table lists the functions
Function Meaning
islower(c) It returns True if C is a lowercase letter otherwise False.
isupper(c) It returns True if C is an uppercase letter otherwise False.
toupper(c) It converts c to uppercase letter.

tolower(c) It converts c to lowercase letter.


8.1.3 Character Functions
Exercise 3
What is the output of the following program?
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char x[] = {'A','B','c','d'};
cout << "The 2nd value of x = "; cout << x[1];
cout <<"\nIs B uppercase? : ";cout << isupper(x[1]);
cout <<"\nIs B lowercase? : ";cout << islower(x[1]);
}
8.1.3 Character Functions
Exercise 3
What is the output of the following program?
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char x[] = {'A','B','c','d'};
cout << "The 2nd value of x = "; cout << x[1];
cout <<"\nIs B uppercase? : ";cout << isupper(x[1]);
cout <<"\nIs B lowercase? : ";cout << islower(x[1]);
}
8.2 User Defined Function
• A function is a subprogram that acts on data and
often returns a value.
• A program written with numerous functions is
easier to maintain, update and debug than one
very long program.
• By programming in a modular (functional)
fashion, several programmers can work
independently on separate functions which can
be assembled at a later date to create the entire
project.
8.2 User Defined Function
• Each function has its own name.
• When that name is encountered in a program,
the execution of the program branches to the
body of that function.
• When the function is finished, execution returns
to the area of the program code from which it
was called, and the program continues on to the
next line of code
8.2 User Defined Function
• Function syntax
• It consists of three parts
• 1. Function declaration
• 2. Function definition
• 3. Function call
8.2 User Defined Function
• Function syntax
#include <iostream>
using namespace std;
functionName(); // 1. function declaration/prototype
int main()
{
FunctionName() // 3. function call
}
Datatype FunctionName()//
{
Statements(s); // 2. function definition

}
8.2 User Defined Function
• Example
• What is the output of the following program when you enter 40 and 30?
8.2 User Defined Function
• Example
• What is the output of the following program when you enter 40 and 30?
8.2 User Defined Function
• Creating User-Defined Functions involves the
following
• 1. Declaring the function.
• 2. Defining the function.
• 3. Inserting Argument To A Function
• 4. Declaring Return Type Of A Function
8.2.1 Declaring the Function
• Creating User-Defined Functions involves the
following
• 1. Declaring the function.
• The declaration, called the FUNCTION
PROTOTYPE, informs the compiler about the
functions to be used in a program, the argument
they take and the type of value they return.
8.2.2 Defining the Function
• 2. Defining the function.
• The function definition tells the compiler what
task the function will be performing.
• The function prototype and the function
definition must be same on the return type, the
name, and the parameters.
• The only difference between the function
prototype and the function header is a
semicolon.
8.2.2 Defining the Function
• 2. Defining the function.
• The function definition consists of the function
header and its body.
• The header is EXACTLY like the function
prototype, EXCEPT that it contains NO
terminating semicolon.
8.2.2 Defining the Function
8.2.3 Inserting Argument To A Function
• 3. Argument To A Function
• Sometimes the calling function supplies some
values to the called function.
• These are known as parameters.
• The variables which supply the values to a calling
function called actual parameters.
• The variable which receive the value from called
statement are termed formal parameters
8.2.3 Inserting Argument To A Function
• Consider the following example that evaluates the area of a
circle.

• Here radius is called actual parameter and r is called formal parameter


8.2.4 Declaring Return Type Of A
Function
• 4. Return Type Of A Function
• Exercise: What is the output of the following program when you enter 45?
8.2.4 Declaring Return Type Of A
Function
• 4. Return Type Of A Function
Solution: What is the output of the following program when you enter 45?

Output
Please enter a number: 45
Your number times two = 90
NEXT LESSON
9.1 OOP Concepts
9.2 Classes and Objects
9.2.1 Object declaration
9.2.2 Accessing Class Members
9.2.3 Defining Member Function of Class
9.3 Constructors and Destructors
9.3.1 Default Constructors
9.3.2 Parameterized Constructors
9.3.3 Copy Constructors
9.3.4 Destructors

You might also like