SlideShare a Scribd company logo
Functions
#include <iostream>
void starline(); //function declaration
int main()
{
starline(); //call to function
cout << “Data type Range” << endl;
starline(); //call to function
cout << “char -128 to 127” << endl
<< “short -32,768 to 32,767” << endl
<< “int System dependent” << endl
<< “long -2,147,483,648 to 2,147,483,647” << endl;
starline();
return 0;
}
Contd..
// function definition
void starline()
{
for(int j=0; j<45; j++) //function body
cout << ‘*’;
cout << endl;
}
Output
• The output from the program looks like this:
• ***************************************
******
• Data type Range
• ***************************************
******
• char -128 to 127
• short -32,768 to 32,767
• int System dependent
• double -2,147,483,648 to 2,147,483,647
Function Without Declaration
• #include <iostream>
• void starline()
• {
• for(int j=0; j<45; j++)
• cout << ‘*’;
• cout << endl;
• }
• int main() //main() follows function
• {
• starline(); //call to function
• cout << “Data type Range” << endl;
• starline(); //call to function
• cout << “char -128 to 127” << endl
• << “short -32,768 to 32,767” << endl
• << “int System dependent” << endl
• << “long -2,147,483,648 to 2,147,483,647” << endl;
• starline(); //call to function
• return 0;
• }
Passing Arguments
#include <iostream>
void repchar(char, int); //function declaration
int main()
{
repchar(‘-’, 43); //call to function
cout << “Data type Range” << endl;
repchar(‘=’, 23); //call to function
cout << “char -128 to 127” << endl
<< “short -32,768 to 32,767” << endl
<< “int System dependent” << endl
<< “double -2,147,483,648 to 2,147,483,647” << endl;
repchar(‘-’, 43); //call to function
return 0;
}
void repchar(char ch, int n) //function declarator
{
for(int j=0; j<n; j++) //function body
cout << ch;
cout << endl;
}
Output
• -------------------------------------------
• Data type Range
• =======================
• Char -128 to 127
• short -32,768 to 32,767
• int System dependent
• long -2,147,483,648 to 2,147,483,647
Passing Variables
• #include <iostream>
• void repchar(char, int); //function declaration
• int main()
• {
• char chin;
• int nin;
• cout << “Enter a character: ”;
• cin >> chin;
• cout << “Enter number of times to repeat it: ”;
• cin >> nin;
• repchar(chin, nin);
• return 0;
• }
Contd..
• void repchar(char ch, int n)
• {
• for(int j=0; j<n; j++) //function body
• cout << ch;
• cout << endl;
• }
Output
• Enter a character: +
• Enter number of times to repeat it: 20
• ++++++++++++++++++++
Passing Structures
• #include <iostream>
• struct Distance //English distance
• {
• int feet;
• float inches;
• };
• void engldisp( Distance ); //declaration
• int main()
• {
• Distance d1, d2; //define two lengths
• //get length d1 from user
• cout << “Enter feet: ”; cin >> d1.feet;
• cout << “Enter inches: ”; cin >> d1.inches;
• //get length d2 from user
• cout << “nEnter feet: ”; cin >> d2.feet;
• cout << “Enter inches: ”; cin >> d2.inches;
Contd..
• cout << “nd1 = ”;
• engldisp(d1); //display length 1
• cout << “nd2 = ”;
• engldisp(d2); //display length 2
• cout << endl;
• return 0;
• }
• // display structure of type Distance in feet and inches
• void engldisp( Distance dd ) //parameter dd of type
Distance
• {
• cout << dd.feet << “’-” << dd.inches << “””;
• }
Output
• Enter feet: 6
• Enter inches: 4
• Enter feet: 5
• Enter inches: 4.25
• d1 = 6’-4”
• d2 = 5’-4.25”
Returning Values From Functions
• #include <iostream>
• float lbstokg(float); //declaration
• int main()
• {
• float lbs, kgs;
• cout << “nEnter your weight in pounds: ”;
• cin >> lbs;
• kgs = lbstokg(lbs);
• cout << “Your weight in kilograms is ” << kgs << endl;
• return 0;
• }
• // converts pounds to kilograms
• float lbstokg(float pounds)
• {
• float kilograms = 0.453592 * pounds;
• return kilograms;
• }
Output
• Enter your weight in pounds: 182
• Your weight in kilograms is 82.553741
Returning Structure Variables
• #include <iostream>
• struct Distance //English distance
• {
• int feet;
• float inches;
• };
• Distance addengl (Distance, Distance); //declarations
• void engldisp (Distance);
• int main()
• {
• Distance d1, d2, d3; //define three lengths
• //get length d1 from user
• cout << “nEnter feet: ”; cin >> d1.feet;
• cout << “Enter inches: ”; cin >> d1.inches;
• //get length d2 from user
• cout << “nEnter feet: ”; cin >> d2.feet;
• cout << “Enter inches: ”; cin >> d2.inches;
• d3 = addengl(d1, d2); //d3 is sum of d1 and d2
• cout << endl;
• engldisp(d1); cout << “ + ”; //display all lengths
• engldisp(d2); cout << “ = ”;
• engldisp(d3); cout << endl;
• return 0;
• }
Contd..
• Distance addengl ( Distance dd1, Distance dd2 )
• {
• Distance dd3; //define a new structure for sum
• dd3.inches = dd1.inches + dd2.inches; //add the
inches
• dd3.feet = 0; //(for possible carry)
• if(dd3.inches >= 12.0) //if inches >= 12.0,
• { //then decrease inches
• dd3.inches -= 12.0; //by 12.0 and
• dd3.feet++; //increase feet
• } //by 1
• dd3.feet += dd1.feet + dd2.feet; //add the feet
• return dd3; //return structure
• }
Contd..
• void engldisp( Distance dd )
• {
• cout << dd.feet << “’-” << dd.inches << “””;
• }
Output
• Enter feet: 4
• Enter inches: 5.5
• Enter feet: 5
• Enter inches: 6.5
• 4’-5.5” + 5’-6.5” = 10’-0”
Passing Simple Data Types by
Reference
• #include <iostream>
• int main()
• {
• void intfrac(float, float&, float&); //declaration
• float number, intpart, fracpart; //float variables
• do {
• cout << “nEnter a real number: ”; //number from user
• cin >> number;
• intfrac(number, intpart, fracpart); //find int and frac
• cout << “Integer part is ” << intpart //print them
• << “, fraction part is ” << fracpart << endl;
• } while( number != 0.0 ); //exit loop on 0.0
• return 0;
• }
Contd..
• // intfrac()
• // finds integer and fractional parts of real
number
• void intfrac(float n, float& intp, float& fracp)
• {
• long temp = static_cast<long>(n); //convert to
long,
• intp = static_cast<float>(temp); //back to float
• fracp = n - intp; //subtract integer part
• }
Output
• Enter a real number: 99.44
• Integer part is 99, fractional part is 0.44
Passing Structures by Reference
• #include <iostream>
• struct Distance //English distance
• {
• int feet;
• float inches;
• };
• void scale( Distance&, float ); //function
• void engldisp( Distance ); //declarations
• int main()
• {
• Distance d1 = { 12, 6.5 }; //initialize d1 and d2
• Distance d2 = { 10, 5.5 };
• cout << “d1 = ”; engldisp(d1); //display old d1 and d2
• cout << “nd2 = ”; engldisp(d2);
• scale(d1, 0.5); //scale d1 and d2
• scale(d2, 0.25);
• cout << “nd1 = ”; engldisp(d1); //display new d1 and d2
• cout << “nd2 = ”; engldisp(d2);
• cout << endl;
• return 0;
• }
Contd..
• void scale( Distance& dd, float factor)
• {
• float inches = (dd.feet*12 + dd.inches) * factor;
• dd.feet = static_cast<int>(inches / 12);
• dd.inches = inches - dd.feet * 12;
• }
• void engldisp ( Distance dd ) //parameter dd of type
Distance
• {
• cout << dd.feet << “’-” << dd.inches << “””;
• }
Output
• d1 = 12’-6.5”
• d2 = 10’-5.5”
• d1 = 6’-3.25”
• d2 = 2’-7.375”
Overloaded Functions
• #include <iostream>
• using namespace std;
• void repchar(); //declarations
• void repchar(char);
• void repchar(char, int);
• int main()
• {
• repchar();
• repchar(‘=’);
• repchar(‘+’, 30);
• return 0;
• }
Contd..
• void repchar()
• {
• for(int j=0; j<45; j++) // always loops 45 times
• cout << ‘*’; // always prints asterisk
• cout << endl;
• }
• // displays 45 copies of specified character
• void repchar(char ch)
• {
• for(int j=0; j<45; j++) // always loops 45 times
• cout << ch; // prints specified character
• cout << endl;
• }
• // displays specified number of copies of specified character
• void repchar(char ch, int n)
• {
• for(int j=0; j<n; j++) // loops n times
• cout << ch; // prints specified character
• cout << endl;
• }
Output
• *************************************
********
• =====================================
========
• ++++++++++++++++++++++++++++++
Inline Function
• #include <iostream>
• // converts pounds to kilograms
• inline float lbstokg (float pounds)
• {
• return 0.453592 * pounds;
• }
• int main()
• {
• float lbs;
• cout << “nEnter your weight in pounds: ”;
• cin >> lbs;
• cout << “Your weight in kilograms is ” << lbstokg(lbs)
• << endl;
• return 0;
• }
Default Arguments
• #include <iostream>
• using namespace std;
• void repchar(char=’*’, int=45); //declaration with
• //default arguments
• int main()
• {
• repchar(); //prints 45 asterisks
• repchar(‘=’); //prints 45 equal signs
• repchar(‘+’, 30); //prints 30 plus signs
• return 0;
• }
• // repchar()
• // displays line of characters
• void repchar(char ch, int n) //defaults supplied
• { // if necessary
• for(int j=0; j<n; j++) //loops n times
• cout << ch; //prints ch
• cout << endl;
• }
Automatic Variables
• Visibilty is within block of Code
• Lifetime: An automatic variable is not created
until the function in which it is defined is
called.
External Variables
• Also called global variables
• It is initialized automatically to 0 when it is
created , if not initialized explicitly
• Lifetime: External variables exist for the life of
the program
• Visibility: Throughtout the succeeding code
Static Variables
• A static automatic variable has the visibility of
a local variable (that is, inside the function
containing it). Its lifetime is similar to that of
an external variable, except that it doesn’t
come into existence until the first call to the
function containing it.

More Related Content

Similar to Lec 2.pptx

C++ process new
C++ process newC++ process new
C++ process new敬倫 林
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays kinan keshkeh
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxDeepasCSE
 
C++ Functions.pptx
C++ Functions.pptxC++ Functions.pptx
C++ Functions.pptxDikshaDani5
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101premrings
 
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.pdfyamew16788
 
Emerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the HorizonEmerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the HorizonAlex Payne
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical fileMitul Patel
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - ReferenceMohammed Sikander
 

Similar to Lec 2.pptx (20)

CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
C++ process new
C++ process newC++ process new
C++ process new
 
C++ practical
C++ practicalC++ practical
C++ practical
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
 
programming fundamentals
programming fundamentalsprogramming fundamentals
programming fundamentals
 
Oop1
Oop1Oop1
Oop1
 
C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
 
C++ Functions.pptx
C++ Functions.pptxC++ Functions.pptx
C++ Functions.pptx
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
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++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
 
Emerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the HorizonEmerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the Horizon
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical file
 
Angular2 for Beginners
Angular2 for BeginnersAngular2 for Beginners
Angular2 for Beginners
 
C++ Programm.pptx
C++ Programm.pptxC++ Programm.pptx
C++ Programm.pptx
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
C
CC
C
 

Recently uploaded

2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edgePaco Orozco
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationDr. Radhey Shyam
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-IVigneshvaranMech
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfKamal Acharya
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdfKamal Acharya
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Krakówbim.edu.pl
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234AafreenAbuthahir2
 
Peek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdfPeek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdfAyahmorsy
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationRobbie Edward Sayers
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdfKamal Acharya
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsAtif Razi
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industriesMuhammadTufail242431
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxCenterEnamel
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Aryaabh.arya
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopEmre Günaydın
 
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringC Sai Kiran
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfKamal Acharya
 
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamKIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamDr. Radhey Shyam
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdfAhmedHussein950959
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdfKamal Acharya
 

Recently uploaded (20)

2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Peek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdfPeek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering Workshop
 
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
 
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamKIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdf
 

Lec 2.pptx

  • 1. Functions #include <iostream> void starline(); //function declaration int main() { starline(); //call to function cout << “Data type Range” << endl; starline(); //call to function cout << “char -128 to 127” << endl << “short -32,768 to 32,767” << endl << “int System dependent” << endl << “long -2,147,483,648 to 2,147,483,647” << endl; starline(); return 0; }
  • 2. Contd.. // function definition void starline() { for(int j=0; j<45; j++) //function body cout << ‘*’; cout << endl; }
  • 3. Output • The output from the program looks like this: • *************************************** ****** • Data type Range • *************************************** ****** • char -128 to 127 • short -32,768 to 32,767 • int System dependent • double -2,147,483,648 to 2,147,483,647
  • 4. Function Without Declaration • #include <iostream> • void starline() • { • for(int j=0; j<45; j++) • cout << ‘*’; • cout << endl; • } • int main() //main() follows function • { • starline(); //call to function • cout << “Data type Range” << endl; • starline(); //call to function • cout << “char -128 to 127” << endl • << “short -32,768 to 32,767” << endl • << “int System dependent” << endl • << “long -2,147,483,648 to 2,147,483,647” << endl; • starline(); //call to function • return 0; • }
  • 5. Passing Arguments #include <iostream> void repchar(char, int); //function declaration int main() { repchar(‘-’, 43); //call to function cout << “Data type Range” << endl; repchar(‘=’, 23); //call to function cout << “char -128 to 127” << endl << “short -32,768 to 32,767” << endl << “int System dependent” << endl << “double -2,147,483,648 to 2,147,483,647” << endl; repchar(‘-’, 43); //call to function return 0; } void repchar(char ch, int n) //function declarator { for(int j=0; j<n; j++) //function body cout << ch; cout << endl; }
  • 6. Output • ------------------------------------------- • Data type Range • ======================= • Char -128 to 127 • short -32,768 to 32,767 • int System dependent • long -2,147,483,648 to 2,147,483,647
  • 7. Passing Variables • #include <iostream> • void repchar(char, int); //function declaration • int main() • { • char chin; • int nin; • cout << “Enter a character: ”; • cin >> chin; • cout << “Enter number of times to repeat it: ”; • cin >> nin; • repchar(chin, nin); • return 0; • }
  • 8. Contd.. • void repchar(char ch, int n) • { • for(int j=0; j<n; j++) //function body • cout << ch; • cout << endl; • }
  • 9. Output • Enter a character: + • Enter number of times to repeat it: 20 • ++++++++++++++++++++
  • 10. Passing Structures • #include <iostream> • struct Distance //English distance • { • int feet; • float inches; • }; • void engldisp( Distance ); //declaration • int main() • { • Distance d1, d2; //define two lengths • //get length d1 from user • cout << “Enter feet: ”; cin >> d1.feet; • cout << “Enter inches: ”; cin >> d1.inches; • //get length d2 from user • cout << “nEnter feet: ”; cin >> d2.feet; • cout << “Enter inches: ”; cin >> d2.inches;
  • 11. Contd.. • cout << “nd1 = ”; • engldisp(d1); //display length 1 • cout << “nd2 = ”; • engldisp(d2); //display length 2 • cout << endl; • return 0; • } • // display structure of type Distance in feet and inches • void engldisp( Distance dd ) //parameter dd of type Distance • { • cout << dd.feet << “’-” << dd.inches << “””; • }
  • 12. Output • Enter feet: 6 • Enter inches: 4 • Enter feet: 5 • Enter inches: 4.25 • d1 = 6’-4” • d2 = 5’-4.25”
  • 13. Returning Values From Functions • #include <iostream> • float lbstokg(float); //declaration • int main() • { • float lbs, kgs; • cout << “nEnter your weight in pounds: ”; • cin >> lbs; • kgs = lbstokg(lbs); • cout << “Your weight in kilograms is ” << kgs << endl; • return 0; • } • // converts pounds to kilograms • float lbstokg(float pounds) • { • float kilograms = 0.453592 * pounds; • return kilograms; • }
  • 14. Output • Enter your weight in pounds: 182 • Your weight in kilograms is 82.553741
  • 15. Returning Structure Variables • #include <iostream> • struct Distance //English distance • { • int feet; • float inches; • }; • Distance addengl (Distance, Distance); //declarations • void engldisp (Distance); • int main() • { • Distance d1, d2, d3; //define three lengths • //get length d1 from user • cout << “nEnter feet: ”; cin >> d1.feet; • cout << “Enter inches: ”; cin >> d1.inches; • //get length d2 from user • cout << “nEnter feet: ”; cin >> d2.feet; • cout << “Enter inches: ”; cin >> d2.inches; • d3 = addengl(d1, d2); //d3 is sum of d1 and d2 • cout << endl; • engldisp(d1); cout << “ + ”; //display all lengths • engldisp(d2); cout << “ = ”; • engldisp(d3); cout << endl; • return 0; • }
  • 16. Contd.. • Distance addengl ( Distance dd1, Distance dd2 ) • { • Distance dd3; //define a new structure for sum • dd3.inches = dd1.inches + dd2.inches; //add the inches • dd3.feet = 0; //(for possible carry) • if(dd3.inches >= 12.0) //if inches >= 12.0, • { //then decrease inches • dd3.inches -= 12.0; //by 12.0 and • dd3.feet++; //increase feet • } //by 1 • dd3.feet += dd1.feet + dd2.feet; //add the feet • return dd3; //return structure • }
  • 17. Contd.. • void engldisp( Distance dd ) • { • cout << dd.feet << “’-” << dd.inches << “””; • }
  • 18. Output • Enter feet: 4 • Enter inches: 5.5 • Enter feet: 5 • Enter inches: 6.5 • 4’-5.5” + 5’-6.5” = 10’-0”
  • 19. Passing Simple Data Types by Reference • #include <iostream> • int main() • { • void intfrac(float, float&, float&); //declaration • float number, intpart, fracpart; //float variables • do { • cout << “nEnter a real number: ”; //number from user • cin >> number; • intfrac(number, intpart, fracpart); //find int and frac • cout << “Integer part is ” << intpart //print them • << “, fraction part is ” << fracpart << endl; • } while( number != 0.0 ); //exit loop on 0.0 • return 0; • }
  • 20. Contd.. • // intfrac() • // finds integer and fractional parts of real number • void intfrac(float n, float& intp, float& fracp) • { • long temp = static_cast<long>(n); //convert to long, • intp = static_cast<float>(temp); //back to float • fracp = n - intp; //subtract integer part • }
  • 21. Output • Enter a real number: 99.44 • Integer part is 99, fractional part is 0.44
  • 22. Passing Structures by Reference • #include <iostream> • struct Distance //English distance • { • int feet; • float inches; • }; • void scale( Distance&, float ); //function • void engldisp( Distance ); //declarations • int main() • { • Distance d1 = { 12, 6.5 }; //initialize d1 and d2 • Distance d2 = { 10, 5.5 }; • cout << “d1 = ”; engldisp(d1); //display old d1 and d2 • cout << “nd2 = ”; engldisp(d2); • scale(d1, 0.5); //scale d1 and d2 • scale(d2, 0.25); • cout << “nd1 = ”; engldisp(d1); //display new d1 and d2 • cout << “nd2 = ”; engldisp(d2); • cout << endl; • return 0; • }
  • 23. Contd.. • void scale( Distance& dd, float factor) • { • float inches = (dd.feet*12 + dd.inches) * factor; • dd.feet = static_cast<int>(inches / 12); • dd.inches = inches - dd.feet * 12; • } • void engldisp ( Distance dd ) //parameter dd of type Distance • { • cout << dd.feet << “’-” << dd.inches << “””; • }
  • 24. Output • d1 = 12’-6.5” • d2 = 10’-5.5” • d1 = 6’-3.25” • d2 = 2’-7.375”
  • 25. Overloaded Functions • #include <iostream> • using namespace std; • void repchar(); //declarations • void repchar(char); • void repchar(char, int); • int main() • { • repchar(); • repchar(‘=’); • repchar(‘+’, 30); • return 0; • }
  • 26. Contd.. • void repchar() • { • for(int j=0; j<45; j++) // always loops 45 times • cout << ‘*’; // always prints asterisk • cout << endl; • } • // displays 45 copies of specified character • void repchar(char ch) • { • for(int j=0; j<45; j++) // always loops 45 times • cout << ch; // prints specified character • cout << endl; • } • // displays specified number of copies of specified character • void repchar(char ch, int n) • { • for(int j=0; j<n; j++) // loops n times • cout << ch; // prints specified character • cout << endl; • }
  • 28. Inline Function • #include <iostream> • // converts pounds to kilograms • inline float lbstokg (float pounds) • { • return 0.453592 * pounds; • } • int main() • { • float lbs; • cout << “nEnter your weight in pounds: ”; • cin >> lbs; • cout << “Your weight in kilograms is ” << lbstokg(lbs) • << endl; • return 0; • }
  • 29. Default Arguments • #include <iostream> • using namespace std; • void repchar(char=’*’, int=45); //declaration with • //default arguments • int main() • { • repchar(); //prints 45 asterisks • repchar(‘=’); //prints 45 equal signs • repchar(‘+’, 30); //prints 30 plus signs • return 0; • } • // repchar() • // displays line of characters • void repchar(char ch, int n) //defaults supplied • { // if necessary • for(int j=0; j<n; j++) //loops n times • cout << ch; //prints ch • cout << endl; • }
  • 30. Automatic Variables • Visibilty is within block of Code • Lifetime: An automatic variable is not created until the function in which it is defined is called.
  • 31. External Variables • Also called global variables • It is initialized automatically to 0 when it is created , if not initialized explicitly • Lifetime: External variables exist for the life of the program • Visibility: Throughtout the succeeding code
  • 32. Static Variables • A static automatic variable has the visibility of a local variable (that is, inside the function containing it). Its lifetime is similar to that of an external variable, except that it doesn’t come into existence until the first call to the function containing it.