SlideShare a Scribd company logo
1 of 15
1
User Defined Functions
2
Chapter Topics
 Standard (Predefined) Functions
 User-Defined Functions
 Value-Returning Functions
 The return Statement
 Function Prototype
 Flow of Execution
3
Top-Down Structured Design with Functions
 Recall two types of functions
 Value returning function
 computes a single value
 returns value to calling code
 uses return command
 Void function (procedure)
 called as a statement
 executes some task
 This chapter focuses on the value returning
4
Advantages of Using Functions
1. To help make the program more
understandable
2. To modularize the tasks of the program
 building blocks of the program
1. Write a module once
 those lines of source code are called multiple
times in the program
5
Advantages of Using Functions
1. While working on one function, you can focus on
just that part of the program
 construct it,
 debug it,
 perfect it.
5. Different people can work on different functions
simultaneously.
6. If a function is needed in more than one place in a
program, or in different programs, you can write it
once and use it many times
6
Standard (Predefined) Functions
 Predefined functions
 Part of the C++ language
 Provided in function libraries
 Examples:
abs(x), sin(x), log(x), pow( x, n)abs(x), sin(x), log(x), pow( x, n)
 These functions will return a value
 To be printed cout << sin (x);cout << sin (x);
 To be assigned y = pow (3, 4.5);y = pow (3, 4.5);
 To be used in an expression 3.14 * sqr(r)3.14 * sqr(r)
Make sure to use
the required
#include#include file
7
Predefined Functions
View
sample
progra
m
8
Value-Returning Functions
For the compiler to use a function you have
written, it must know when it finds that
function call in your source code …
1. The name of the function
2. The number of parameters, if any
3. The data type of each parameter
4. Data type of the value returned by the
function
5. The code required to do the calculation
Information provided
by the heading of the
function
Information provided in
the body of the function
All the properties
together make up
the definition of the
function
9
Value-Returning Functions
 Consider a function for the area of a circle:
double circleArea (double radius)double circleArea (double radius)
{{
return 3.14159 * radius *return 3.14159 * radius *
radius;radius;
}}
 Note the
 Heading (type, name, parameters)
 The body
 The return statement
10
Parameters
 Function definition syntax:
functionType functionName (formal parameter list)functionType functionName (formal parameter list)
{{
statementsstatements
}}
 Call (invocation of the function)
cout << "Enter radius for circle area -> ";cout << "Enter radius for circle area -> ";
cin >> radius;cin >> radius;
area = circleArea (radius);area = circleArea (radius);
Parameters in the
declaration : formal
parameters
Parameters in the
call: actual
parameters
11
The return Statement
 A value returning statement must have a
return statement
 Else a warning from the compiler
 Also the function will actually return a "garbage"
value
 Syntax:
return expression;return expression;
 View example
12
Function Prototype
 Recall that the compiler must know certain
things about your function
 When it finds a function call in your source code
 Must know information in heading
 Your program must have at least the
heading of a function before it is invoked

Usually listed before function main ( )main ( )
 View example
13
Alternative to Prototype
 Also possible to place whole function
definition before main ()main ()
 This is a requirement in some languages
(Pascal)
 Either is acceptable in this class
 There may be a standard required of
programmers within a particular
organization
 View Example
14
Flow of Control
 First statement executed in any program is
the first statement in the function main( )
 When another function called
 logical control passed to first statement in that
function’s body
 program proceeds through sequence of
statements within the function
 When last statement of function executed
 control returns to where function was called
 control given to next command after the call
15
Flow of Control
void main ( )
{ . . .
print_summary (rpt_total);
revenue = rpt_total * .72675;
. . .
}
void print_summary (int total)
{ . . .
cout << . . .
}
void main ( )
{ . . .
print_summary (rpt_total);
revenue = rpt_total * .72675;
. . .
}
void print_summary (int total)
{ . . .
cout << . . .
}
- first statement of main
- function call, jumps to first statement
of that function
- proceeds through function
- returns to next statement after call
- first statement of main
- function call, jumps to first statement
of that function
- proceeds through function
- returns to next statement after call

More Related Content

What's hot

Recursive Function
Recursive FunctionRecursive Function
Recursive FunctionHarsh Pathak
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppteShikshak
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Functions in C - Programming
Functions in C - Programming Functions in C - Programming
Functions in C - Programming GaurangVishnoi
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++Vraj Patel
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Functions in c
Functions in cFunctions in c
Functions in creshmy12
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1Jeevan Raj
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functionsAlisha Korpal
 
Modular Programming in C
Modular Programming in CModular Programming in C
Modular Programming in Cbhawna kol
 

What's hot (20)

C++ Function
C++ FunctionC++ Function
C++ Function
 
Function C++
Function C++ Function C++
Function C++
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppt
 
Inline function
Inline functionInline function
Inline function
 
Functions in C - Programming
Functions in C - Programming Functions in C - Programming
Functions in C - Programming
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functions in c
Functions in cFunctions in c
Functions in c
 
functions of C++
functions of C++functions of C++
functions of C++
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
 
Header files in c
Header files in cHeader files in c
Header files in c
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
Modular Programming in C
Modular Programming in CModular Programming in C
Modular Programming in C
 
4. function
4. function4. function
4. function
 

Similar to Basic information of function in cpu

Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
 
c.p function
c.p functionc.p function
c.p functiongiri5624
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfTeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdfTeshaleSiyum
 
Python Programming - Functions and Modules
Python Programming - Functions and ModulesPython Programming - Functions and Modules
Python Programming - Functions and ModulesOmid AmirGhiasvand
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxvekariyakashyap
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1YOGESH SINGH
 

Similar to Basic information of function in cpu (20)

Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
c.p function
c.p functionc.p function
c.p function
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Function
FunctionFunction
Function
 
Savitch ch 04
Savitch ch 04Savitch ch 04
Savitch ch 04
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Python Programming - Functions and Modules
Python Programming - Functions and ModulesPython Programming - Functions and Modules
Python Programming - Functions and Modules
 
Functions
FunctionsFunctions
Functions
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 

More from Dhaval Jalalpara

More from Dhaval Jalalpara (20)

Online freely available remote sensed data
Online freely available remote sensed dataOnline freely available remote sensed data
Online freely available remote sensed data
 
Global positioning system
Global positioning systemGlobal positioning system
Global positioning system
 
Geographic information system
Geographic information systemGeographic information system
Geographic information system
 
Compaction in different type of structure
Compaction  in different type of structure Compaction  in different type of structure
Compaction in different type of structure
 
The wave equation
The wave equationThe wave equation
The wave equation
 
Soil consistency
Soil  consistencySoil  consistency
Soil consistency
 
Repairing of masonry structures
Repairing of masonry structuresRepairing of masonry structures
Repairing of masonry structures
 
Hydrographic survey
Hydrographic surveyHydrographic survey
Hydrographic survey
 
Compressible Fluid
Compressible FluidCompressible Fluid
Compressible Fluid
 
Acoustical technology
Acoustical technologyAcoustical technology
Acoustical technology
 
Acoustical and noise insulation
Acoustical and noise insulationAcoustical and noise insulation
Acoustical and noise insulation
 
Contributor personality and development
Contributor personality and developmentContributor personality and development
Contributor personality and development
 
Food and land resources
Food and land resourcesFood and land resources
Food and land resources
 
Remote sensing
Remote sensingRemote sensing
Remote sensing
 
Remote sensing 1
Remote sensing 1Remote sensing 1
Remote sensing 1
 
Digital image processing
Digital image processingDigital image processing
Digital image processing
 
Digital image processing 1
Digital  image processing 1Digital  image processing 1
Digital image processing 1
 
03 listening skills
03 listening skills03 listening skills
03 listening skills
 
Function in cpu 2
Function in cpu 2Function in cpu 2
Function in cpu 2
 
Function in cpu 1
Function in cpu 1Function in cpu 1
Function in cpu 1
 

Recently uploaded

Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Christo Ananth
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 

Recently uploaded (20)

(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 

Basic information of function in cpu

  • 2. 2 Chapter Topics  Standard (Predefined) Functions  User-Defined Functions  Value-Returning Functions  The return Statement  Function Prototype  Flow of Execution
  • 3. 3 Top-Down Structured Design with Functions  Recall two types of functions  Value returning function  computes a single value  returns value to calling code  uses return command  Void function (procedure)  called as a statement  executes some task  This chapter focuses on the value returning
  • 4. 4 Advantages of Using Functions 1. To help make the program more understandable 2. To modularize the tasks of the program  building blocks of the program 1. Write a module once  those lines of source code are called multiple times in the program
  • 5. 5 Advantages of Using Functions 1. While working on one function, you can focus on just that part of the program  construct it,  debug it,  perfect it. 5. Different people can work on different functions simultaneously. 6. If a function is needed in more than one place in a program, or in different programs, you can write it once and use it many times
  • 6. 6 Standard (Predefined) Functions  Predefined functions  Part of the C++ language  Provided in function libraries  Examples: abs(x), sin(x), log(x), pow( x, n)abs(x), sin(x), log(x), pow( x, n)  These functions will return a value  To be printed cout << sin (x);cout << sin (x);  To be assigned y = pow (3, 4.5);y = pow (3, 4.5);  To be used in an expression 3.14 * sqr(r)3.14 * sqr(r) Make sure to use the required #include#include file
  • 8. 8 Value-Returning Functions For the compiler to use a function you have written, it must know when it finds that function call in your source code … 1. The name of the function 2. The number of parameters, if any 3. The data type of each parameter 4. Data type of the value returned by the function 5. The code required to do the calculation Information provided by the heading of the function Information provided in the body of the function All the properties together make up the definition of the function
  • 9. 9 Value-Returning Functions  Consider a function for the area of a circle: double circleArea (double radius)double circleArea (double radius) {{ return 3.14159 * radius *return 3.14159 * radius * radius;radius; }}  Note the  Heading (type, name, parameters)  The body  The return statement
  • 10. 10 Parameters  Function definition syntax: functionType functionName (formal parameter list)functionType functionName (formal parameter list) {{ statementsstatements }}  Call (invocation of the function) cout << "Enter radius for circle area -> ";cout << "Enter radius for circle area -> "; cin >> radius;cin >> radius; area = circleArea (radius);area = circleArea (radius); Parameters in the declaration : formal parameters Parameters in the call: actual parameters
  • 11. 11 The return Statement  A value returning statement must have a return statement  Else a warning from the compiler  Also the function will actually return a "garbage" value  Syntax: return expression;return expression;  View example
  • 12. 12 Function Prototype  Recall that the compiler must know certain things about your function  When it finds a function call in your source code  Must know information in heading  Your program must have at least the heading of a function before it is invoked  Usually listed before function main ( )main ( )  View example
  • 13. 13 Alternative to Prototype  Also possible to place whole function definition before main ()main ()  This is a requirement in some languages (Pascal)  Either is acceptable in this class  There may be a standard required of programmers within a particular organization  View Example
  • 14. 14 Flow of Control  First statement executed in any program is the first statement in the function main( )  When another function called  logical control passed to first statement in that function’s body  program proceeds through sequence of statements within the function  When last statement of function executed  control returns to where function was called  control given to next command after the call
  • 15. 15 Flow of Control void main ( ) { . . . print_summary (rpt_total); revenue = rpt_total * .72675; . . . } void print_summary (int total) { . . . cout << . . . } void main ( ) { . . . print_summary (rpt_total); revenue = rpt_total * .72675; . . . } void print_summary (int total) { . . . cout << . . . } - first statement of main - function call, jumps to first statement of that function - proceeds through function - returns to next statement after call - first statement of main - function call, jumps to first statement of that function - proceeds through function - returns to next statement after call