SlideShare a Scribd company logo
3. Functions in C++
Introduction
 Using functions we can structure our
programs in a more modular way, accessing
all the potential that structured programming
can offer to us in C++
 A function is a group of statements that is
executed when it is called from some point of
the program.
Functions
 In C++, functions are characterized by a name, a
return type and by a list of arguments (optional)
 The argument list must always be present (even in
the case it is empty)
 The return value must be compatible (maybe by
means of an explicit cast) with the function type
return value
double max( double a, double b)
{
return (a>b) ? a : b;
}
return type arguments
function body
Function prototypes
 A function must be declared before you can use it (in the file
where it is used)
 Function prototypes make C++ functions “type safe”, the
compiler can always convert the real values of the
arguments to the types formally specified in the prototype
#include <iostream>
double max(double, double);
int main()
{
double m = max(1, 3);
cout<<“the maximum is “<<m<<endl;
return 0;
}
main.cpp
double max (double a, double b)
{
return (a>b) ? a : b;
}
max.cpp
max prototype
(usually in max.h)
Exercise
 Write a C++ program that will ask for a temperature in
Fahrenheit and display in Celsius.
#include<iostream.h>
float ftoc(float f){
float c;
c = float((f-32.0) * 5/9);
return c;
}
Exercise
int main(){
float farTemp,celTemp;
float ftoc(float farTemp);
cout<<"Enter the temperature in Fahrenheit : ";
cin>>farTemp;
cout<<"n";
celTemp = ftoc(farTemp);
cout<<"Equivalent in Celcius is: ";
cout << celTemp<<"nn";
return 0;
}
Call by Reference
 Use of references allows a function to modify the value of its
arguments
 The reference can be defined const so that the function can
not modify that argument
bool greater(int& i, int& j) {
if (i>j) {
int temp=i;
i=j;
j=temp;
return true;
}
else
return false;
}
Arguments passed “by reference”
The function can modify its
arguments
Default Arguments
 A default value can be assigned to all arguments of a
function. The function can then be called by omitting those
parameter for which a default value has been specified
 A default value can only be specified for the rightmost
parameters in the calling sequence
int pow(int , int);
int main()
{
int r=3;
int a1=pow(3,3); // a1=27
int a2=pow(3); // a2=9
return 0;
}
main.cpp
int pow (int a, int k=2)
{
if (k==2) return a*a;
else return a*pow(a, k-1);
}
pow.cpp
Default argument
#include <iostream>
using namespace std;
int AreaCube(int length, int width = 25, int height = 1);
int main()
{
int length = 100;
int width = 50;
int height = 2;
int area;
area = AreaCube(length, width, height);
cout << "First area equals: " << area << "n";
area = AreaCube(length, width);
cout << "Second time area equals: " << area << "n";
area = AreaCube(length);
cout << "Third time area equals: " << area << "n";
return 0;
}
int AreaCube(int length, int width, int height)
{
return (length * width * height);
}
inline functions
 The inline keyword is used to suggest the compiler
that all calls to that function must be replaced with
executable code (all calls are replaced with the
function’s definition, everywhere in the code)
 inline functions are used for efficiency reasons and
must be kept simple (not to get the compiler
overworked)
 The compiler will decide autonomously (for
instance if the function is too big) whether to
ignore the inline directive or not
#include <iostream>
using namespace std;
inline int Double(int);
int main()
{
int target;
cout << "Enter a number to work with: ";
cin >> target; 13: cout << "n";
target = Double(target);
cout << "Target: " << target << endl;
target = Double(target);
cout << "Target: " << target << endl;
target = Double(target);
cout << "Target: " << target << endl;
return 0;
}
int Double(int target)
{
return 2*target;
}
Overloading
 Different functions can carry the same name
 The function being called is then chosen by the compiler on
the basis of the return type and on the argument number
and type
double average_array(const int a[], int size)
{
int sum=0;
for (int i=0;i<size;i++) sum+=a[i];
return double(sum)/size;
}
double average_array(const double a[], int size)
{
double sum=0;
for (int i=0;i<size;i++) sum+=a[i];
return sum/size;
}
average_array.cpp
Overloading
 The list of arguments and their types is called the function’s
signature
 The return type is not part of a function’s signature, the number
and the ordering of the arguments is on the other hand crucial
void print(int i=0) {. . .} // (1)
void print(int i, double x) {. . .} // (2)
void print(double y, int i) {. . .} // (3)
. . .
print(‘A’); // ‘A’ is converted to int, (1) is called
print(str[]); // error! No conversion is possible
print(15,9); // error! Ambiguity between (2) and (3)
print(15,9.); // OK, (2) is called
print(); // OK, (1) is called (default argument)
The main() arguments
 By specifying a list of arguments for main() it is possible to have
access to all parameters passed on the command line:
 argc is the # of parameters passed on the command line (at least
1, the program name) argv contains all single parameters
#include <iostream.h>
int main(int argc, char *argv[])
{
cout<<“ argc value: “<<argc<<endl;
cout<<“ the executable name is “<<*argv<<endl;
for (int i=1; i<argc; i++)
cout<<“Arg #”<<i<<“ = “<<*(argv+i)<<endl;
return 0;
}
The main() arguments (2)
 If you run this program with the following
command
prompt> mytest this is a test
 ..you’ll get
argc value : 5
the executable name is e` mytest
Arg #1 = this
Arg #2 = is
Arg #3 = a
Arg #4 = test

More Related Content

Similar to Inbuilt Functions in C++ computer language.ppt (20)

Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
DeepasCSE
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
Amrit Kaur
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
Venkateswarlu Vuggam
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
Jasleen Kaur (Chandigarh University)
 
Chapter 1 (2) array and structure r.pptx
Chapter 1 (2) array and structure r.pptxChapter 1 (2) array and structure r.pptx
Chapter 1 (2) array and structure r.pptx
abenezertekalign118
 
functions in c++ basic concept for students
functions in c++ basic concept for studentsfunctions in c++ basic concept for students
functions in c++ basic concept for students
soniasharmafdp
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
Venkateswarlu Vuggam
 
Lecture05
Lecture05Lecture05
Lecture05
elearning_portal
 
Functions in c++,
Functions in c++,Functions in c++,
Functions in c++,
Padma Kannan
 
C++ functions
C++ functionsC++ functions
C++ functions
Mayank Jain
 
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptx
rebin5725
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
C++ Functions.pptx
C++ Functions.pptxC++ Functions.pptx
C++ Functions.pptx
DikshaDani5
 
Oop1
Oop1Oop1
Oop1
Vaibhav Bajaj
 
6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming
Ahmad177077
 
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
 
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
edukuldeep2005
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Nikhil Pandit
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
DeepasCSE
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Chapter 1 (2) array and structure r.pptx
Chapter 1 (2) array and structure r.pptxChapter 1 (2) array and structure r.pptx
Chapter 1 (2) array and structure r.pptx
abenezertekalign118
 
functions in c++ basic concept for students
functions in c++ basic concept for studentsfunctions in c++ basic concept for students
functions in c++ basic concept for students
soniasharmafdp
 
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptx
rebin5725
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
C++ Functions.pptx
C++ Functions.pptxC++ Functions.pptx
C++ Functions.pptx
DikshaDani5
 
6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming
Ahmad177077
 
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
 
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
edukuldeep2005
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Nikhil Pandit
 

Recently uploaded (20)

Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning ModelEnhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
IRJET Journal
 
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Mohamed905031
 
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
gerogepatton
 
Utilizing Biomedical Waste for Sustainable Brick Manufacturing: A Novel Appro...
Utilizing Biomedical Waste for Sustainable Brick Manufacturing: A Novel Appro...Utilizing Biomedical Waste for Sustainable Brick Manufacturing: A Novel Appro...
Utilizing Biomedical Waste for Sustainable Brick Manufacturing: A Novel Appro...
IRJET Journal
 
Influence line diagram in a robust model
Influence line diagram in a robust modelInfluence line diagram in a robust model
Influence line diagram in a robust model
ParthaSengupta26
 
Forensic Science – Digital Forensics – Digital Evidence – The Digital Forensi...
Forensic Science – Digital Forensics – Digital Evidence – The Digital Forensi...Forensic Science – Digital Forensics – Digital Evidence – The Digital Forensi...
Forensic Science – Digital Forensics – Digital Evidence – The Digital Forensi...
ManiMaran230751
 
Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos12
 
Axial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Axial Capacity Estimation of FRP-strengthened Corroded Concrete ColumnsAxial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Axial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Journal of Soft Computing in Civil Engineering
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos10
 
HVAC Air Filter Equipment-Catalouge-Final.pdf
HVAC Air Filter Equipment-Catalouge-Final.pdfHVAC Air Filter Equipment-Catalouge-Final.pdf
HVAC Air Filter Equipment-Catalouge-Final.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
MODULE 4 BUILDING PLANNING AND DESIGN SY BTECH HVAC SYSTEM IN BUILDING
MODULE 4 BUILDING PLANNING AND DESIGN SY BTECH HVAC SYSTEM IN BUILDINGMODULE 4 BUILDING PLANNING AND DESIGN SY BTECH HVAC SYSTEM IN BUILDING
MODULE 4 BUILDING PLANNING AND DESIGN SY BTECH HVAC SYSTEM IN BUILDING
Dr. BASWESHWAR JIRWANKAR
 
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her IndustryTesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
Software_Engineering_in_6_Hours_lyst1728638742594.pdf
Software_Engineering_in_6_Hours_lyst1728638742594.pdfSoftware_Engineering_in_6_Hours_lyst1728638742594.pdf
Software_Engineering_in_6_Hours_lyst1728638742594.pdf
VanshMunjal7
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)
[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)
[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)
하이플럭스 / HIFLUX Co., Ltd.
 
ENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdfENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdf
TAMILISAI R
 
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdfISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning ModelEnhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
IRJET Journal
 
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Mohamed905031
 
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
gerogepatton
 
Utilizing Biomedical Waste for Sustainable Brick Manufacturing: A Novel Appro...
Utilizing Biomedical Waste for Sustainable Brick Manufacturing: A Novel Appro...Utilizing Biomedical Waste for Sustainable Brick Manufacturing: A Novel Appro...
Utilizing Biomedical Waste for Sustainable Brick Manufacturing: A Novel Appro...
IRJET Journal
 
Influence line diagram in a robust model
Influence line diagram in a robust modelInfluence line diagram in a robust model
Influence line diagram in a robust model
ParthaSengupta26
 
Forensic Science – Digital Forensics – Digital Evidence – The Digital Forensi...
Forensic Science – Digital Forensics – Digital Evidence – The Digital Forensi...Forensic Science – Digital Forensics – Digital Evidence – The Digital Forensi...
Forensic Science – Digital Forensics – Digital Evidence – The Digital Forensi...
ManiMaran230751
 
Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos12
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos10
 
MODULE 4 BUILDING PLANNING AND DESIGN SY BTECH HVAC SYSTEM IN BUILDING
MODULE 4 BUILDING PLANNING AND DESIGN SY BTECH HVAC SYSTEM IN BUILDINGMODULE 4 BUILDING PLANNING AND DESIGN SY BTECH HVAC SYSTEM IN BUILDING
MODULE 4 BUILDING PLANNING AND DESIGN SY BTECH HVAC SYSTEM IN BUILDING
Dr. BASWESHWAR JIRWANKAR
 
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her IndustryTesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
Software_Engineering_in_6_Hours_lyst1728638742594.pdf
Software_Engineering_in_6_Hours_lyst1728638742594.pdfSoftware_Engineering_in_6_Hours_lyst1728638742594.pdf
Software_Engineering_in_6_Hours_lyst1728638742594.pdf
VanshMunjal7
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
ENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdfENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdf
TAMILISAI R
 
Ad

Inbuilt Functions in C++ computer language.ppt

  • 2. Introduction  Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us in C++  A function is a group of statements that is executed when it is called from some point of the program.
  • 3. Functions  In C++, functions are characterized by a name, a return type and by a list of arguments (optional)  The argument list must always be present (even in the case it is empty)  The return value must be compatible (maybe by means of an explicit cast) with the function type return value double max( double a, double b) { return (a>b) ? a : b; } return type arguments function body
  • 4. Function prototypes  A function must be declared before you can use it (in the file where it is used)  Function prototypes make C++ functions “type safe”, the compiler can always convert the real values of the arguments to the types formally specified in the prototype #include <iostream> double max(double, double); int main() { double m = max(1, 3); cout<<“the maximum is “<<m<<endl; return 0; } main.cpp double max (double a, double b) { return (a>b) ? a : b; } max.cpp max prototype (usually in max.h)
  • 5. Exercise  Write a C++ program that will ask for a temperature in Fahrenheit and display in Celsius. #include<iostream.h> float ftoc(float f){ float c; c = float((f-32.0) * 5/9); return c; }
  • 6. Exercise int main(){ float farTemp,celTemp; float ftoc(float farTemp); cout<<"Enter the temperature in Fahrenheit : "; cin>>farTemp; cout<<"n"; celTemp = ftoc(farTemp); cout<<"Equivalent in Celcius is: "; cout << celTemp<<"nn"; return 0; }
  • 7. Call by Reference  Use of references allows a function to modify the value of its arguments  The reference can be defined const so that the function can not modify that argument bool greater(int& i, int& j) { if (i>j) { int temp=i; i=j; j=temp; return true; } else return false; } Arguments passed “by reference” The function can modify its arguments
  • 8. Default Arguments  A default value can be assigned to all arguments of a function. The function can then be called by omitting those parameter for which a default value has been specified  A default value can only be specified for the rightmost parameters in the calling sequence int pow(int , int); int main() { int r=3; int a1=pow(3,3); // a1=27 int a2=pow(3); // a2=9 return 0; } main.cpp int pow (int a, int k=2) { if (k==2) return a*a; else return a*pow(a, k-1); } pow.cpp Default argument
  • 9. #include <iostream> using namespace std; int AreaCube(int length, int width = 25, int height = 1); int main() { int length = 100; int width = 50; int height = 2; int area; area = AreaCube(length, width, height); cout << "First area equals: " << area << "n"; area = AreaCube(length, width); cout << "Second time area equals: " << area << "n"; area = AreaCube(length); cout << "Third time area equals: " << area << "n"; return 0; } int AreaCube(int length, int width, int height) { return (length * width * height); }
  • 10. inline functions  The inline keyword is used to suggest the compiler that all calls to that function must be replaced with executable code (all calls are replaced with the function’s definition, everywhere in the code)  inline functions are used for efficiency reasons and must be kept simple (not to get the compiler overworked)  The compiler will decide autonomously (for instance if the function is too big) whether to ignore the inline directive or not
  • 11. #include <iostream> using namespace std; inline int Double(int); int main() { int target; cout << "Enter a number to work with: "; cin >> target; 13: cout << "n"; target = Double(target); cout << "Target: " << target << endl; target = Double(target); cout << "Target: " << target << endl; target = Double(target); cout << "Target: " << target << endl; return 0; } int Double(int target) { return 2*target; }
  • 12. Overloading  Different functions can carry the same name  The function being called is then chosen by the compiler on the basis of the return type and on the argument number and type double average_array(const int a[], int size) { int sum=0; for (int i=0;i<size;i++) sum+=a[i]; return double(sum)/size; } double average_array(const double a[], int size) { double sum=0; for (int i=0;i<size;i++) sum+=a[i]; return sum/size; } average_array.cpp
  • 13. Overloading  The list of arguments and their types is called the function’s signature  The return type is not part of a function’s signature, the number and the ordering of the arguments is on the other hand crucial void print(int i=0) {. . .} // (1) void print(int i, double x) {. . .} // (2) void print(double y, int i) {. . .} // (3) . . . print(‘A’); // ‘A’ is converted to int, (1) is called print(str[]); // error! No conversion is possible print(15,9); // error! Ambiguity between (2) and (3) print(15,9.); // OK, (2) is called print(); // OK, (1) is called (default argument)
  • 14. The main() arguments  By specifying a list of arguments for main() it is possible to have access to all parameters passed on the command line:  argc is the # of parameters passed on the command line (at least 1, the program name) argv contains all single parameters #include <iostream.h> int main(int argc, char *argv[]) { cout<<“ argc value: “<<argc<<endl; cout<<“ the executable name is “<<*argv<<endl; for (int i=1; i<argc; i++) cout<<“Arg #”<<i<<“ = “<<*(argv+i)<<endl; return 0; }
  • 15. The main() arguments (2)  If you run this program with the following command prompt> mytest this is a test  ..you’ll get argc value : 5 the executable name is e` mytest Arg #1 = this Arg #2 = is Arg #3 = a Arg #4 = test