SlideShare a Scribd company logo
1 of 32
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

UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 

Recently uploaded (20)

UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 

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.