You are on page 1of 16

Functions (Part I)

QF002/7/1

UNIT 7

FUNCTIONS (Part I)

OBJECTIVES
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 function of a program.

________________________________________________________________________

Functions (Part I)

QF002/7/2

INPUT

7.0

Introduction

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

 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.

________________________________________________________________________

Functions (Part I)

QF002/7/3

Start

Program Execution Flow

Function Call

Function Execution

Function Return

End

Figure 7.1: A Program Execution Jumps To An Invoked Function When A Function Call Is Made

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 Header Files in Turbo C++ Programming.

note

A 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.

________________________________________________________________________

Functions (Part I)

QF002/7/4

Figure 7.2 : Location Of The Header Files.

 Before you can use any of the functions contained in a header file, 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 standard header files.
#include < iostream.h> #include < math.h> #include < string.h> { ::::::::::: ::::::::::: } Figure 7.3 : Simple Program That Uses The Standard Header Files. ________________________________________________________________________

Functions (Part I)

QF002/7/5

 iostream.h - The name iostream.h stands for input/output stream header file. The basic I/O objects contained in this header files are cin for console input (the keyboard) and cout for console output (the screen). cin operator is used with the operator >> while cout is used with operator <<.  iomaip.h - The iomanip header file contains function for formatting your output. For example, the function setw (n) prints your output in n columns.

7.1.1 ctype.h

 ctype.h Figure 7.2 gives a partial list of function contained in the ctype.h (character type header file). This file contains function such as for determining whether a given character is alphanumeric, alphabetic, digital, uppercase or lowe rcase.

The function definition must match the function prototype in return type, name, and parameter list. Function names can be overloaded by changing the number or type of parameters; the compiler finds the right function based on the argument list.

________________________________________________________________________

Functions (Part I)

QF002/7/6

FUNCTION

RETURN VALUE

iIsalnum ( c ) Non-zero if c is alphanumeric; 0 otherwise 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 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

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.

The header files that are required by the #include directive, like iostrem.h.

________________________________________________________________________

Functions (Part I)

QF002/7/7

FUNCTION stsrcat ( ) strchr ( )

ACTION Appends one string to another Scans a string for the first occurrence of a given character

strcmp ( s1, s2 ) strcmpi ( s1, s2 )

Compares one string to another Compares one string to another without case sensitivity

strcpy ( s1, s2 ) strlen ( s ) strlwr ( )

Copies one string into another Calculates the length of a string Converts uppercase letters in a string to lowercase

strrev ( s ) strset ( s, c )

Reverses the string 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

Figure 7.3: Header File That Contains Several String Manipulation Functions.

________________________________________________________________________

Functions (Part I)

QF002/7/8

 The function strlen ( ) calculates the length of string. It takes the form
strlen (string)

Example:
Cout<<strlen (good day);

The above statement will display the value of 8 . The embedded blank in the string is also counted as a character.  The function strcmp ( ) expression takes the general form
strcmp ( *s1, *s2)

The function compares two strings s1 and s2 character by character until the corresponding character dif fers or until the end of the string is reached. They return a value of:
< 0, = 0, > 0, if s1 < s2 if s1 = s2 if s1 > 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.

________________________________________________________________________

Functions (Part I)

QF002/7/9

FUNCTION abort ( ) atof( s ) abs ( I ) atoi ( s ) atol ( s ) exit ( s ) labs ( l ) rand( ) strset ( s, c )

ACTION Terminates program without closing files and buffers Converts (string) s to a float (double) Converts to the absolute value of i Converts s to an integer Converts s to a long integer Terminate program after closing all files and buffers Takes the absolute value of a long integer Returns a random positive integer between 0 and RAND_MAX (327676) Initialize the random number generator rand( )

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 it first. A function definition takes the form Retur_type function_name (parameter-list) as follows:
{ variable declaration statements return expression }

________________________________________________________________________

Functions (Part I)

QF002/7/10

where
y return type is any valid C++ date type y function_name is any valid C++ identifier y parameter-list is a list of parameter separated by commas y variable declaration is a list of variables declared within the

function
y return is a C++ keyword y expression is the value returned by the function

 The return_type (placed before the function_name ) indicates the type of value that the function will return when the return statement(s) in the function is executed. The return value (given by the expression 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 the calling 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).

________________________________________________________________________

Functions (Part I)

QF002/7/11

 Every parameter in the parameter -list must include its name and type. Thus the parameter-list for a function declaration takes the form;
Type var_1, type var_2, . . . , type var_N

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.

What are the differences between the function prototype and the function definition?

________________________________________________________________________

Functions (Part I)

QF002/7/12

Activity 7a

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 program function 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 function before 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.

________________________________________________________________________

Functions (Part I)

QF002/7/13

Feedback 7a

Make sure you have tried to answer all the questions given. You can check your answers with the answers below.

7.1. 7.2. 7.3. 7.4.

void name , type define function_name

________________________________________________________________________

Functions (Part I)

QF002/7/14

Key Facts

 A 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.  The preprocessor directive has the general form #include < filename>  iostream.h stands for input/output stream header file  iomanip is a header file that contains function for formatting your output.  ctype.h gives a partial list of function contained in the ctype.h (character type header file).  string.h is a header file that contains several string manipulation functions.  stdlib.h is a standard library header file contains several miscellaneous functions.

________________________________________________________________________

Functions (Part I)

QF002/7/15

Self-Assessment

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 used functions 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.h contain? 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. A function definition takes the form as below: { variable declaration statements return expression } What is the expression for ?

________________________________________________________________________

Functions (Part I)

QF002/7/16

Feedback On Self-Assessment

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 library function. 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.


CONGRATULATIONS May success be with

you always..

________________________________________________________________________

You might also like