SlideShare a Scribd company logo
1 of 40
Prepared By: Asst. Prof. Sejal Jadav
Unit-1
Principles of Object Oriented Programming Tokens,
expressions & Control Statements
(14 MARKS)
B.C.A & B.Sc.(IT) – 3
CS-13 C++ and Object Oriented
Programming
Prepared By: Asst. Prof. Sejal Jadav
Functions in c++
• What is Function?
• Definition, Declaration & Call
• Declaration
• Way to define a function
• Formal & Actual Arguments
• Types of formal arguments
• Call by value, call by address and call by reference
Prepared By: Asst. Prof. Sejal Jadav
What is Function?
• Function is block of code performing a unit
task.
• Function definition is block of code.
• Function is a way to achieve modularization.
(no. of small functions)
Prepared By: Asst. Prof. Sejal Jadav
• Function are of two types : Library (predefined)
function and user –defined
• Predefined functions are
• declared in header files and
• defined in library files
Prepared By: Asst. Prof. Sejal Jadav
Function Definition, Declaration & Call
#include<iostream>
using namespace std;
main()
{
void fun();
Cout<<“You are in main”;
fun();
}
void fun()
{
Cout<<“you are in fun”;
}
Function Definition
Function Call
Function Declaration
Declaration of cout & cin
Prepared By: Asst. Prof. Sejal Jadav
Function Declaration [Function PROTOTYPE]
•Function Declaration is also known as Function
Prototype.
•Functions need to be declared before use (just
like variable).
Prepared By: Asst. Prof. Sejal Jadav
•Function can be declared locally or globally.
•Function has a name, return type and
arguments.
Syntax:
•ReturnType functionName(argumentList);
Prepared By: Asst. Prof. Sejal Jadav
Way to define a function
• No argument no return value
• With argument no return value
• No argument with return value
• With argument with return value
Prepared By: Asst. Prof. Sejal Jadav
5
6
Formal & Actual Arguments
#include<iostream>
int sum(int,int);
main()
{
int a=5,b=6;
int s=sum(a,b);
Cout<<“Sum is :
“<<s;
}
int sum(int x, int y)
{
Return (x+y);
}
a
b
s
a & b are actual argument
Sum() memory
block
main()
memory
block
x & y are formal argument
5
6
11
x
y
Prepared By: Asst. Prof. Sejal Jadav
Types of Formal Argument
• Formal arguments can be of three types
• Ordinary variables of any type
• Pointer variables
• Reference variables
Prepared By: Asst. Prof. Sejal Jadav
• On the basis of arguments there are two types of
function are available in C++ language, they are;
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
• There are two ways to pass value or data to function
in C++ language which is given below;
Prepared By: Asst. Prof. Sejal Jadav
1. Ordinary Variables of any type
Prepared By: Asst. Prof. Sejal Jadav
2. Pointer variables
Prepared By: Asst. Prof. Sejal Jadav
3. Call by reference
Prepared By: Asst. Prof. Sejal Jadav
Call by reference
• When formal arguments are reference variables, it is
function call by reference.
• The call by reference method the called function
does not create its own copy rather it refers to
original value only by different name i.e. reference.
Prepared By: Asst. Prof. Sejal Jadav
• When we pass arguments by reference, the formal
arguments int sum(int &x,int &y) in the called
function becomes aliases to the actual arguments in
the calling function.
• (actual arguments -> aliases -> formal arguments )
• It means that when the called function is working
with its own arguments, it is actually working on the
original data.
Prepared By: Asst. Prof. Sejal Jadav
Inline function
Benefits of Function:
• Easy to read
• Easy to modify
• Avoids rewriting of same code
• Easy to debug
• Better memory utilization
Prepared By: Asst. Prof. Sejal Jadav
Function saves memory
•Function in a memory is to save memory space.
which becomes appreciable when a function is
likely to be called many times.
Prepared By: Asst. Prof. Sejal Jadav
Function is time consuming:
• However every time a function is called, it takes lot of
extra time in executing a series of instructions for
tasks such as jumping to the functions, saving
registers, pushing arguments into the stack and
returning to the calling function.
Prepared By: Asst. Prof. Sejal Jadav
• So, when function is small it is worthless to spend so
much extra time in such tasks in cost of saving
comparatively small space.
Prepared By: Asst. Prof. Sejal Jadav
Inline function
•To eliminate the cost of calls to small functions,
C++ proposes a new feature called inline
function.
•An inline function is a function that is expanded
in line when it is invoked.
Prepared By: Asst. Prof. Sejal Jadav
•Inline is a request not a command.
•The benefit of speed of inline functions reduces
as the function grows in size.
Prepared By: Asst. Prof. Sejal Jadav
•So the compiler may ignore the request in some
situations. Few of them:
• Function containing loops, switch, goto.
• Functions with recursion.
•Containing static variable.
Prepared By: Asst. Prof. Sejal Jadav
Syntax:
inline<function header>
{
Function body
}
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
#include<iostream>
using namespace std;
inline void address();
main()
{
address();
}
void address()
{
cout<<endl<<"CCSIT";
cout<<endl<<"Junagadh";
}
Prepared By: Asst. Prof. Sejal Jadav
Default Arguments in C++ Functions
• The default arguments are used when you provide no
arguments or only few arguments while calling a
function.
• The default arguments are used during compilation of
program.
• For example, lets say you have a user-defined function
sum declared like this: int sum(int a=10, int b=20),
Prepared By: Asst. Prof. Sejal Jadav
• For example: int sum(int a=10, int b=20),
• now while calling this function you do not provide
any arguments, simply called sum(); then in this case
the result would be 30, compiler used the default
values 10 and 20 declared in function signature.
• If you pass only one argument like this: sum(80) then
the result would be 100, using the passed argument
80 as first value and 20 taken from the default
argument.
Prepared By: Asst. Prof. Sejal Jadav
• Let’s look at example:01_defaultArg.Cpp
Prepared By: Asst. Prof. Sejal Jadav
Rules of default arguments
• As you have seen in the above example that I have
assigned the default values for only two arguments b
and c during function declaration.
• It is up to you to assign default values to all
arguments or only selected arguments but remember
the following rule while assigning default values to
only some of the arguments:
Prepared By: Asst. Prof. Sejal Jadav
• If you assign default value to an argument, the
subsequent arguments must have default values
assigned to them, else you will get compilation error.
Prepared By: Asst. Prof. Sejal Jadav
• For example: Lets see some valid and invalid cases.
• Valid: Following function declarations are valid –
int sum(int a=10, int b=20, int c=30);
int sum(int a, int b=20, int c=30);
int sum(int a, int b, int c=30);
Prepared By: Asst. Prof. Sejal Jadav
• Invalid: Following function declarations are invalid –
• Since a(a variable) has default value assigned, all the
arguments after a (in this case b and c) must have
default values assigned
int sum(int a=10, int b, int c=30);
Prepared By: Asst. Prof. Sejal Jadav
• Since b has default value assigned, all the arguments
after b (in this case c) must have default values
assigned.
int sum(int a, int b=20, int c);
Prepared By: Asst. Prof. Sejal Jadav
• Since a has default value assigned, all the arguments
after a (in this case b and c) must have default values
assigned, b has default value but c doesn't have, that’s
why this is also invalid
int sum(int a=10, int b=20, int c);
Prepared By: Asst. Prof. Sejal Jadav
Const arguments
• The constant variable can be declared using
const keyword.
• The const keyword makes variable value stable.
• The constant variable should be initialized
while declaring.
Prepared By: Asst. Prof. Sejal Jadav
• Syntax :
<function name> (const <type> <variable name>);
• The qualifier ‘const’ tells the compiler that the
function should not modify the argument.
• Let’s Look at example: DefaultArg.cpp
Prepared By: Asst. Prof. Sejal Jadav
void add(const int a, int b)
{
a=a+b; //error
b=a+b;
cout<<“Sum = “<<b;
}
main()
{
add(2,5);
}
• In the above program, the prototype
of function add declared two
arguments.
• Out of two arguments the first
argument is a constant. The second
argument is not constant.
• When function add is invoked, the
first argument a is a const according
to prototype of function.
• Any attempt made to change the
value of variable a will cause an
error message “cannot modify a
constant variable.”

More Related Content

What's hot

Functions in C++ (OOP)
Functions in C++ (OOP)Functions in C++ (OOP)
Functions in C++ (OOP)Faizan Janjua
 
C++ functions
C++ functionsC++ functions
C++ functionsDawood Jutt
 
C++ Function
C++ FunctionC++ Function
C++ FunctionPingLun Liao
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 
CSEG1001 Unit 4 Functions and Pointers
CSEG1001 Unit 4 Functions and PointersCSEG1001 Unit 4 Functions and Pointers
CSEG1001 Unit 4 Functions and PointersDhiviya Rose
 
C++ unit-1-part-13
C++ unit-1-part-13C++ unit-1-part-13
C++ unit-1-part-13Jadavsejal
 
Functions in C++
Functions in C++Functions in C++
Functions in C++Nikhil Pandit
 
Java8 javatime-api
Java8 javatime-apiJava8 javatime-api
Java8 javatime-apiJini Lee
 
Functions in c++
Functions in c++Functions in c++
Functions in c++HalaiHansaika
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 
C++ functions
C++ functionsC++ functions
C++ functionsMayank Jain
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsDhiviya Rose
 
DotNet programming & Practices
DotNet programming & PracticesDotNet programming & Practices
DotNet programming & PracticesDev Raj Gautam
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functionssathish sak
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_cForwardBlog Enewzletter
 
C++ overloading
C++ overloadingC++ overloading
C++ overloadingsanya6900
 
C workshop day 7
C workshop day 7C workshop day 7
C workshop day 7Mayank Agrawal
 

What's hot (18)

Functions in C++ (OOP)
Functions in C++ (OOP)Functions in C++ (OOP)
Functions in C++ (OOP)
 
C++ functions
C++ functionsC++ functions
C++ functions
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
CSEG1001 Unit 4 Functions and Pointers
CSEG1001 Unit 4 Functions and PointersCSEG1001 Unit 4 Functions and Pointers
CSEG1001 Unit 4 Functions and Pointers
 
C++ unit-1-part-13
C++ unit-1-part-13C++ unit-1-part-13
C++ unit-1-part-13
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Java8 javatime-api
Java8 javatime-apiJava8 javatime-api
Java8 javatime-api
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
C++ functions
C++ functionsC++ functions
C++ functions
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and Strings
 
DotNet programming & Practices
DotNet programming & PracticesDotNet programming & Practices
DotNet programming & Practices
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
C workshop day 7
C workshop day 7C workshop day 7
C workshop day 7
 

Similar to C++ unit-1-part-12

C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10Jadavsejal
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5Jadavsejal
 
C++ unit-1-part-9
C++ unit-1-part-9C++ unit-1-part-9
C++ unit-1-part-9Jadavsejal
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 developmentFisnik Doko
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8Jadavsejal
 
User defined functions
User defined functionsUser defined functions
User defined functionsRokonuzzaman Rony
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++Nilesh Dalvi
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringSri Harsha Pamu
 
C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)Christian Nagel
 
JavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdfJavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdfkatarichallenge
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffJAX London
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxvanshhans21102005
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7Jadavsejal
 

Similar to C++ unit-1-part-12 (20)

Functions
FunctionsFunctions
Functions
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5
 
C++ unit-1-part-9
C++ unit-1-part-9C++ unit-1-part-9
C++ unit-1-part-9
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 development
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
embedded C.pptx
embedded C.pptxembedded C.pptx
embedded C.pptx
 
L4 functions
L4 functionsL4 functions
L4 functions
 
C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)
 
JavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdfJavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdf
 
Java 8
Java 8Java 8
Java 8
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard Wolff
 
Lec-1c.pdf
Lec-1c.pdfLec-1c.pdf
Lec-1c.pdf
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7
 

More from Jadavsejal

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassJadavsejal
 
Programming Java Concept of Event Handling
Programming Java Concept of Event HandlingProgramming Java Concept of Event Handling
Programming Java Concept of Event HandlingJadavsejal
 
Programming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingProgramming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingJadavsejal
 
concept of applet and concept of Layout Managers
concept of applet and concept of Layout Managersconcept of applet and concept of Layout Managers
concept of applet and concept of Layout ManagersJadavsejal
 
C++ unit-2-part-3
C++ unit-2-part-3C++ unit-2-part-3
C++ unit-2-part-3Jadavsejal
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2Jadavsejal
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1Jadavsejal
 
C++ unit-1-part-15
C++ unit-1-part-15C++ unit-1-part-15
C++ unit-1-part-15Jadavsejal
 
C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14Jadavsejal
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6Jadavsejal
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4Jadavsejal
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2Jadavsejal
 
C++ unit-1-part-3
C++ unit-1-part-3C++ unit-1-part-3
C++ unit-1-part-3Jadavsejal
 
C++-Unit-1-Part-1
C++-Unit-1-Part-1C++-Unit-1-Part-1
C++-Unit-1-Part-1Jadavsejal
 
05_system architecture
05_system architecture05_system architecture
05_system architectureJadavsejal
 
04 data flow architecture
04 data flow architecture 04 data flow architecture
04 data flow architecture Jadavsejal
 
Vpn protocols
Vpn protocolsVpn protocols
Vpn protocolsJadavsejal
 
04 authentication
04 authentication04 authentication
04 authenticationJadavsejal
 

More from Jadavsejal (20)

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone Class
 
Programming Java Concept of Event Handling
Programming Java Concept of Event HandlingProgramming Java Concept of Event Handling
Programming Java Concept of Event Handling
 
Programming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingProgramming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event Handling
 
concept of applet and concept of Layout Managers
concept of applet and concept of Layout Managersconcept of applet and concept of Layout Managers
concept of applet and concept of Layout Managers
 
C++ unit-2-part-3
C++ unit-2-part-3C++ unit-2-part-3
C++ unit-2-part-3
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1
 
C++ unit-1-part-15
C++ unit-1-part-15C++ unit-1-part-15
C++ unit-1-part-15
 
C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2
 
C++ unit-1-part-3
C++ unit-1-part-3C++ unit-1-part-3
C++ unit-1-part-3
 
C++-Unit-1-Part-1
C++-Unit-1-Part-1C++-Unit-1-Part-1
C++-Unit-1-Part-1
 
05_system architecture
05_system architecture05_system architecture
05_system architecture
 
04 data flow architecture
04 data flow architecture 04 data flow architecture
04 data flow architecture
 
Vpn protocols
Vpn protocolsVpn protocols
Vpn protocols
 
04 authentication
04 authentication04 authentication
04 authentication
 
03 cia
03 cia03 cia
03 cia
 
01
01 01
01
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAĐĄY_INDEX-DM_23-1-final-eng.pdf
 

C++ unit-1-part-12

  • 1. Prepared By: Asst. Prof. Sejal Jadav Unit-1 Principles of Object Oriented Programming Tokens, expressions & Control Statements (14 MARKS) B.C.A & B.Sc.(IT) – 3 CS-13 C++ and Object Oriented Programming
  • 2. Prepared By: Asst. Prof. Sejal Jadav Functions in c++ • What is Function? • Definition, Declaration & Call • Declaration • Way to define a function • Formal & Actual Arguments • Types of formal arguments • Call by value, call by address and call by reference
  • 3. Prepared By: Asst. Prof. Sejal Jadav What is Function? • Function is block of code performing a unit task. • Function definition is block of code. • Function is a way to achieve modularization. (no. of small functions)
  • 4. Prepared By: Asst. Prof. Sejal Jadav • Function are of two types : Library (predefined) function and user –defined • Predefined functions are • declared in header files and • defined in library files
  • 5. Prepared By: Asst. Prof. Sejal Jadav Function Definition, Declaration & Call #include<iostream> using namespace std; main() { void fun(); Cout<<“You are in main”; fun(); } void fun() { Cout<<“you are in fun”; } Function Definition Function Call Function Declaration Declaration of cout & cin
  • 6. Prepared By: Asst. Prof. Sejal Jadav Function Declaration [Function PROTOTYPE] •Function Declaration is also known as Function Prototype. •Functions need to be declared before use (just like variable).
  • 7. Prepared By: Asst. Prof. Sejal Jadav •Function can be declared locally or globally. •Function has a name, return type and arguments. Syntax: •ReturnType functionName(argumentList);
  • 8. Prepared By: Asst. Prof. Sejal Jadav Way to define a function • No argument no return value • With argument no return value • No argument with return value • With argument with return value
  • 9. Prepared By: Asst. Prof. Sejal Jadav 5 6 Formal & Actual Arguments #include<iostream> int sum(int,int); main() { int a=5,b=6; int s=sum(a,b); Cout<<“Sum is : “<<s; } int sum(int x, int y) { Return (x+y); } a b s a & b are actual argument Sum() memory block main() memory block x & y are formal argument 5 6 11 x y
  • 10. Prepared By: Asst. Prof. Sejal Jadav Types of Formal Argument • Formal arguments can be of three types • Ordinary variables of any type • Pointer variables • Reference variables
  • 11. Prepared By: Asst. Prof. Sejal Jadav • On the basis of arguments there are two types of function are available in C++ language, they are;
  • 12. Prepared By: Asst. Prof. Sejal Jadav
  • 13. Prepared By: Asst. Prof. Sejal Jadav • There are two ways to pass value or data to function in C++ language which is given below;
  • 14. Prepared By: Asst. Prof. Sejal Jadav 1. Ordinary Variables of any type
  • 15. Prepared By: Asst. Prof. Sejal Jadav 2. Pointer variables
  • 16. Prepared By: Asst. Prof. Sejal Jadav 3. Call by reference
  • 17. Prepared By: Asst. Prof. Sejal Jadav Call by reference • When formal arguments are reference variables, it is function call by reference. • The call by reference method the called function does not create its own copy rather it refers to original value only by different name i.e. reference.
  • 18. Prepared By: Asst. Prof. Sejal Jadav • When we pass arguments by reference, the formal arguments int sum(int &x,int &y) in the called function becomes aliases to the actual arguments in the calling function. • (actual arguments -> aliases -> formal arguments ) • It means that when the called function is working with its own arguments, it is actually working on the original data.
  • 19. Prepared By: Asst. Prof. Sejal Jadav Inline function Benefits of Function: • Easy to read • Easy to modify • Avoids rewriting of same code • Easy to debug • Better memory utilization
  • 20. Prepared By: Asst. Prof. Sejal Jadav Function saves memory •Function in a memory is to save memory space. which becomes appreciable when a function is likely to be called many times.
  • 21. Prepared By: Asst. Prof. Sejal Jadav Function is time consuming: • However every time a function is called, it takes lot of extra time in executing a series of instructions for tasks such as jumping to the functions, saving registers, pushing arguments into the stack and returning to the calling function.
  • 22. Prepared By: Asst. Prof. Sejal Jadav • So, when function is small it is worthless to spend so much extra time in such tasks in cost of saving comparatively small space.
  • 23. Prepared By: Asst. Prof. Sejal Jadav Inline function •To eliminate the cost of calls to small functions, C++ proposes a new feature called inline function. •An inline function is a function that is expanded in line when it is invoked.
  • 24. Prepared By: Asst. Prof. Sejal Jadav •Inline is a request not a command. •The benefit of speed of inline functions reduces as the function grows in size.
  • 25. Prepared By: Asst. Prof. Sejal Jadav •So the compiler may ignore the request in some situations. Few of them: • Function containing loops, switch, goto. • Functions with recursion. •Containing static variable.
  • 26. Prepared By: Asst. Prof. Sejal Jadav Syntax: inline<function header> { Function body }
  • 27. Prepared By: Asst. Prof. Sejal Jadav
  • 28. Prepared By: Asst. Prof. Sejal Jadav #include<iostream> using namespace std; inline void address(); main() { address(); } void address() { cout<<endl<<"CCSIT"; cout<<endl<<"Junagadh"; }
  • 29. Prepared By: Asst. Prof. Sejal Jadav Default Arguments in C++ Functions • The default arguments are used when you provide no arguments or only few arguments while calling a function. • The default arguments are used during compilation of program. • For example, lets say you have a user-defined function sum declared like this: int sum(int a=10, int b=20),
  • 30. Prepared By: Asst. Prof. Sejal Jadav • For example: int sum(int a=10, int b=20), • now while calling this function you do not provide any arguments, simply called sum(); then in this case the result would be 30, compiler used the default values 10 and 20 declared in function signature. • If you pass only one argument like this: sum(80) then the result would be 100, using the passed argument 80 as first value and 20 taken from the default argument.
  • 31. Prepared By: Asst. Prof. Sejal Jadav • Let’s look at example:01_defaultArg.Cpp
  • 32. Prepared By: Asst. Prof. Sejal Jadav Rules of default arguments • As you have seen in the above example that I have assigned the default values for only two arguments b and c during function declaration. • It is up to you to assign default values to all arguments or only selected arguments but remember the following rule while assigning default values to only some of the arguments:
  • 33. Prepared By: Asst. Prof. Sejal Jadav • If you assign default value to an argument, the subsequent arguments must have default values assigned to them, else you will get compilation error.
  • 34. Prepared By: Asst. Prof. Sejal Jadav • For example: Lets see some valid and invalid cases. • Valid: Following function declarations are valid – int sum(int a=10, int b=20, int c=30); int sum(int a, int b=20, int c=30); int sum(int a, int b, int c=30);
  • 35. Prepared By: Asst. Prof. Sejal Jadav • Invalid: Following function declarations are invalid – • Since a(a variable) has default value assigned, all the arguments after a (in this case b and c) must have default values assigned int sum(int a=10, int b, int c=30);
  • 36. Prepared By: Asst. Prof. Sejal Jadav • Since b has default value assigned, all the arguments after b (in this case c) must have default values assigned. int sum(int a, int b=20, int c);
  • 37. Prepared By: Asst. Prof. Sejal Jadav • Since a has default value assigned, all the arguments after a (in this case b and c) must have default values assigned, b has default value but c doesn't have, that’s why this is also invalid int sum(int a=10, int b=20, int c);
  • 38. Prepared By: Asst. Prof. Sejal Jadav Const arguments • The constant variable can be declared using const keyword. • The const keyword makes variable value stable. • The constant variable should be initialized while declaring.
  • 39. Prepared By: Asst. Prof. Sejal Jadav • Syntax : <function name> (const <type> <variable name>); • The qualifier ‘const’ tells the compiler that the function should not modify the argument. • Let’s Look at example: DefaultArg.cpp
  • 40. Prepared By: Asst. Prof. Sejal Jadav void add(const int a, int b) { a=a+b; //error b=a+b; cout<<“Sum = “<<b; } main() { add(2,5); } • In the above program, the prototype of function add declared two arguments. • Out of two arguments the first argument is a constant. The second argument is not constant. • When function add is invoked, the first argument a is a const according to prototype of function. • Any attempt made to change the value of variable a will cause an error message “cannot modify a constant variable.”