SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Objectives
In this chapter you will learn about
•Strict type checking
•Function Overloading
•Default Arguments
•Inline Function
• In C, Prototype is not required if the function
returns integer.
• printf can be used without including stdio.h
 In C++, prototype is compulsory even if functions
return int.
 Inclusion of stdio.h is compulsory if you are using printf
function.
C PROGRAM C PROGRAM
• Can we pass argument for function given the
following Prototype
• int func();
• In C –Yes , says that a function can take zero or more
number of arguments. (K & R Style of Prototyping)
• In C++ - No, says that a function takes zero arguments
 A feature of C++ which allows multiple functions to
have same function name
 Calls to an overloaded function are resolved using
function signatures.
 Allows multiple functions which perform similar tasks
to be grouped under a single name “single interface
and multiple implementations”
 How does C++ compiler distinguishes
between different functions when it
generates object code
 It changes names by adding information
about arguments. 
 This technique of adding additional
information to function names is
called Name Mangling
 What is the difference between strcpy
and strncpy?
 Write two functions by name mystrcpy.
• First functions takes two arguments (dest , src)
• Second takes three arguments(dest,src,n);
 “Single interface multiple implementations” is
technically termed as “Polymorphism”.
 The binding takes place during compile time, so its
called “compile time polymorphism” or “static
polymorphism”.
 Ensure that function overloading in unambiguous
int main( )
{
char str[50];
cout <<"Enter a name : ";
cin >> str;
cout <<"Entered name is " << str;
}
.getline(str , 50 );; .getline(str , 50 , ‘.’ );
• A default argument is a value provided in function declaration
that is automatically assigned by the compiler if caller of the
function doesn't provide a value for the argument.
• Allows a function to be called without providing one or more
trailing arguments.
• Default arguments should be provided only in function prototype
and shouldn’t be repeated in function definition.The compiler
uses the prototype to build a function call and not the function
definition.
• Only trailing arguments should be given default values. i.e., you
cant have a default argument followed by a non default argument.
void printBinary(int num,int bits = 8);
int main( )
{
printBinary(23);
printBinary(500 , 16);
printBinary(28 );
}
void printBinary(int num,int bits)
{
for(int i = bits - 1 ; i >= 0 ; i--)
cout << ((num & 1 << i )?1:0) ;
}
 If a function is declared as inline, at the time of
compilation, the body of the function is expanded at the
point at which it is invoked.
 For small functions, the inline function removes all the
overheads of function calls.
 To make a function inline prefix the keyword inline
before the function name Syntax is
 inline <return_Type> <Func_Name>(arguments)
 Not all requests to make a function inline are honored.
Generally C++ examines the
• complexity of the function
• should request for inline replacement only if the functions are short.
 The compiler may ignore the function as inline when:
• If there are loops, switch statements or goto’s in the function
• If there are static variables in a function
• If the function is recursive
• If the function call and function definition are not in a single translation
unit
 Inline functions are functionally similar to #define
macros. In both the cases, the body of the macro or the
function is expanded. But inline functions are preferred
over macros because of three main reasons:
• Argument Evaluation - Arguments are first evaluated and then
expanded unlike in macros where arguments are expanded and then
evaluated.
• Type Checking - The types of the arguments are checked against the
parameter list in the declaration for the function. As a result, any
mismatch in the parameters can be detected at the time of
compilation.
• Overloading - inline functions can be overloaded, which is not
possible in the case of macros.
#define square(x) x * x
OR
inline int square(int x)
{
return x * x;
}
int main( )
{
int x = 5;
cout << square( x + 2) << endl;
return 0;
}
• Prototypes are a must in C++
• Every function should have a explicit return-type.
• Multiple functions can have same name with
different signature.
• Default parameters allows us to pass lesser
arguments to functions.
• Default parameters will help in combining multiple
functions into one.
• Inline functions to be used as alternative to macros
• Reference is an alternate name given to existing
variable
Functions in C++

Weitere ähnliche Inhalte

Was ist angesagt?

Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++Neeru Mittal
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++Nitin Jawla
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++Learn By Watch
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++Bhavik Vashi
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language Mohamed Loey
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++Ankur Pandey
 

Was ist angesagt? (20)

Functions in C
Functions in CFunctions in C
Functions in C
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Functions in c
Functions in cFunctions in c
Functions in c
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
 

Andere mochten auch

16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 
String & its application
String & its applicationString & its application
String & its applicationTech_MX
 
Function in c++
Function in c++Function in c++
Function in c++Kumar
 
Netw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire classNetw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire classEugenioBrown1
 
Functions c++ مشروع
Functions c++ مشروعFunctions c++ مشروع
Functions c++ مشروعziadalmulla
 
Network-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in PersianNetwork-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in PersianMuhibullah Aman
 
Network Security in 2016
Network Security in 2016Network Security in 2016
Network Security in 2016Qrator Labs
 
Functions in c++
Functions in c++Functions in c++
Functions in c++Maaz Hasan
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3Ali Aminian
 
Basic openCV Functions Using CPP
Basic openCV Functions Using CPPBasic openCV Functions Using CPP
Basic openCV Functions Using CPPWei-Wen Hsu
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You ExposedWeb Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You ExposedImperva
 
C programming & data structure [character strings & string functions]
C programming & data structure   [character strings & string functions]C programming & data structure   [character strings & string functions]
C programming & data structure [character strings & string functions]MomenMostafa
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++Aabha Tiwari
 
C++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer CentreC++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer Centrejatin batra
 

Andere mochten auch (20)

16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
String & its application
String & its applicationString & its application
String & its application
 
Function in c++
Function in c++Function in c++
Function in c++
 
Netw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire classNetw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire class
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
 
C++
C++C++
C++
 
Functions c++ مشروع
Functions c++ مشروعFunctions c++ مشروع
Functions c++ مشروع
 
Network-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in PersianNetwork-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in Persian
 
Network Security in 2016
Network Security in 2016Network Security in 2016
Network Security in 2016
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
The Algebra of Functions
The Algebra of FunctionsThe Algebra of Functions
The Algebra of Functions
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
Basic openCV Functions Using CPP
Basic openCV Functions Using CPPBasic openCV Functions Using CPP
Basic openCV Functions Using CPP
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You ExposedWeb Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
 
C programming & data structure [character strings & string functions]
C programming & data structure   [character strings & string functions]C programming & data structure   [character strings & string functions]
C programming & data structure [character strings & string functions]
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
C++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer CentreC++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer Centre
 

Ähnlich wie Functions in C++

Ähnlich wie Functions in C++ (20)

Function in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programmingFunction in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programming
 
Functions
FunctionsFunctions
Functions
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuples
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
c++ UNIT II.pptx
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
Functions and modular programming.pptx
Functions and modular programming.pptxFunctions and modular programming.pptx
Functions and modular programming.pptx
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
4th unit full
4th unit full4th unit full
4th unit full
 
Function
Function Function
Function
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
 
1.6 Function.pdf
1.6 Function.pdf1.6 Function.pdf
1.6 Function.pdf
 

Mehr von Mohammed Sikander (20)

Operator Overloading in C++
Operator Overloading in C++Operator Overloading in C++
Operator Overloading in C++
 
Python_Regular Expression
Python_Regular ExpressionPython_Regular Expression
Python_Regular Expression
 
Modern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptxModern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptx
 
Modern_cpp_auto.pdf
Modern_cpp_auto.pdfModern_cpp_auto.pdf
Modern_cpp_auto.pdf
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Python strings
Python stringsPython strings
Python strings
 
Python set
Python setPython set
Python set
 
Python list
Python listPython list
Python list
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
 
Pointer basics
Pointer basicsPointer basics
Pointer basics
 
Signal
SignalSignal
Signal
 
File management
File managementFile management
File management
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
Java strings
Java   stringsJava   strings
Java strings
 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flow
 

Kürzlich hochgeladen

Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 

Kürzlich hochgeladen (20)

Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 

Functions in C++

  • 1.
  • 2. Objectives In this chapter you will learn about •Strict type checking •Function Overloading •Default Arguments •Inline Function
  • 3. • In C, Prototype is not required if the function returns integer. • printf can be used without including stdio.h
  • 4.  In C++, prototype is compulsory even if functions return int.  Inclusion of stdio.h is compulsory if you are using printf function.
  • 5. C PROGRAM C PROGRAM
  • 6. • Can we pass argument for function given the following Prototype • int func(); • In C –Yes , says that a function can take zero or more number of arguments. (K & R Style of Prototyping) • In C++ - No, says that a function takes zero arguments
  • 7.  A feature of C++ which allows multiple functions to have same function name  Calls to an overloaded function are resolved using function signatures.  Allows multiple functions which perform similar tasks to be grouped under a single name “single interface and multiple implementations”
  • 8.
  • 9.  How does C++ compiler distinguishes between different functions when it generates object code  It changes names by adding information about arguments.   This technique of adding additional information to function names is called Name Mangling
  • 10.
  • 11.  What is the difference between strcpy and strncpy?  Write two functions by name mystrcpy. • First functions takes two arguments (dest , src) • Second takes three arguments(dest,src,n);
  • 12.
  • 13.  “Single interface multiple implementations” is technically termed as “Polymorphism”.  The binding takes place during compile time, so its called “compile time polymorphism” or “static polymorphism”.  Ensure that function overloading in unambiguous
  • 14.
  • 15. int main( ) { char str[50]; cout <<"Enter a name : "; cin >> str; cout <<"Entered name is " << str; } .getline(str , 50 );; .getline(str , 50 , ‘.’ );
  • 16. • A default argument is a value provided in function declaration that is automatically assigned by the compiler if caller of the function doesn't provide a value for the argument. • Allows a function to be called without providing one or more trailing arguments.
  • 17. • Default arguments should be provided only in function prototype and shouldn’t be repeated in function definition.The compiler uses the prototype to build a function call and not the function definition. • Only trailing arguments should be given default values. i.e., you cant have a default argument followed by a non default argument.
  • 18. void printBinary(int num,int bits = 8); int main( ) { printBinary(23); printBinary(500 , 16); printBinary(28 ); } void printBinary(int num,int bits) { for(int i = bits - 1 ; i >= 0 ; i--) cout << ((num & 1 << i )?1:0) ; }
  • 19.
  • 20.
  • 21.  If a function is declared as inline, at the time of compilation, the body of the function is expanded at the point at which it is invoked.  For small functions, the inline function removes all the overheads of function calls.  To make a function inline prefix the keyword inline before the function name Syntax is  inline <return_Type> <Func_Name>(arguments)
  • 22.  Not all requests to make a function inline are honored. Generally C++ examines the • complexity of the function • should request for inline replacement only if the functions are short.  The compiler may ignore the function as inline when: • If there are loops, switch statements or goto’s in the function • If there are static variables in a function • If the function is recursive • If the function call and function definition are not in a single translation unit
  • 23.  Inline functions are functionally similar to #define macros. In both the cases, the body of the macro or the function is expanded. But inline functions are preferred over macros because of three main reasons: • Argument Evaluation - Arguments are first evaluated and then expanded unlike in macros where arguments are expanded and then evaluated. • Type Checking - The types of the arguments are checked against the parameter list in the declaration for the function. As a result, any mismatch in the parameters can be detected at the time of compilation. • Overloading - inline functions can be overloaded, which is not possible in the case of macros.
  • 24.
  • 25.
  • 26. #define square(x) x * x OR inline int square(int x) { return x * x; } int main( ) { int x = 5; cout << square( x + 2) << endl; return 0; }
  • 27.
  • 28. • Prototypes are a must in C++ • Every function should have a explicit return-type. • Multiple functions can have same name with different signature. • Default parameters allows us to pass lesser arguments to functions. • Default parameters will help in combining multiple functions into one. • Inline functions to be used as alternative to macros • Reference is an alternate name given to existing variable

Hinweis der Redaktion

  1. (No assumption that a function returns int if no prototype is mentioned as in C) int main() { int result = add(5 , 8); printf(&amp;quot; %d&amp;quot; , result); } int add(int a,int b) { return a + b; } Change the extension to c++ and compile the program
  2. http://stackoverflow.com/questions/3092006/function-declaration-kr-vs-ansi In C, A non void function can skip the return statement(No compilation error, returns some garbage)
  3. OVERLOADING A FUNCTION NAME If you have two or more function definitions for the same function name, that is called overloading. When you overload a function name, the function definitions must have different numbers of formal parameters or some formal parameters of different types. When there is a function call, the compiler uses the function definition whose number of formal parameters and types of formal parameters match the arguments in the function call. (Interface always refers to “function names” and implementation always refers to “data and function definition”). A best match must be unique. Function selection involves the following steps. The compiler first tries to find an exact match in which the types of actual arguments are the same and use that function. If an exact match is not found, the compiler uses the integral promotions to actual arguments, such as, char to int, or float to double to find the match. In case of multiple matches the compiler will generate an error message.
  4. #include &amp;lt;iostream&amp;gt; using namespace std; double area(int radius) { return 3.14 * radius * radius; } int area(int length,int breadth) { return length * breadth; } int main() { cout &amp;lt;&amp;lt; area(5) &amp;lt;&amp;lt; endl; cout &amp;lt;&amp;lt; area(5 , 3) &amp;lt;&amp;lt; endl; }
  5. C++ supports function overloading, i.e., there can be more than one functions with same name and differences in parameters. How does C++ compiler distinguishes between different functions when it generates object code – it changes names by adding information about arguments. This technique of adding additional information to function names is called Name Mangling
  6. #include &amp;lt;iostream&amp;gt; using namespace std; void mystrcpy(char *dest,const char *src) { while((*dest = *src)) { dest++; src++; } } void mystrcpy(char *dest,const char *src,int n) { for(int i = 0 ; i &amp;lt; n &amp;&amp; (*dest = *src) ; i++) { dest++; src++; } } int main() { char src[20] = &amp;quot;SIKANDER&amp;quot;; char dest1[20] = &amp;quot;&amp;quot;; char dest2[20] = &amp;quot;&amp;quot;; mystrcpy(dest1 , src); mystrcpy(dest2 , src , 3); cout &amp;lt;&amp;lt;&amp;quot;Dest 1 = &amp;quot; &amp;lt;&amp;lt; dest1 &amp;lt;&amp;lt; endl; cout &amp;lt;&amp;lt;&amp;quot;Dest 2 = &amp;quot; &amp;lt;&amp;lt; dest2 &amp;lt;&amp;lt; endl; }
  7. default_printbinary.cpp
  8. #include &amp;lt;iostream&amp;gt; #include &amp;lt;cstdio&amp;gt; using namespace std; void myprint(const char *str) { printf(str); } void myprint(const char *str,FILE *fp) { fprintf(fp , str); } int main( ){ myprint(&amp;quot;HELLO WORLD&amp;quot;); //Print to Monitor FILE *fp = fopen(&amp;quot;myfile.txt&amp;quot; , &amp;quot;w&amp;quot;); myprint(&amp;quot;WELCOME TO BANGALORE&amp;quot; , fp); //Print to file }
  9. #include &amp;lt;iostream&amp;gt; #include &amp;lt;cstdio&amp;gt; using namespace std; void myprint(const char *str,FILE *fp = stdout) { fprintf(fp , str); } int main( ){ myprint(&amp;quot;HELLO WORLD&amp;quot;); //Print to Monitor FILE *fp = fopen(&amp;quot;myfile.txt&amp;quot; , &amp;quot;w&amp;quot;); myprint(&amp;quot;WELCOME TO BANGALORE&amp;quot; , fp); //Print to file }
  10. Inlinefunc_macro.cpp
  11. Inline functions have internal linkage, can be placed in header files