SlideShare a Scribd company logo
// Functions that take no arguments
#include <iostream>
using namespace std;
void function1();
void function2( void );
int main()
{
function1();
function2();
return 0;
}
void function1()
{
cout << "function1 takes no arguments" << endl;
}
void function2( void )
{
cout << "function2 also takes no arguments" << endl;
}
Example
#include <iostream>
using namespace std;
void printStars();
int main()
{
printStars(); //Line 1
cout << "********** Annual ***********" << endl; //Line 2
printStars(); //Line 3
cout << "******* Spring Sale **********" << endl; //Line 4
printStars(); //Line 5
return 0;
}
void printStars()
{
cout << "******************************" << endl;
cout << "******************************" << endl;
}
solution
#include<iostream>
using namespace std;
void square( int m ); // function prototype
//it can be int square( int );
int main()
{
for ( int x = 1; x <= 10; x++ )
square( x ) ; //calling statement x is actual parameter
cout << endl;
return 0;
}
// Function definition
void square( int y ) // Heading y is Formal Parameter
{
return y * y; // The return Statement
}
#include<iostream>
using namespace std;
void square( int m ); // function prototype
//it can be int square( int );
int main()
{
for ( int x = 1; x <= 10; x++ )
square( x ) ; //calling statement x is actual parameter
cout << endl;
return 0;
}
// Function definition
void square( int y ) // Heading y is Formal Parameter
{
cout<< y * y<<endl; // The return Statement
}
)Reference and Value
parameters)
Modify (Value parameters)
#include<iostream>
using namespace std;
void modify( int m ); // function prototype
int main()
{
int x;
x=7;
cout<<"x befor calling modify function = "<<x<<endl;
modify( x ) ; //calling statement x is actual parameter
cout<<"x After calling modify function = "<<x<<endl;
cout << endl;
return 0;
}
// Function definition
void modify( int y ) // Heading y is Formal Parameter
{
y=y+10; // The return Statement
cout<<"y inside modify function = "<<y<<endl;
}
Reference parameters
#include<iostream>
using namespace std;
void modify( int &m ); // function prototype
int main()
{
int x;
x=7;
cout<<"x befor calling modify function = "<<x<<endl;
modify( x ) ; //calling statement x is actual parameter
cout<<"x After calling modify function = "<<x<<endl;
cout << endl;
return 0;
}
// Function definition
void modify( int &y ) // Heading y is Formal Parameter
{
y=y+10; // The return Statement
cout<<"y inside modify function = "<<y<<endl;
}
Reference
// References must be initialized
#include <iostream>
using namespace std;
int main()
{
int x = 3, &y = x; // y is now an alias for x
cout << "x = " << x << endl << "y = " << y << endl;
y = 7;
cout << "x = " << x << endl << "y = " << y << endl;
return 0;
}
// A scoping example
#include <iostream>
using namespace std;
void a( ); // function prototype
void b( ); // function prototype
void c( void ); // function prototype
int x = 1; // global variable
int main()
{
int x = 5; // local variable to main
cout << "local x in outer scope of main is " << x << endl;
{ // start new scope
int x = 7;
cout << "local x in inner scope of main is " << x << endl;
} // end new scope
cout << "globel x in outer scope of main is " << ::x << endl;
cout << "local x in outer scope of main is " << x << endl;
cout<<"*************************************************"<<endl;
a(); // a has automatic local x
b(); // b has static local x
c(); // c uses global x
cout<<"*************************************************"<<endl;
cout<<"Afetr second calling"<<endl;
a(); // a reinitializes automatic local x
b(); // static local x retains its previous value
c(); // global x also retains its value
cout <<endl<< "local x in main is " << x <<endl<< endl;
return 0;
}
5
7
1
5
5
void a( )
{
int x = 25; // initialized each time a is called
cout << endl << "local x in a is " << x
<< " after entering a" << endl;
}
void b( )
{
static int x = 50; // Static initialization only
// first time b is called.
cout << endl << "local static x is " << x
<< " on entering b" << endl;
x=x+2;
}
void c( void )
{
cout << endl << "global x is " << x
<< " on entering c" << endl;
x *= 10;
}
25
50 in first
call
10 after
second call
1
25 in
secon
d call
52 in Second
call call
// Using overloaded functions
// Using overloaded functions
#include <iostream>
using namespace std;
int square( int x );
double square( double y );
int main()
{
cout << "The square of integer 7 is " << square( 7 )<<endl
<< "The square of double 7.5 is " << square( 7.5 )
<< endl;
return 0;
}
int square( int x )
{
return x * x;
}
double square( double y )
{
return y * y;
}
// Using default arguments
// Using default arguments
#include <iostream>
using namespace std;
int boxVolume( int length = 1, int width = 1, int height = 1 );
int main()
{
cout << "The default box volume is: " << boxVolume()
<<endl;
cout<< "width 1 and height 1 is: " << boxVolume( 10 )
<< endl;
cout << "width 5 and height 1 is: " << boxVolume( 10, 5 )
<< endl;
cout << "width 5 and height 2 is: " << boxVolume( 10, 5, 2 )
<< endl;
return 0;
}
// Calculate the volume of a box
int boxVolume( int length, int width, int height )
{
return length * width * height;
}
Ch7 C++

More Related Content

What's hot

Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
NUST Stuff
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
NUST Stuff
 
C++ ch2
C++ ch2C++ ch2
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
rohassanie
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
Chaand Sheikh
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
AmIt Prasad
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
rohassanie
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
Ali Aminian
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
Dheeraj Kataria
 
Pointers
PointersPointers
Pointers
sanya6900
 
C++ programming
C++ programmingC++ programming
C++ programming
viancagerone
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
Mohammed Saleh
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
sathish sak
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
Mohammed Sikander
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Pranali Chaudhari
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
SAFFI Ud Din Ahmad
 

What's hot (20)

Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
 
C++ ch2
C++ ch2C++ ch2
C++ ch2
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
 
Pointers
PointersPointers
Pointers
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 

Similar to Ch7 C++

Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
Abdul Samee
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
LokeshK66
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
Mohammad Shaker
 
functions of C++
functions of C++functions of C++
functions of C++
tarandeep_kaur
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
Dendi Riadi
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
temkin abdlkader
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
NUST Stuff
 
Pointers in c++ programming presentation
Pointers in c++ programming presentationPointers in c++ programming presentation
Pointers in c++ programming presentation
SourabhGour9
 
Learn c++ (functions) with nauman ur rehman
Learn  c++ (functions) with nauman ur rehmanLearn  c++ (functions) with nauman ur rehman
Learn c++ (functions) with nauman ur rehman
Nauman Rehman
 
w10 (1).ppt
w10 (1).pptw10 (1).ppt
w10 (1).ppt
amal68766
 
C++ Programm.pptx
C++ Programm.pptxC++ Programm.pptx
C++ Programm.pptx
Åįjâž Ali
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
WaheedAnwar20
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
Farhan Ab Rahman
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docx
MARRY7
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
Anwar Ul Haq
 
Function C++
Function C++ Function C++
Function C++
Shahzad Afridi
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
Russell Childs
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
Farhan Ab Rahman
 

Similar to Ch7 C++ (20)

Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
 
functions of C++
functions of C++functions of C++
functions of C++
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
 
Pointers in c++ programming presentation
Pointers in c++ programming presentationPointers in c++ programming presentation
Pointers in c++ programming presentation
 
Learn c++ (functions) with nauman ur rehman
Learn  c++ (functions) with nauman ur rehmanLearn  c++ (functions) with nauman ur rehman
Learn c++ (functions) with nauman ur rehman
 
w10 (1).ppt
w10 (1).pptw10 (1).ppt
w10 (1).ppt
 
C++ Programm.pptx
C++ Programm.pptxC++ Programm.pptx
C++ Programm.pptx
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docx
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
 
Function C++
Function C++ Function C++
Function C++
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 

Recently uploaded

clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
ArianaBusciglio
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
NelTorrente
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 

Recently uploaded (20)

clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 

Ch7 C++

  • 1. // Functions that take no arguments #include <iostream> using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return 0; } void function1() { cout << "function1 takes no arguments" << endl; } void function2( void ) { cout << "function2 also takes no arguments" << endl; }
  • 2.
  • 3. Example #include <iostream> using namespace std; void printStars(); int main() { printStars(); //Line 1 cout << "********** Annual ***********" << endl; //Line 2 printStars(); //Line 3 cout << "******* Spring Sale **********" << endl; //Line 4 printStars(); //Line 5 return 0; } void printStars() { cout << "******************************" << endl; cout << "******************************" << endl; }
  • 4.
  • 5. solution #include<iostream> using namespace std; void square( int m ); // function prototype //it can be int square( int ); int main() { for ( int x = 1; x <= 10; x++ ) square( x ) ; //calling statement x is actual parameter cout << endl; return 0; } // Function definition void square( int y ) // Heading y is Formal Parameter { return y * y; // The return Statement }
  • 6. #include<iostream> using namespace std; void square( int m ); // function prototype //it can be int square( int ); int main() { for ( int x = 1; x <= 10; x++ ) square( x ) ; //calling statement x is actual parameter cout << endl; return 0; } // Function definition void square( int y ) // Heading y is Formal Parameter { cout<< y * y<<endl; // The return Statement }
  • 7.
  • 9. Modify (Value parameters) #include<iostream> using namespace std; void modify( int m ); // function prototype int main() { int x; x=7; cout<<"x befor calling modify function = "<<x<<endl; modify( x ) ; //calling statement x is actual parameter cout<<"x After calling modify function = "<<x<<endl; cout << endl; return 0; } // Function definition void modify( int y ) // Heading y is Formal Parameter { y=y+10; // The return Statement cout<<"y inside modify function = "<<y<<endl; }
  • 10.
  • 11. Reference parameters #include<iostream> using namespace std; void modify( int &m ); // function prototype int main() { int x; x=7; cout<<"x befor calling modify function = "<<x<<endl; modify( x ) ; //calling statement x is actual parameter cout<<"x After calling modify function = "<<x<<endl; cout << endl; return 0; } // Function definition void modify( int &y ) // Heading y is Formal Parameter { y=y+10; // The return Statement cout<<"y inside modify function = "<<y<<endl; }
  • 12.
  • 13. Reference // References must be initialized #include <iostream> using namespace std; int main() { int x = 3, &y = x; // y is now an alias for x cout << "x = " << x << endl << "y = " << y << endl; y = 7; cout << "x = " << x << endl << "y = " << y << endl; return 0; }
  • 14.
  • 15. // A scoping example #include <iostream> using namespace std; void a( ); // function prototype void b( ); // function prototype void c( void ); // function prototype int x = 1; // global variable int main() { int x = 5; // local variable to main cout << "local x in outer scope of main is " << x << endl; { // start new scope int x = 7; cout << "local x in inner scope of main is " << x << endl; } // end new scope cout << "globel x in outer scope of main is " << ::x << endl; cout << "local x in outer scope of main is " << x << endl; cout<<"*************************************************"<<endl; a(); // a has automatic local x b(); // b has static local x c(); // c uses global x cout<<"*************************************************"<<endl; cout<<"Afetr second calling"<<endl; a(); // a reinitializes automatic local x b(); // static local x retains its previous value c(); // global x also retains its value cout <<endl<< "local x in main is " << x <<endl<< endl; return 0; } 5 7 1 5 5
  • 16. void a( ) { int x = 25; // initialized each time a is called cout << endl << "local x in a is " << x << " after entering a" << endl; } void b( ) { static int x = 50; // Static initialization only // first time b is called. cout << endl << "local static x is " << x << " on entering b" << endl; x=x+2; } void c( void ) { cout << endl << "global x is " << x << " on entering c" << endl; x *= 10; } 25 50 in first call 10 after second call 1 25 in secon d call 52 in Second call call
  • 17.
  • 18. // Using overloaded functions // Using overloaded functions #include <iostream> using namespace std; int square( int x ); double square( double y ); int main() { cout << "The square of integer 7 is " << square( 7 )<<endl << "The square of double 7.5 is " << square( 7.5 ) << endl; return 0; } int square( int x ) { return x * x; } double square( double y ) { return y * y; }
  • 19.
  • 20. // Using default arguments // Using default arguments #include <iostream> using namespace std; int boxVolume( int length = 1, int width = 1, int height = 1 ); int main() { cout << "The default box volume is: " << boxVolume() <<endl; cout<< "width 1 and height 1 is: " << boxVolume( 10 ) << endl; cout << "width 5 and height 1 is: " << boxVolume( 10, 5 ) << endl; cout << "width 5 and height 2 is: " << boxVolume( 10, 5, 2 ) << endl; return 0; } // Calculate the volume of a box int boxVolume( int length, int width, int height ) { return length * width * height; }