Download - Unit 7 - Object Oriented Programming / C++
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
1/16
________________________________________________________________________
Functions (Part I) QF002/7/1
UNIT 7
General Objective : To understand and apply the fundamental
concept of functions in C++ programming.
Specific Objective : At the end of the unit you should be able to :-
Describe the declaration of functions. Explain how to declare and initialize functions. Use functions in a program. Write and design a simple functionof a program.
OBJECTIVES
FUNCTIONS
(Part I)
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
2/16
________________________________________________________________________
Functions (Part I) QF002/7/2
7.0 Introduction
As shown in figure 7.1 below, when a function call is made, the program execution jumps to the function and finishes the task
assigned to the function. Then the program execution resumes
after the called function returns.
INPUT
To make large program manageable, programmers
modularize them into subprograms. These subprograms are
called functions. They can be compiled and tested separately
and being reused in different programs. This modularization
is one of the characteristics of successful object -oriented
software.
I see
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
3/16
________________________________________________________________________
Functions (Part I) QF002/7/3
7.1 Standard Header Files C++ provides numerous predefined functions. These functions
are stored in what are known as standard header files
(with extension .h)
Figure 7.2 shows the location of the HeaderFiles in Turbo C++Programming.
Start
Function Call
Function
Execution
Function
Return
End
Program
ExecutionFlow
Figure 7.1: A Program Execution Jumps To An Invoked Function
When A Function Call Is Made
noteA function is declared with a function prototype, which describes the
return value, the function name, and its parameter types. A function
can optionally be declared inline. A function prototype can also
declare default variables for one or more of the parameters.
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
4/16
________________________________________________________________________
Functions (Part I) QF002/7/4
Before you can use any of the functions contained in a headerfile, you must include the header file in your program. You can
do this by using the #include preprocessor directive place before
the function main().
The preprocessor directive has the general form#include < filename>
Figure 7.3 below shows a simple program that uses the standardheader files.
#include < iostream.h>
#include < math.h>#include < string.h>
{:::::::::::
:::::::::::
}
Figure 7.3 :Simple Program That Uses The Standard Header Files.
Figure 7.2 : Location Of The Header Files.
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
5/16
________________________________________________________________________
Functions (Part I) QF002/7/5
iostream.h - The name iostream.h stands for input/outputstream header file. The basic I/O objects contained in this header
files are cin for console input (the keyboard) and cout forconsole output (the screen). cin operator is used with the
operator >> while cout is used with operator
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
6/16
________________________________________________________________________
Functions (Part I) QF002/7/6
7.1.2 string.h string.h - This header file contains several string
manipulation functions. A partial list is given in Figure 7.3 .
Here, we will only highlight some of these functions.
FUNCTION RETURN VALUE
iIsalnum ( c )
isalpha ( c )
isascii ( c )
iscntri ( c )
isdigit ( c )
isgraph ( c )
islower ( c )
isodigit ( c )
isprint ( c )
isunct ( c )
isspace ( c )
isupper ( c )
isxdigit ( c )
toascii ( c )
tolower ( c )
toupper ( c )
Non-zero if c is alphanumeric; 0 otherwise
Non-zero if c is alphabetic; 0 otherwise
Non-zero if c is an ASCII character; 0 otherwise
Non-zero if c is a control character; 0 otherwise
Non-zero if c is a digit; 0 otherwise
Non-zero if c is a graphic character space excluded; 0 otherwise
Non-zero if c is a lowercase letter; 0 otherwise
Non-zero if c is an octal digit; 0 otherwise
Non-zero if c is a printable character; 0 otherwise
Non-zero if c is a punctuation character; 0 otherwise
Non-zero if c is a space, tab, form feed or newline character; 0 otherwise
Non-zero if c is uppercase letter; 0 otherwise
Non-zero if c is a hexadecimal digit; 0 otherwise
ASCII equivalent
Lowercase equivalent of c if it is a letter; unchanged otherwise
Uppercase equivalent of c if it is letter; changed otherwise
Figure 7.2 : List Of Function In Ctype.H
The header files that are
required by the #include
directive, like iostrem.h.
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
7/16
________________________________________________________________________
Functions (Part I) QF002/7/7
Figure 7.3: Header File That Contains Several String Manipulation
Functions.
FUNCTION ACTION
stsrcat ( ) Appends one string to another
strchr ( ) Scans a string for the first
occurrence of a given character
strcmp ( s1, s2 ) Compares one string to another
strcmpi ( s1, s2 ) Compares one string to another
without case sensitivity
strcpy ( s1, s2 ) Copies one string into another
strlen ( s ) Calculates the length of a string
strlwr ( ) Converts uppercase letters in a
string to lowercase
strrev ( s ) Reverses the string
strset ( s, c ) Sets all characters in a string to
given character
strstr ( ) Scans a string for the
occurrence of a given substring
strupr ( ) Converts lowers letter in a
string to uppercase
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
8/16
________________________________________________________________________
Functions (Part I) QF002/7/8
The function strlen ( ) calculates the length of string. It takesthe form
strlen (string)
Example:
Cout s2
7.1.3 stdlib.h stdlib.h - The standard library header file contains several
miscellaneous functions. Figure 7.4 gives a partial list o f
these functions. The argument type varies depending on the
function. They are void, int, char or some other types.
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
9/16
________________________________________________________________________
Functions (Part I) QF002/7/9
Figure 7.4 Standard Library Header File Contains Several Miscellaneous
Functions
7.2 Defining Header
Before you can use a function in a program, you must define itfirst. A function definition takes the form Retur_type
function_name (parameter-list) as follows:
FUNCTION ACTION
abort ( ) Terminates program without closing files and buffers
atof( s ) Converts (string) s to a float (double)
abs ( I ) Converts to the absolute value of i
atoi ( s ) Converts s to an integer
atol ( s ) Converts s to a long integer
exit ( s ) Terminate program after closing all files and buffers
labs ( l ) Takes the absolute value of a long integer
rand( ) Returns a random positive integer between 0 and
RAND_MAX (327676)
strset ( s, c ) Initialize the random number generator rand( )
{
variable
declaration
statements
return expression
}
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
10/16
________________________________________________________________________
Functions (Part I) QF002/7/10
where
y return type is any valid C++ date typey function_name is any valid C++ identifiery parameter-list is a list of parameter separated by commasy variable declaration is a list of variables declared within the
function
y return is a C++ keywordy expression is the value returned by the function
The return_type (placed before the function_name) indicatesthe type of value that the function will return when the return
statement(s) in the function is executed. The return value (given
by the ex pression in the return statement) may be of any valid
data type: int, double, char, etc. If you do not specify its type,
the function is assumed to return an integer value by default.
It is also possible that a function may not return any value to thecalling program function. In this case we say the function returns
a value of type void. If the return type is void, the return
statement may be dropped as we have done in our programs.
The function_name is the name of the function and the parameter-list is a list of parameter (sometimes called formal
parameters or dummy variables), separated by commas. The
parameter type tells what value the parameter will receive if the
function is invoked (called).
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
11/16
________________________________________________________________________
Functions (Part I) QF002/7/11
Every parameter in the parameter-list must include its name andtype. Thus the parameter-list for a function declaration takes the
form;Type var_1, type var_2, ... , type var_N
What are the differences
between the function prototype
and the function definition?
The function prototype declares the function; the
definition defines it. The prototype ends with a
semicolon; the definition need not. The declaration
can include the keyword inline and default values
for the parameters; the definition cannot. The
declaration need not include names for the
parameters; the definition must.
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
12/16
________________________________________________________________________
Functions (Part I) QF002/7/12
Test your comprehension before continuing the next input.
Check your answers on the next page.
7.1. If a function is not returning any value to the calling programfunction we say the function returns a value of type
___________.
7.2. Usually every parameter in the parameter-list must include its____________ and __________.
7.3. Normally in a program we must _______________ a functionbefore we can use it.
7.4. The return_type which placed before the _______________indicates the type of value that the function will return when the
return statement(s) in the function is executed.
Activity 7a
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
13/16
________________________________________________________________________
Functions (Part I) QF002/7/13
Make sure you have tried to answer all the questions given. You can
check your answers with the answers below .
7.1. void7.2. name , type7.3. define7.4. function_name
Feedback7a
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
14/16
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
15/16
________________________________________________________________________
Functions (Part I) QF002/7/15
You are approaching success, please answer the questions below. If you
have any problems, please discuss it with your lecturer. Wish you good
luck and all the best.
Question 7-1
a. C++ provides a set of header files containing commonly usedfunctions that programmers can use without having to write any code
for them. What are those functions called?
b. To simplify programming C++ provides a set off predefined function.Where are these functions stored?
c. What does iomaip.hcontain?d. What is the function of strlwr()?e. The header file contains several string manipulation functions. List
down 3 of them.
f. Before you can use a function in a program, you must define it first. Afunction definition takes the form as below:
{
variable declaration
statements
return expression
}
What is the ex pression for ?
Self-Assessment
-
8/6/2019 Unit 7 - Object Oriented Programming / C++
16/16
Functions (Part I) QF002/7/16
Make sure you have tried to answer all the questions given. You can check
your answers with the answers below.
Answer 7 - 1
a. These functions are called built -in, pre-defined or standard libraryfunction.
b. These functions are stored in header files.c. The iomaip.h header file contains function for formatting your output.d. It converts uppercase letters in a string to lowercase.e.
i. stsrcat ( )ii. strchr ( )
iii. strlen ( s )
f. The expression is the value returned by the function.
FeedbackOn Self-Assessment
CONGRATULATIONS
May success be withyou always..