SlideShare a Scribd company logo
Lecture 06
Function
Introduction
Functions allow a sequence of statement to be referred to by a
name and parameterized.
•Calling a function causes statement sequence to be executed and
a value may be returned when the function terminates.
•Parameterization allows the same function to be applied to many
different values without changing the statements.
•sqrt is function that computes square root of number, sqrt(4)
computes square root of 4, sqrt(9) computer square root of 9!
Same sequence of statements inside function, with different
parameter returns different value.
•
Declaration
Introduces the function name, function return type,
and function parameters to the program.
•The function body (statements)is not part of the
declaration
•A function must be declared before it is used
Format: return_type function_name(parameter_list);
parameter_list: type param1,type param2,type
param3, etc
•

Some function declarations:

•

double sqrt(double);
int func1();
void func2(char,int);
Function Definition
Introduces the function name, function return type, and function
parameters as well as the function body to the program Function
body (the implementation)is a compound statement
return_type function_name(parameter_list)
{
// Function body
}
• Function Definition Example:
•

void print_func()
{
cout << “hello world” << endl;
}

A function can be defined in any part of the program text or
within a library
•
Functions And Program
Structure
Functions must be declared before they are used, e.g:
l// preprocessor statements
lDefine function 1
lDefine function 2
lDefine function 3
l{ call function 2}
lmain()
l{
lCall function 1
lCall function 3
l}

•
Void Functions
These are functions that do not return a value
They can still cause a side effect by modifying a global variable
Example:
l int num = 1;
lvoid print_func()
l{
lcout << “hello world” << num << endl;
lnum = num + 1;
l}
•Note: It is not possible to declare variables of type void because
type void has no values, i.e. it has no structure
•
Parameterized Functions
A function declaration must include the list of parameter types
(with an optional name):
int func(int x)
•Each parameter type specifies the type of the value that should
appear as a function argument when the function is called.
•Therefore the function func takes one parameter which is an
integer func can be called using: int x =func(42);
•The function name is followed by a bracketed list of function
arguments
•Note: A function definition also requires a parameter list with in
the function body the parameters are accessible as variables
which are named in the parameter list with their types:
•
Int func(int x,int y)
{
// place function body here
// variables x and y can be used here
}

x and y are the parameter variables that can be used in the
function body
• Note that the types of the parameters must be the same in the
function declaration and definition
•Formal parameters: the parameters used in the function
definition, e.g. x and y in
int func(int x, int y) …
•
Parameterized Functions
Actual parameters or arguments: the parameters used in the
function call, e.g. 10 and 20 in
int x = func(10, 20);
•Note that the actual parameters are evaluated before being
passed to
the function, e.g.
•

int a = 10, b =20;
int x =func(a/2, a+b);
•

The actual parameters passed to func are 5 and 30
The order of evaluation of arguments is undefined.
Functions and Scope
Functions may only be referred to within the scope of the
declaration.
•Declarations made in a function body or compound statement
are local to that scope.
•Function parameters are variables in the scope of the function
body.
•A new variable is created for each parameter when ever the
function is called.
•Each parameter variable is destroyed when the function
terminates.
•Hence parameters are local to the function they are part of.
•
Default Parameters
Function parameters may be given default values

•

int func(int x = 10);

If func is called with an empty parameter list

•

func();

then the parameter variable x will be initialized to 10
•If func is called with a parameter
func(15);
then the default is NOT used and the parameter variable x is
initialized to the actual parameter value,I.e.15
Default Parameters
Note that default parameters must appear at the END of the
parameter list
•

int func1(int x = 1);
int func2(int x, int y = 1);
int func3(int x = 1, int y);
•

// this is ok
// this is ok
// this is not ok

Also several default parameters can be given

int func4(char c, int x = 1, int y = 1); this is ok
Parameter Passing In
Functions

Arguments/Parameters are a way to share data between two
functions.
•In all the programs we have seen so far, the arguments in a
function call provide data needed by the called function
•

they pass data in to the function
There are times when a function wants to use its parameters to
pass data back to the calling program.
•

examples are when functions must return more than one value or must
modify the arguments passed to them.

We need two types of parameter passing to handle these two
situations.
•
Call By Value
Call by value means the value of i is passed as a parameter to
func
The local variable I of func is initialized to that value
The local variable I of main that appears in the call is left
untouched -only a copy of its value is passed to the function
func.
•
Call By Reference
References allow a second parameter passing mechanism to be
used - call by reference
•If a formal parameter type is a reference type
•

int func(int &x)

Then the parameter variable x will be a reference to the actual
parameter.
•

int y = 1;
func(y);
x will be initialized to a reference to y when the function func is called
So x and y will both denote the SAME object

Contrast this to call by value where x will be initialized to a copy
of the value of y.
•
Inline Function
Inline functions are functions where the call is made to
inline functions. The actual code then gets placed in the
calling program.
Once you define an inline function, using the 'inline'
keyword, whenever you call that function the compiler
will replace the function call with the actual code from
the function.
General form
inline datatype function_name(arguments)
Inline Function
#include <iostream>
using namespace std;
int func(int);
void main( )
{
int x;
cout << "n Enter the Input Value: ";
cin>>x;
cout << "n The Output is: " << func(x);
}
inline int func(int x1)
{
return 5*x1;
}
Note:-Inline functions will save time and are useful if the function is
very small. If the function is large, use of inline functions must be
avoided.

More Related Content

PPTX
functions of C++
PPTX
Function C++
PPT
16717 functions in C++
 
PPTX
Parameter passing to_functions_in_c
PPT
Functions in C++
PPT
Functions
PPTX
Call by value
PPTX
C and C++ functions
functions of C++
Function C++
16717 functions in C++
 
Parameter passing to_functions_in_c
Functions in C++
Functions
Call by value
C and C++ functions

What's hot (19)

PPT
User Defined Functions
ODP
Function
PPT
Functions in C++
PPT
FUNCTIONS IN c++ PPT
PPT
C++ Function
PPT
Prsentation on functions
PPT
Function in C Language
PPTX
predefined and user defined functions
PPTX
Inline function
PPT
RECURSION IN C
PPT
Pre defined Functions in C
PPTX
Functions in C++
PPTX
Built in function
PPT
user defined function
PPTX
Function in C program
PPT
Lecture#7 Call by value and reference in c++
PPTX
Function in c
PPT
Functions in c++
PPTX
User defined functions in C
User Defined Functions
Function
Functions in C++
FUNCTIONS IN c++ PPT
C++ Function
Prsentation on functions
Function in C Language
predefined and user defined functions
Inline function
RECURSION IN C
Pre defined Functions in C
Functions in C++
Built in function
user defined function
Function in C program
Lecture#7 Call by value and reference in c++
Function in c
Functions in c++
User defined functions in C
Ad

Viewers also liked (11)

PPTX
C++ programming function
PPT
Functions in C++
PPTX
Functions in C++
PPT
Operator Overloading
PPTX
OPERATOR OVERLOADING IN C++
PPT
08 c++ Operator Overloading.ppt
PPTX
operator overloading in c++
PPT
Function overloading(c++)
PPTX
Operator overloading
PPTX
Presentation on overloading
PPTX
Function overloading
C++ programming function
Functions in C++
Functions in C++
Operator Overloading
OPERATOR OVERLOADING IN C++
08 c++ Operator Overloading.ppt
operator overloading in c++
Function overloading(c++)
Operator overloading
Presentation on overloading
Function overloading
Ad

Similar to Function in c++ (20)

DOCX
Introduction to c programming
PDF
Functions in C++.pdf
DOCX
Functions assignment
PPT
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
PPT
Chapter Introduction to Modular Programming.ppt
PPT
Savitch ch 05
PPT
Savitch Ch 05
PPT
Savitch Ch 05
PDF
Chapter 1. Functions in C++.pdf
PDF
Chapter_1.__Functions_in_C++[1].pdf
PDF
PSPC-UNIT-4.pdf
PPTX
3 Function & Storage Class.pptx
PPT
arrays.ppt
PDF
VIT351 Software Development VI Unit1
PPTX
Programming Fundamentals lecture-10.pptx
PPTX
PPTX
Amit user defined functions xi (2)
PDF
PPTX
FUNCTION CPU
PDF
4th unit full
Introduction to c programming
Functions in C++.pdf
Functions assignment
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
Chapter Introduction to Modular Programming.ppt
Savitch ch 05
Savitch Ch 05
Savitch Ch 05
Chapter 1. Functions in C++.pdf
Chapter_1.__Functions_in_C++[1].pdf
PSPC-UNIT-4.pdf
3 Function & Storage Class.pptx
arrays.ppt
VIT351 Software Development VI Unit1
Programming Fundamentals lecture-10.pptx
Amit user defined functions xi (2)
FUNCTION CPU
4th unit full

More from Kumar (20)

PPT
Graphics devices
PPT
Fill area algorithms
PDF
region-filling
PDF
Bresenham derivation
PPT
Bresenham circles and polygons derication
PPTX
Introductionto xslt
PPTX
Extracting data from xml
PPTX
Xml basics
PPTX
XML Schema
PPTX
Publishing xml
PPTX
DTD
PPTX
Applying xml
PPTX
Introduction to XML
PDF
How to deploy a j2ee application
PDF
JNDI, JMS, JPA, XML
PDF
EJB Fundmentals
PDF
JSP and struts programming
PDF
java servlet and servlet programming
PDF
Introduction to JDBC and JDBC Drivers
PDF
Introduction to J2EE
Graphics devices
Fill area algorithms
region-filling
Bresenham derivation
Bresenham circles and polygons derication
Introductionto xslt
Extracting data from xml
Xml basics
XML Schema
Publishing xml
DTD
Applying xml
Introduction to XML
How to deploy a j2ee application
JNDI, JMS, JPA, XML
EJB Fundmentals
JSP and struts programming
java servlet and servlet programming
Introduction to JDBC and JDBC Drivers
Introduction to J2EE

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Basic Mud Logging Guide for educational purpose
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
RMMM.pdf make it easy to upload and study
PDF
Classroom Observation Tools for Teachers
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
Cell Types and Its function , kingdom of life
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Lesson notes of climatology university.
O7-L3 Supply Chain Operations - ICLT Program
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Basic Mud Logging Guide for educational purpose
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
human mycosis Human fungal infections are called human mycosis..pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
RMMM.pdf make it easy to upload and study
Classroom Observation Tools for Teachers
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Supply Chain Operations Speaking Notes -ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Insiders guide to clinical Medicine.pdf
Cell Structure & Organelles in detailed.
Cell Types and Its function , kingdom of life
O5-L3 Freight Transport Ops (International) V1.pdf
01-Introduction-to-Information-Management.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Lesson notes of climatology university.

Function in c++

  • 2. Introduction Functions allow a sequence of statement to be referred to by a name and parameterized. •Calling a function causes statement sequence to be executed and a value may be returned when the function terminates. •Parameterization allows the same function to be applied to many different values without changing the statements. •sqrt is function that computes square root of number, sqrt(4) computes square root of 4, sqrt(9) computer square root of 9! Same sequence of statements inside function, with different parameter returns different value. •
  • 3. Declaration Introduces the function name, function return type, and function parameters to the program. •The function body (statements)is not part of the declaration •A function must be declared before it is used Format: return_type function_name(parameter_list); parameter_list: type param1,type param2,type param3, etc • Some function declarations: • double sqrt(double); int func1(); void func2(char,int);
  • 4. Function Definition Introduces the function name, function return type, and function parameters as well as the function body to the program Function body (the implementation)is a compound statement return_type function_name(parameter_list) { // Function body } • Function Definition Example: • void print_func() { cout << “hello world” << endl; } A function can be defined in any part of the program text or within a library •
  • 5. Functions And Program Structure Functions must be declared before they are used, e.g: l// preprocessor statements lDefine function 1 lDefine function 2 lDefine function 3 l{ call function 2} lmain() l{ lCall function 1 lCall function 3 l} •
  • 6. Void Functions These are functions that do not return a value They can still cause a side effect by modifying a global variable Example: l int num = 1; lvoid print_func() l{ lcout << “hello world” << num << endl; lnum = num + 1; l} •Note: It is not possible to declare variables of type void because type void has no values, i.e. it has no structure •
  • 7. Parameterized Functions A function declaration must include the list of parameter types (with an optional name): int func(int x) •Each parameter type specifies the type of the value that should appear as a function argument when the function is called. •Therefore the function func takes one parameter which is an integer func can be called using: int x =func(42); •The function name is followed by a bracketed list of function arguments •Note: A function definition also requires a parameter list with in the function body the parameters are accessible as variables which are named in the parameter list with their types: •
  • 8. Int func(int x,int y) { // place function body here // variables x and y can be used here } x and y are the parameter variables that can be used in the function body • Note that the types of the parameters must be the same in the function declaration and definition •Formal parameters: the parameters used in the function definition, e.g. x and y in int func(int x, int y) … •
  • 9. Parameterized Functions Actual parameters or arguments: the parameters used in the function call, e.g. 10 and 20 in int x = func(10, 20); •Note that the actual parameters are evaluated before being passed to the function, e.g. • int a = 10, b =20; int x =func(a/2, a+b); • The actual parameters passed to func are 5 and 30 The order of evaluation of arguments is undefined.
  • 10. Functions and Scope Functions may only be referred to within the scope of the declaration. •Declarations made in a function body or compound statement are local to that scope. •Function parameters are variables in the scope of the function body. •A new variable is created for each parameter when ever the function is called. •Each parameter variable is destroyed when the function terminates. •Hence parameters are local to the function they are part of. •
  • 11. Default Parameters Function parameters may be given default values • int func(int x = 10); If func is called with an empty parameter list • func(); then the parameter variable x will be initialized to 10 •If func is called with a parameter func(15); then the default is NOT used and the parameter variable x is initialized to the actual parameter value,I.e.15
  • 12. Default Parameters Note that default parameters must appear at the END of the parameter list • int func1(int x = 1); int func2(int x, int y = 1); int func3(int x = 1, int y); • // this is ok // this is ok // this is not ok Also several default parameters can be given int func4(char c, int x = 1, int y = 1); this is ok
  • 13. Parameter Passing In Functions Arguments/Parameters are a way to share data between two functions. •In all the programs we have seen so far, the arguments in a function call provide data needed by the called function • they pass data in to the function There are times when a function wants to use its parameters to pass data back to the calling program. • examples are when functions must return more than one value or must modify the arguments passed to them. We need two types of parameter passing to handle these two situations. •
  • 14. Call By Value Call by value means the value of i is passed as a parameter to func The local variable I of func is initialized to that value The local variable I of main that appears in the call is left untouched -only a copy of its value is passed to the function func. •
  • 15. Call By Reference References allow a second parameter passing mechanism to be used - call by reference •If a formal parameter type is a reference type • int func(int &x) Then the parameter variable x will be a reference to the actual parameter. • int y = 1; func(y); x will be initialized to a reference to y when the function func is called So x and y will both denote the SAME object Contrast this to call by value where x will be initialized to a copy of the value of y. •
  • 16. Inline Function Inline functions are functions where the call is made to inline functions. The actual code then gets placed in the calling program. Once you define an inline function, using the 'inline' keyword, whenever you call that function the compiler will replace the function call with the actual code from the function. General form inline datatype function_name(arguments)
  • 17. Inline Function #include <iostream> using namespace std; int func(int); void main( ) { int x; cout << "n Enter the Input Value: "; cin>>x; cout << "n The Output is: " << func(x); } inline int func(int x1) { return 5*x1; } Note:-Inline functions will save time and are useful if the function is very small. If the function is large, use of inline functions must be avoided.