SlideShare a Scribd company logo
1 of 51
Chapter 6:
User-Defined Functions
Objectives
In this chapter, you will:
– Learn about standard (predefined) functions
– Learn about user-defined functions
– Examine value-returning functions
– Explore how to construct and use a value-returning,
user-defined function
– Learn about function prototypes
– Learn how to construct and use void functions
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 2
Objectives (cont’d.)
– 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
– Discover static variables
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 3
Objectives (cont’d.)
– Learn how to debug programs using drivers and stubs
– Learn function overloading
– Explore functions with default parameters
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 4
Introduction
• Functions are often called modules
• They are like miniature programs that can be
combined to form larger programs
• They allow complicated programs to be divided
into manageable pieces
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 5
Predefined Functions
• In C++, a function is similar to that of a
function in algebra
– It has a name
– It does some computation
• Some of the predefined mathematical
functions are:
sqrt(x)
pow(x, y)
floor(x)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 6
Predefined Functions (cont'd.)
• Predefined functions are organized into
separate libraries
– I/O functions are in iostream header
– Math functions are in cmath header
• To use predefined functions, you must include
the header file using an include statement
• See Table 6-1 in the text for some common
predefined functions
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 7
User-Defined Functions
• Value-returning functions: have a return type
– Return a value of a specific data type using the
return statement
• Void functions: do not have a return type
– Do not use a return statement to return a value
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 8
Value-Returning Functions
• To use these functions, you must:
– Include the appropriate header file in your
program using the include statement
– Know the following items:
• Name of the function
• Number of parameters, if any
• Data type of each parameter
• Data type of the value returned: called the type of the
function
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 9
Value-Returning Functions (cont’d.)
• Can use the value returned by a value-
returning function by:
– Saving it for further calculation
– Using it in some calculation
– Printing it
• A value-returning function is used in an
assignment or in an output statement
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 10
Value-Returning Functions (cont’d.)
• Heading (or function header): first line of the
function
– Example: int abs(int number)
• Formal parameter: variable declared in the
heading
– Example: number
• Actual parameter: variable or expression
listed in a call to a function
– Example: x = pow(u, v)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 11
Syntax: Value-Returning Function
• Syntax:
• functionType is also called the data type
or return type
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 12
Syntax: Formal Parameter List
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 13
Function Call
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
• Syntax to call a value-returning function:
14
Syntax: Actual Parameter List
• Syntax of the actual parameter list:
• Formal parameter list can be empty:
• A call to a value-returning function with an
empty formal parameter list is:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 15
return Statement
• Function returns its value via the return
statement
– It passes this value outside the function
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 16
Syntax: return Statement
• Syntax:
• In C++, return is a reserved word
• When a return statement executes
– Function immediately terminates
– Control goes back to the caller
• When a return statement executes in the
function main, the program terminates
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 17
Syntax: return Statement (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 18
Function Prototype
• Function prototype: function heading without
the body of the function
• Syntax:
• Not necessary to specify the variable name in
the parameter list
• Data type of each parameter must be
specified
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 19
Value-Returning Functions: Some
Peculiarities
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 20
Value-Returning Functions: Some
Peculiarities (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 21
Flow of Compilation and Execution
• Execution always begins at the first statement
in the function main
• Other functions are executed only when called
• Function prototypes appear before any
function definition
– Compiler translates these first
• Compiler can then correctly translate a
function call
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 22
Flow of Compilation and Execution
(cont’d.)
• Function call transfers control to the first
statement in the body of the called function
• When the end of a called function is executed,
control is passed back to the point immediately
following the function call
– Function’s returned value replaces the function
call statement
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 23
Void Functions
• User-defined void functions can be placed
either before or after the function main
• If user-defined void functions are placed after
the function main
– The function prototype must be placed before the
function main
• Void function does not have a return type
– return statement without any value is typically
used to exit the function early
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 24
Void Functions (cont’d.)
• Formal parameters are optional
• A call to a void function is a stand-alone
statement
• Void function definition syntax:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 25
Void Functions (cont’d.)
• Formal parameter list syntax:
• Function call syntax:
• Actual parameter list syntax:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 26
Void Functions (cont’d.)
• Value parameter: a formal parameter that
receives a copy of the content of
corresponding actual parameter
• Reference parameter: a formal parameter
that receives the location (memory address)
of the corresponding actual parameter
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 27
Value Parameters
• If a formal parameter is a value parameter:
– The value of the corresponding actual parameter
is copied into it
– Formal parameter has its own copy of the data
• During program execution
– Formal parameter manipulates the data stored in
its own memory space
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 28
Reference Variables as Parameters
• If a formal parameter is a reference
parameter
– It receives the memory address of the
corresponding actual parameter
• During program execution to manipulate data
– Changes to formal parameter will change the
corresponding actual parameter
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 29
Reference Variables as Parameters
(cont’d.)
• Reference parameters are useful in three
situations:
– Returning more than one value
– Changing the actual parameter
– When passing the address would save memory
space and time
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 30
Value and Reference Parameters and
Memory Allocation
• When a function is called
– Memory for its formal parameters and its local
variables is allocated in the function data area
• For a value parameter, the actual parameter’s
value is copied into the formal parameter’s
memory cell
– Changes to the formal parameter do not affect
the actual parameter’s value
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 31
Value and Reference Parameters and
Memory Allocation (cont’d.)
• For a reference parameter, the actual
parameter’s address passes to the formal
parameter
• Both formal and actual parameters refer to the
same memory location
• During execution, changes made to the formal
parameter’s value permanently change the actual
parameter’s value
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 32
Reference Parameters and Value-
Returning Functions
• Can also use reference parameters in a value-
returning function
– Not recommended
• By definition, a value-returning function
returns a single value via return statement
• If a function needs to return more than one
value, change it to a void function and use
reference parameters to return the values
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 33
Scope of an Identifier
• Scope of an identifier: where in the program
the identifier is accessible
• Local identifier: identifiers declared within a
function (or block)
• Global identifier: identifiers declared outside
of every function definition
• C++ does not allow nested functions
– Definition of one function cannot be included in
the body of another function
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 34
Scope of an Identifier (cont’d.)
• Rules when an identifier is accessed:
– Global identifiers are accessible by a function or
block if:
• Declared before function definition
• Function name different from identifier
• Parameters to the function have different names
• All local identifiers have different names
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 35
Scope of an Identifier (cont’d.)
• Rules when an identifier is accessed (cont’d.):
– Nested block
• Identifier accessible from declaration to end of block in
which it is declared
• Within nested blocks if no identifier with same name
exists
– Scope of function name similar to scope of
identifier declared outside any block
• i.e., function name scope = global variable scope
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 36
Scope of an Identifier (cont’d.)
• Some compilers initialize global variables to
default values
• Scope resolution operator in C++ is ::
• By using the scope resolution operator
– A global variable declared before the definition of
a function (or block) can be accessed by the
function (or block)
– Even if the function (or block) has an identifier
with the same name as the global variable
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 37
Scope of an Identifier (cont’d.)
• To access a global variable declared after the
definition of a function, the function must not
contain any identifier with the same name
– Reserved word extern indicates that a global
variable has been declared elsewhere
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 38
Global Variables, Named Constants,
and Side Effects
• Using global variables causes side effects
• A function that uses global variables is not
independent
• If more than one function uses the same
global variable:
– Can be difficult to debug problems with it
– Problems caused in one area of the program may
appear to be from another area
• Global named constants have no side effects
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 39
Static and Automatic Variables
• Automatic variable: memory is allocated at
block entry and deallocated at block exit
– By default, variables declared within a block are
automatic variables
• Static variable: memory remains allocated as
long as the program executes
– Global variables declared outside of any block are
static variables
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 40
Static and Automatic Variables
(cont’d.)
• Can declare a static variable within a block by
using the reserved word static
• Syntax:
• Static variables declared within a block are
local to the block
– Have same scope as any other local identifier in
that block
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 41
Debugging:
Using Drivers and Stubs
• Driver program: separate program used to
test a function
• When results calculated by one function are
needed in another function, use a function
stub
• Function stub: a function that is not fully
coded
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 42
Function Overloading:
An Introduction
• In a C++ program, several functions can have
the same name
• Function overloading: creating several
functions with the same name
• Function signature: the name and formal
parameter list of the function
– Does not include the return type of the function
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 43
Function Overloading (cont’d.)
• Two functions are said to have different
formal parameter lists if both functions have
either:
– A different number of formal parameters
– If the number of formal parameters is the same,
but the data type of the formal parameters differs
in at least one position
• Overloaded functions must have different
function signatures
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 44
Function Overloading (cont’d.)
• The parameter list supplied in a call to an
overloaded function determines which
function is executed
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 45
Functions with Default Parameters
• In a function call, the number of actual and
formal parameters must be the same
– C++ relaxes this condition for functions with
default parameters
• Can specify the value of a default parameter
in the function prototype
• If you do not specify the value for a default
parameter when calling the function, the
default value is used
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 46
Functions with Default Parameters
(cont’d.)
• All default parameters must be the rightmost
parameters of the function
• If a default parameter value is not specified:
– You must omit all of the arguments to its right
• Default values can be constants, global
variables, or function calls
• Cannot assign a constant value as a default
value to a reference parameter
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 47
Summary
• Functions (modules) divide a program into
manageable tasks
• C++ provides standard, predefined functions
• Two types of user-defined functions: value-
returning functions and void functions
• Variables defined in a function heading are called
formal parameters
• Expressions, variables, or constant values in a
function call are called actual parameters
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 48
Summary (cont’d.)
• Function heading and the body of the function
are called the definition of the function
• A value-returning function returns its value via
the return statement
• A prototype is the function heading without the
body of the function
• User-defined functions execute only when they
are called
• Void functions do not have a data type
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 49
Summary (cont’d.)
• Two types of formal parameters:
– A value parameter receives a copy of its
corresponding actual parameter
– A reference parameter receives the memory
address of its corresponding actual parameter
• Variables declared within a function (or block)
are called local variables
• Variables declared outside of every function
definition (and block) are global variables
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 50
Summary (cont’d.)
• Automatic variable: variable for which
memory is allocated on function/block entry
and deallocated on function/block exit
• Static variable: memory remains allocated
throughout the execution of the program
• C++ functions can have default parameters
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 51

More Related Content

What's hot

Function-Definition, Need, Declaration, Definition, Arguments, Return Value
Function-Definition, Need, Declaration, Definition, Arguments, Return ValueFunction-Definition, Need, Declaration, Definition, Arguments, Return Value
Function-Definition, Need, Declaration, Definition, Arguments, Return Valuemanish maurya
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and typesimtiazalijoono
 
Basic structure of C++ program
Basic structure of C++ programBasic structure of C++ program
Basic structure of C++ programmatiur rahman
 
Overview of c++
Overview of c++Overview of c++
Overview of c++geeeeeet
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Languagesatvirsandhu9
 
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Md. Imran Hossain Showrov
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming LanguageAhmad Idrees
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Functions in C
Functions in CFunctions in C
Functions in CKamal Acharya
 
C++ ppt
C++ pptC++ ppt
C++ pptparpan34
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmigAppili Vamsi Krishna
 
9781285852744 ppt ch01
9781285852744 ppt ch019781285852744 ppt ch01
9781285852744 ppt ch01Terry Yoast
 
Steps for c program execution
Steps for c program executionSteps for c program execution
Steps for c program executionRumman Ansari
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 

What's hot (20)

Function-Definition, Need, Declaration, Definition, Arguments, Return Value
Function-Definition, Need, Declaration, Definition, Arguments, Return ValueFunction-Definition, Need, Declaration, Definition, Arguments, Return Value
Function-Definition, Need, Declaration, Definition, Arguments, Return Value
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
 
Basic structure of C++ program
Basic structure of C++ programBasic structure of C++ program
Basic structure of C++ program
 
Overview of c++
Overview of c++Overview of c++
Overview of c++
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Functions in C
Functions in CFunctions in C
Functions in C
 
C++ ppt
C++ pptC++ ppt
C++ ppt
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
9781285852744 ppt ch01
9781285852744 ppt ch019781285852744 ppt ch01
9781285852744 ppt ch01
 
Steps for c program execution
Steps for c program executionSteps for c program execution
Steps for c program execution
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 

Viewers also liked

9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08Terry Yoast
 
9781285852744 ppt ch09
9781285852744 ppt ch099781285852744 ppt ch09
9781285852744 ppt ch09Terry Yoast
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02Terry Yoast
 
Software development
Software developmentSoftware development
Software developmentTerry Yoast
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingRahul Sharma
 
Thinking in c++ volume1
Thinking in c++ volume1Thinking in c++ volume1
Thinking in c++ volume1mrahil
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10Terry Yoast
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16Terry Yoast
 
9781285852744 ppt ch17
9781285852744 ppt ch179781285852744 ppt ch17
9781285852744 ppt ch17Terry Yoast
 
9781285852744 ppt ch18
9781285852744 ppt ch189781285852744 ppt ch18
9781285852744 ppt ch18Terry Yoast
 
Thinking in C/C++, coding in Java
Thinking in C/C++, coding in JavaThinking in C/C++, coding in Java
Thinking in C/C++, coding in Javaanomalizer
 
9781285852744 ppt ch11
9781285852744 ppt ch119781285852744 ppt ch11
9781285852744 ppt ch11Terry Yoast
 
9781285852744 ppt ch15
9781285852744 ppt ch159781285852744 ppt ch15
9781285852744 ppt ch15Terry Yoast
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13Terry Yoast
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10Terry Yoast
 
9780840024220 ppt ch06
9780840024220 ppt ch069780840024220 ppt ch06
9780840024220 ppt ch06Kristin Harrison
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12Terry Yoast
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14Terry Yoast
 
Object oriented programming (oop) cs304 power point slides lecture 01
Object oriented programming (oop)   cs304 power point slides lecture 01Object oriented programming (oop)   cs304 power point slides lecture 01
Object oriented programming (oop) cs304 power point slides lecture 01Adil Kakakhel
 

Viewers also liked (20)

9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
 
9781285852744 ppt ch09
9781285852744 ppt ch099781285852744 ppt ch09
9781285852744 ppt ch09
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
Software development
Software developmentSoftware development
Software development
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Thinking in c++ volume1
Thinking in c++ volume1Thinking in c++ volume1
Thinking in c++ volume1
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16
 
9781285852744 ppt ch17
9781285852744 ppt ch179781285852744 ppt ch17
9781285852744 ppt ch17
 
9781285852744 ppt ch18
9781285852744 ppt ch189781285852744 ppt ch18
9781285852744 ppt ch18
 
Thinking in C/C++, coding in Java
Thinking in C/C++, coding in JavaThinking in C/C++, coding in Java
Thinking in C/C++, coding in Java
 
9781285852744 ppt ch11
9781285852744 ppt ch119781285852744 ppt ch11
9781285852744 ppt ch11
 
Chap 1 c++
Chap 1 c++Chap 1 c++
Chap 1 c++
 
9781285852744 ppt ch15
9781285852744 ppt ch159781285852744 ppt ch15
9781285852744 ppt ch15
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
9780840024220 ppt ch06
9780840024220 ppt ch069780840024220 ppt ch06
9780840024220 ppt ch06
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14
 
Object oriented programming (oop) cs304 power point slides lecture 01
Object oriented programming (oop)   cs304 power point slides lecture 01Object oriented programming (oop)   cs304 power point slides lecture 01
Object oriented programming (oop) cs304 power point slides lecture 01
 

Similar to Functions ppt ch06

9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.pptLokeshK66
 
Functions
FunctionsFunctions
FunctionsOnline
 
C++.ppt
C++.pptC++.ppt
C++.pptshweta210
 
Computer Programming
Computer ProgrammingComputer Programming
Computer ProgrammingBurhan Fakhar
 
Intro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, RecursionIntro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, RecursionBlue Elephant Consulting
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxGebruGetachew2
 
Functions in c language1
Functions in c language1Functions in c language1
Functions in c language1sirikeshava
 
Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3ahmed22dg
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfmadihamaqbool6
 
1.6 Function.pdf
1.6 Function.pdf1.6 Function.pdf
1.6 Function.pdfNirmalaShinde3
 
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.pptLokeshK66
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptxzueZ3
 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptManivannan837728
 
Programming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptxProgramming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptxNoorAntakia
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringSri Harsha Pamu
 

Similar to Functions ppt ch06 (20)

9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
 
Functions
FunctionsFunctions
Functions
 
Lesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th stepLesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th step
 
C++.ppt
C++.pptC++.ppt
C++.ppt
 
Intro To C++ - Class #19: Functions
Intro To C++ - Class #19: FunctionsIntro To C++ - Class #19: Functions
Intro To C++ - Class #19: Functions
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
Intro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, RecursionIntro To C++ - Class #20: Functions, Recursion
Intro To C++ - Class #20: Functions, Recursion
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
Functions in c language1
Functions in c language1Functions in c language1
Functions in c language1
 
Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3
 
Functions
Functions Functions
Functions
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdf
 
1.6 Function.pdf
1.6 Function.pdf1.6 Function.pdf
1.6 Function.pdf
 
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
 
Programming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptxProgramming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptx
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 

More from Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 

More from Terry Yoast (20)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

Recently uploaded

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 

Recently uploaded (20)

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 

Functions ppt ch06

  • 2. Objectives In this chapter, you will: – Learn about standard (predefined) functions – Learn about user-defined functions – Examine value-returning functions – Explore how to construct and use a value-returning, user-defined function – Learn about function prototypes – Learn how to construct and use void functions C++ Programming: From Problem Analysis to Program Design, Seventh Edition 2
  • 3. Objectives (cont’d.) – 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 – Discover static variables C++ Programming: From Problem Analysis to Program Design, Seventh Edition 3
  • 4. Objectives (cont’d.) – Learn how to debug programs using drivers and stubs – Learn function overloading – Explore functions with default parameters C++ Programming: From Problem Analysis to Program Design, Seventh Edition 4
  • 5. Introduction • Functions are often called modules • They are like miniature programs that can be combined to form larger programs • They allow complicated programs to be divided into manageable pieces C++ Programming: From Problem Analysis to Program Design, Seventh Edition 5
  • 6. Predefined Functions • In C++, a function is similar to that of a function in algebra – It has a name – It does some computation • Some of the predefined mathematical functions are: sqrt(x) pow(x, y) floor(x) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 6
  • 7. Predefined Functions (cont'd.) • Predefined functions are organized into separate libraries – I/O functions are in iostream header – Math functions are in cmath header • To use predefined functions, you must include the header file using an include statement • See Table 6-1 in the text for some common predefined functions C++ Programming: From Problem Analysis to Program Design, Seventh Edition 7
  • 8. User-Defined Functions • Value-returning functions: have a return type – Return a value of a specific data type using the return statement • Void functions: do not have a return type – Do not use a return statement to return a value C++ Programming: From Problem Analysis to Program Design, Seventh Edition 8
  • 9. Value-Returning Functions • To use these functions, you must: – Include the appropriate header file in your program using the include statement – Know the following items: • Name of the function • Number of parameters, if any • Data type of each parameter • Data type of the value returned: called the type of the function C++ Programming: From Problem Analysis to Program Design, Seventh Edition 9
  • 10. Value-Returning Functions (cont’d.) • Can use the value returned by a value- returning function by: – Saving it for further calculation – Using it in some calculation – Printing it • A value-returning function is used in an assignment or in an output statement C++ Programming: From Problem Analysis to Program Design, Seventh Edition 10
  • 11. Value-Returning Functions (cont’d.) • Heading (or function header): first line of the function – Example: int abs(int number) • Formal parameter: variable declared in the heading – Example: number • Actual parameter: variable or expression listed in a call to a function – Example: x = pow(u, v) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 11
  • 12. Syntax: Value-Returning Function • Syntax: • functionType is also called the data type or return type C++ Programming: From Problem Analysis to Program Design, Seventh Edition 12
  • 13. Syntax: Formal Parameter List C++ Programming: From Problem Analysis to Program Design, Seventh Edition 13
  • 14. Function Call C++ Programming: From Problem Analysis to Program Design, Seventh Edition • Syntax to call a value-returning function: 14
  • 15. Syntax: Actual Parameter List • Syntax of the actual parameter list: • Formal parameter list can be empty: • A call to a value-returning function with an empty formal parameter list is: C++ Programming: From Problem Analysis to Program Design, Seventh Edition 15
  • 16. return Statement • Function returns its value via the return statement – It passes this value outside the function C++ Programming: From Problem Analysis to Program Design, Seventh Edition 16
  • 17. Syntax: return Statement • Syntax: • In C++, return is a reserved word • When a return statement executes – Function immediately terminates – Control goes back to the caller • When a return statement executes in the function main, the program terminates C++ Programming: From Problem Analysis to Program Design, Seventh Edition 17
  • 18. Syntax: return Statement (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 18
  • 19. Function Prototype • Function prototype: function heading without the body of the function • Syntax: • Not necessary to specify the variable name in the parameter list • Data type of each parameter must be specified C++ Programming: From Problem Analysis to Program Design, Seventh Edition 19
  • 20. Value-Returning Functions: Some Peculiarities C++ Programming: From Problem Analysis to Program Design, Seventh Edition 20
  • 21. Value-Returning Functions: Some Peculiarities (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 21
  • 22. Flow of Compilation and Execution • Execution always begins at the first statement in the function main • Other functions are executed only when called • Function prototypes appear before any function definition – Compiler translates these first • Compiler can then correctly translate a function call C++ Programming: From Problem Analysis to Program Design, Seventh Edition 22
  • 23. Flow of Compilation and Execution (cont’d.) • Function call transfers control to the first statement in the body of the called function • When the end of a called function is executed, control is passed back to the point immediately following the function call – Function’s returned value replaces the function call statement C++ Programming: From Problem Analysis to Program Design, Seventh Edition 23
  • 24. Void Functions • User-defined void functions can be placed either before or after the function main • If user-defined void functions are placed after the function main – The function prototype must be placed before the function main • Void function does not have a return type – return statement without any value is typically used to exit the function early C++ Programming: From Problem Analysis to Program Design, Seventh Edition 24
  • 25. Void Functions (cont’d.) • Formal parameters are optional • A call to a void function is a stand-alone statement • Void function definition syntax: C++ Programming: From Problem Analysis to Program Design, Seventh Edition 25
  • 26. Void Functions (cont’d.) • Formal parameter list syntax: • Function call syntax: • Actual parameter list syntax: C++ Programming: From Problem Analysis to Program Design, Seventh Edition 26
  • 27. Void Functions (cont’d.) • Value parameter: a formal parameter that receives a copy of the content of corresponding actual parameter • Reference parameter: a formal parameter that receives the location (memory address) of the corresponding actual parameter C++ Programming: From Problem Analysis to Program Design, Seventh Edition 27
  • 28. Value Parameters • If a formal parameter is a value parameter: – The value of the corresponding actual parameter is copied into it – Formal parameter has its own copy of the data • During program execution – Formal parameter manipulates the data stored in its own memory space C++ Programming: From Problem Analysis to Program Design, Seventh Edition 28
  • 29. Reference Variables as Parameters • If a formal parameter is a reference parameter – It receives the memory address of the corresponding actual parameter • During program execution to manipulate data – Changes to formal parameter will change the corresponding actual parameter C++ Programming: From Problem Analysis to Program Design, Seventh Edition 29
  • 30. Reference Variables as Parameters (cont’d.) • Reference parameters are useful in three situations: – Returning more than one value – Changing the actual parameter – When passing the address would save memory space and time C++ Programming: From Problem Analysis to Program Design, Seventh Edition 30
  • 31. Value and Reference Parameters and Memory Allocation • When a function is called – Memory for its formal parameters and its local variables is allocated in the function data area • For a value parameter, the actual parameter’s value is copied into the formal parameter’s memory cell – Changes to the formal parameter do not affect the actual parameter’s value C++ Programming: From Problem Analysis to Program Design, Seventh Edition 31
  • 32. Value and Reference Parameters and Memory Allocation (cont’d.) • For a reference parameter, the actual parameter’s address passes to the formal parameter • Both formal and actual parameters refer to the same memory location • During execution, changes made to the formal parameter’s value permanently change the actual parameter’s value C++ Programming: From Problem Analysis to Program Design, Seventh Edition 32
  • 33. Reference Parameters and Value- Returning Functions • Can also use reference parameters in a value- returning function – Not recommended • By definition, a value-returning function returns a single value via return statement • If a function needs to return more than one value, change it to a void function and use reference parameters to return the values C++ Programming: From Problem Analysis to Program Design, Seventh Edition 33
  • 34. Scope of an Identifier • Scope of an identifier: where in the program the identifier is accessible • Local identifier: identifiers declared within a function (or block) • Global identifier: identifiers declared outside of every function definition • C++ does not allow nested functions – Definition of one function cannot be included in the body of another function C++ Programming: From Problem Analysis to Program Design, Seventh Edition 34
  • 35. Scope of an Identifier (cont’d.) • Rules when an identifier is accessed: – Global identifiers are accessible by a function or block if: • Declared before function definition • Function name different from identifier • Parameters to the function have different names • All local identifiers have different names C++ Programming: From Problem Analysis to Program Design, Seventh Edition 35
  • 36. Scope of an Identifier (cont’d.) • Rules when an identifier is accessed (cont’d.): – Nested block • Identifier accessible from declaration to end of block in which it is declared • Within nested blocks if no identifier with same name exists – Scope of function name similar to scope of identifier declared outside any block • i.e., function name scope = global variable scope C++ Programming: From Problem Analysis to Program Design, Seventh Edition 36
  • 37. Scope of an Identifier (cont’d.) • Some compilers initialize global variables to default values • Scope resolution operator in C++ is :: • By using the scope resolution operator – A global variable declared before the definition of a function (or block) can be accessed by the function (or block) – Even if the function (or block) has an identifier with the same name as the global variable C++ Programming: From Problem Analysis to Program Design, Seventh Edition 37
  • 38. Scope of an Identifier (cont’d.) • To access a global variable declared after the definition of a function, the function must not contain any identifier with the same name – Reserved word extern indicates that a global variable has been declared elsewhere C++ Programming: From Problem Analysis to Program Design, Seventh Edition 38
  • 39. Global Variables, Named Constants, and Side Effects • Using global variables causes side effects • A function that uses global variables is not independent • If more than one function uses the same global variable: – Can be difficult to debug problems with it – Problems caused in one area of the program may appear to be from another area • Global named constants have no side effects C++ Programming: From Problem Analysis to Program Design, Seventh Edition 39
  • 40. Static and Automatic Variables • Automatic variable: memory is allocated at block entry and deallocated at block exit – By default, variables declared within a block are automatic variables • Static variable: memory remains allocated as long as the program executes – Global variables declared outside of any block are static variables C++ Programming: From Problem Analysis to Program Design, Seventh Edition 40
  • 41. Static and Automatic Variables (cont’d.) • Can declare a static variable within a block by using the reserved word static • Syntax: • Static variables declared within a block are local to the block – Have same scope as any other local identifier in that block C++ Programming: From Problem Analysis to Program Design, Seventh Edition 41
  • 42. Debugging: Using Drivers and Stubs • Driver program: separate program used to test a function • When results calculated by one function are needed in another function, use a function stub • Function stub: a function that is not fully coded C++ Programming: From Problem Analysis to Program Design, Seventh Edition 42
  • 43. Function Overloading: An Introduction • In a C++ program, several functions can have the same name • Function overloading: creating several functions with the same name • Function signature: the name and formal parameter list of the function – Does not include the return type of the function C++ Programming: From Problem Analysis to Program Design, Seventh Edition 43
  • 44. Function Overloading (cont’d.) • Two functions are said to have different formal parameter lists if both functions have either: – A different number of formal parameters – If the number of formal parameters is the same, but the data type of the formal parameters differs in at least one position • Overloaded functions must have different function signatures C++ Programming: From Problem Analysis to Program Design, Seventh Edition 44
  • 45. Function Overloading (cont’d.) • The parameter list supplied in a call to an overloaded function determines which function is executed C++ Programming: From Problem Analysis to Program Design, Seventh Edition 45
  • 46. Functions with Default Parameters • In a function call, the number of actual and formal parameters must be the same – C++ relaxes this condition for functions with default parameters • Can specify the value of a default parameter in the function prototype • If you do not specify the value for a default parameter when calling the function, the default value is used C++ Programming: From Problem Analysis to Program Design, Seventh Edition 46
  • 47. Functions with Default Parameters (cont’d.) • All default parameters must be the rightmost parameters of the function • If a default parameter value is not specified: – You must omit all of the arguments to its right • Default values can be constants, global variables, or function calls • Cannot assign a constant value as a default value to a reference parameter C++ Programming: From Problem Analysis to Program Design, Seventh Edition 47
  • 48. Summary • Functions (modules) divide a program into manageable tasks • C++ provides standard, predefined functions • Two types of user-defined functions: value- returning functions and void functions • Variables defined in a function heading are called formal parameters • Expressions, variables, or constant values in a function call are called actual parameters C++ Programming: From Problem Analysis to Program Design, Seventh Edition 48
  • 49. Summary (cont’d.) • Function heading and the body of the function are called the definition of the function • A value-returning function returns its value via the return statement • A prototype is the function heading without the body of the function • User-defined functions execute only when they are called • Void functions do not have a data type C++ Programming: From Problem Analysis to Program Design, Seventh Edition 49
  • 50. Summary (cont’d.) • Two types of formal parameters: – A value parameter receives a copy of its corresponding actual parameter – A reference parameter receives the memory address of its corresponding actual parameter • Variables declared within a function (or block) are called local variables • Variables declared outside of every function definition (and block) are global variables C++ Programming: From Problem Analysis to Program Design, Seventh Edition 50
  • 51. Summary (cont’d.) • Automatic variable: variable for which memory is allocated on function/block entry and deallocated on function/block exit • Static variable: memory remains allocated throughout the execution of the program • C++ functions can have default parameters C++ Programming: From Problem Analysis to Program Design, Seventh Edition 51