SlideShare a Scribd company logo
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 (20)

ODP
C++ Function
PingLun Liao
 
PPTX
Function C++
Shahzad Afridi
 
PPTX
Recursive Function
Harsh Pathak
 
PPT
User Defined Functions
Praveen M Jigajinni
 
PDF
Lecture20 user definedfunctions.ppt
eShikshak
 
PPTX
Inline function
Tech_MX
 
PPTX
Functions in C - Programming
GaurangVishnoi
 
PPTX
INLINE FUNCTION IN C++
Vraj Patel
 
PPTX
Functions in C++
home
 
PPT
Functions in C++
Nikhil Pandit
 
PPTX
Functions in c
reshmy12
 
PPTX
Functions in c
sunila tharagaturi
 
PPTX
functions of C++
tarandeep_kaur
 
PPTX
User defined functions in C
Harendra Singh
 
DOCX
C programming language working with functions 1
Jeevan Raj
 
PPTX
Header files in c
HoneyChintal
 
PPT
Recursion in c
Saket Pathak
 
PPT
Prsentation on functions
Alisha Korpal
 
PDF
Modular Programming in C
bhawna kol
 
DOC
4. function
Shankar Gangaju
 
C++ Function
PingLun Liao
 
Function C++
Shahzad Afridi
 
Recursive Function
Harsh Pathak
 
User Defined Functions
Praveen M Jigajinni
 
Lecture20 user definedfunctions.ppt
eShikshak
 
Inline function
Tech_MX
 
Functions in C - Programming
GaurangVishnoi
 
INLINE FUNCTION IN C++
Vraj Patel
 
Functions in C++
home
 
Functions in C++
Nikhil Pandit
 
Functions in c
reshmy12
 
Functions in c
sunila tharagaturi
 
functions of C++
tarandeep_kaur
 
User defined functions in C
Harendra Singh
 
C programming language working with functions 1
Jeevan Raj
 
Header files in c
HoneyChintal
 
Recursion in c
Saket Pathak
 
Prsentation on functions
Alisha Korpal
 
Modular Programming in C
bhawna kol
 
4. function
Shankar Gangaju
 

Similar to Basic information of function in cpu (20)

PPT
Chapter 1.ppt
ethiouniverse
 
PPT
Chapter Introduction to Modular Programming.ppt
AmanuelZewdie4
 
PPT
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
harinipradeep15
 
PDF
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
PDF
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
jacobdiriba
 
PPTX
Intro To C++ - Class #19: Functions
Blue Elephant Consulting
 
PPT
Savitch ch 04
Terry Yoast
 
PPTX
Programming Fundamentals lecture-10.pptx
singyali199
 
PPTX
9781337102087 ppt ch06
Terry Yoast
 
PPT
Functions
Online
 
PPT
POLITEKNIK MALAYSIA
Aiman Hud
 
PPT
Chap 5 c++
Venkateswarlu Vuggam
 
DOCX
Functions assignment
Ahmad Kamal
 
PPTX
Amit user defined functions xi (2)
Arpit Meena
 
PPTX
Chapter 4
temkin abdlkader
 
DOCX
Introduction to c programming
AMAN ANAND
 
PPT
Savitch Ch 04
Terry Yoast
 
PPT
Savitch Ch 04
Terry Yoast
 
Chapter 1.ppt
ethiouniverse
 
Chapter Introduction to Modular Programming.ppt
AmanuelZewdie4
 
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
harinipradeep15
 
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
jacobdiriba
 
Intro To C++ - Class #19: Functions
Blue Elephant Consulting
 
Savitch ch 04
Terry Yoast
 
Programming Fundamentals lecture-10.pptx
singyali199
 
9781337102087 ppt ch06
Terry Yoast
 
Functions
Online
 
POLITEKNIK MALAYSIA
Aiman Hud
 
Functions assignment
Ahmad Kamal
 
Amit user defined functions xi (2)
Arpit Meena
 
Chapter 4
temkin abdlkader
 
Introduction to c programming
AMAN ANAND
 
Savitch Ch 04
Terry Yoast
 
Savitch Ch 04
Terry Yoast
 
Ad

More from Dhaval Jalalpara (20)

PPTX
Online freely available remote sensed data
Dhaval Jalalpara
 
PPT
Global positioning system
Dhaval Jalalpara
 
PPTX
Geographic information system
Dhaval Jalalpara
 
PPTX
Compaction in different type of structure
Dhaval Jalalpara
 
PPT
The wave equation
Dhaval Jalalpara
 
PPTX
Soil consistency
Dhaval Jalalpara
 
PPTX
Repairing of masonry structures
Dhaval Jalalpara
 
PPTX
Hydrographic survey
Dhaval Jalalpara
 
PPTX
Compressible Fluid
Dhaval Jalalpara
 
PPTX
Acoustical technology
Dhaval Jalalpara
 
PPTX
Acoustical and noise insulation
Dhaval Jalalpara
 
PPTX
Contributor personality and development
Dhaval Jalalpara
 
PPTX
Food and land resources
Dhaval Jalalpara
 
PPT
Remote sensing
Dhaval Jalalpara
 
PPTX
Remote sensing 1
Dhaval Jalalpara
 
PPT
Digital image processing
Dhaval Jalalpara
 
PPT
Digital image processing 1
Dhaval Jalalpara
 
PPT
03 listening skills
Dhaval Jalalpara
 
PPT
Function in cpu 2
Dhaval Jalalpara
 
PPT
Function in cpu 1
Dhaval Jalalpara
 
Online freely available remote sensed data
Dhaval Jalalpara
 
Global positioning system
Dhaval Jalalpara
 
Geographic information system
Dhaval Jalalpara
 
Compaction in different type of structure
Dhaval Jalalpara
 
The wave equation
Dhaval Jalalpara
 
Soil consistency
Dhaval Jalalpara
 
Repairing of masonry structures
Dhaval Jalalpara
 
Hydrographic survey
Dhaval Jalalpara
 
Compressible Fluid
Dhaval Jalalpara
 
Acoustical technology
Dhaval Jalalpara
 
Acoustical and noise insulation
Dhaval Jalalpara
 
Contributor personality and development
Dhaval Jalalpara
 
Food and land resources
Dhaval Jalalpara
 
Remote sensing
Dhaval Jalalpara
 
Remote sensing 1
Dhaval Jalalpara
 
Digital image processing
Dhaval Jalalpara
 
Digital image processing 1
Dhaval Jalalpara
 
03 listening skills
Dhaval Jalalpara
 
Function in cpu 2
Dhaval Jalalpara
 
Function in cpu 1
Dhaval Jalalpara
 
Ad

Recently uploaded (20)

PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Hashing Introduction , hash functions and techniques
sailajam21
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Design Thinking basics for Engineers.pdf
CMR University
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Zilliz Cloud Demo for performance and scale
Zilliz
 

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