You are on page 1of 28

TOPIC 4:

FUNCTION (PART 1)
OBJECTIVES

In this chapter you will:


 Learn about standard (predefined) functions
 Discover how to use them in a program
 Learn about user-predefined 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
OBJECTIVES

 Learn about user-predefined functions (cont.)


 Learn how to construct and use void functions in a program
 Discover the difference between value and reference
parameters
 Explore reference parameters and value-returning functions
 Learn about the scope of an identifier
 Examine the difference between local and global identifiers
INTRODUCTION

 A function is a block of code that design to perform a specific


task
 Often called modules
 Every C++ program contains at least one function (main)
 Some functions are built-in functions and defined in language
libraries (predefined function).
 Other, called user-defined functions, are written by programmer;
define in a program
 Return-value function
 Void function
PREDEFINED FUNCTIONS

 Predefined functions are organized as a collection of libraries called


header files
 To use a predefined function, you need the name of the appropriate
header file

Mathematical Math functions are contained in the header file such as


functions <cmath> for:
• pow (x,y)
• sqrt (x)
• abs (x)
PREDEFINED FUNCTIONS

String String functions are contained in the header file such as


functions <cctype> for:
• getline : to read strings with blanks

String functions are contained in the header file such as


<cstring> for:
• strcpy : to copy one string to another
• strcmp : take two strings as arguments and
compare these two strings
PREDEFINED FUNCTIONS (CONT.)

Manipulator Manipulator functions are contained in the header file such as


functions <iomanip>:
•setw ( )
 set the width of the filed assigned for the output
•setprecision ( )
 set the total number of digits to be displayed when
floating numbers are printed
•fixed :
 sets floating-point numbers in a fixed decimal format
PREDEFINED FUNCTIONS
PREDEFINED FUNCTIONS
PREDEFINED FUNCTION (STRING)
STRING COMPARISON
STRING COMPARISON (CONT.)
EXAMPLE
EXAMPLE (CONT.)
EXAMPLE (CONT.)
EXAMPLE (CONT.)
EXAMPLE (CONT.)

Output:
EXAMPLE (CONT.)

Output:
USER DEFINED FUNCTION

 Problem breaks up into small pieces of solution


 Separate solution for each module, but then combined in a whole
main program
 Practically, each function will solve only one particular problem
 Types of user-defined function:
 Return-value function : have a return type
 Void function : do not have a return type
USER DEFINED FUNCTION (CONT.)

 Every function consists of two parts:


 A function header is the initial line of code
which always has three parts;
 Return type of the function
 Name of the function
 Types and name of any variables
enclosed in parentheses, and which
the function receives
 A function body
A) RETURN VALUE FUNCTION
 All return value function perform a task and then precisely return one value
 Example:

Main
Program

Return Value Function


EXAMPLE (RETURN VALUE FUNCTION)
PARAMETERS

Actual Parameters

Formal Parameters
 Function prototype (declaration)
 Tells the compiler about a function’s name, return type, and parameters
 Function definition
 Provides the actual body of the function
 Function call
 A statement that executes a function
 Formal Parameter
 Is a reference parameter
 These are the variables written or declared in function definition/prototype, and
received their values when a call to that function is made
 Actual Parameter
 The values/variables passed while calling a function
SYNTAX
SYNTAX (CONT.)

 The formal parameter list can be empty


 If the formal parameter list is empty
 Parentheses are still needed
 Function heading of the value-returning function takes the following
form:
functionType functionName ( )

 In a function call the actual parameter is empty


 A call to a value-returning function with an empty formal parameter list:

functionName ( )
FLOW OF EXECUTION

 Execution always begin at


 The first statement in the function main no matter where main is
placed in the program
 Other functions are executed only when they are called
 Function prototypes appear before any function definition
 The compiler translated these first
 The compiler can then correctly translate a function call
 A function call statement results in
 Transfer of control to the first statement in the body of the called
function
FLOW OF EXECUTION (CONT.)

 After the last statement of the called function is executed


 Control is passed back to the point immediately following the
function call
 A value-returning function returns a value
 After executing the function
 The value that the function returns replaces the function call
statement

You might also like