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

PPTX
UNIT3.pptx
PPTX
Silde of the cse fundamentals a deep analysis
PPTX
Chp8_C++_Functions_Part2_User-defined functions.pptx
PPTX
Classes function overloading
DOC
Functions
PPTX
Fundamental of programming Fundamental of programming
PPTX
C++ lecture 03
PPTX
Function in c program
UNIT3.pptx
Silde of the cse fundamentals a deep analysis
Chp8_C++_Functions_Part2_User-defined functions.pptx
Classes function overloading
Functions
Fundamental of programming Fundamental of programming
C++ lecture 03
Function in c program

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

PPT
Fp201 unit5 1
PDF
PPTX
C function
PPTX
C++ FUNCTIONS-1.pptx
PPT
PPTX
Reference Parameter, Passing object by reference, constant parameter & Defaul...
PPTX
Fundamentals of functions in C program.pptx
PPTX
C Programming Language Part 7
PPTX
Functionincprogram
PPTX
Computer-programming-User-defined-function-1.pptx
PPT
Lecture 4
PPTX
Dti2143 chapter 5
PPTX
Detailed concept of function in c programming
PPTX
unit_2 (1).pptx
PPTX
unit_2.pptx
PDF
function in in thi pdf you will learn what is fu...
PDF
Unit 4.pdf
PPTX
Function in C program
DOCX
PDF
Cpp functions
Fp201 unit5 1
C function
C++ FUNCTIONS-1.pptx
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Fundamentals of functions in C program.pptx
C Programming Language Part 7
Functionincprogram
Computer-programming-User-defined-function-1.pptx
Lecture 4
Dti2143 chapter 5
Detailed concept of function in c programming
unit_2 (1).pptx
unit_2.pptx
function in in thi pdf you will learn what is fu...
Unit 4.pdf
Function in C program
Cpp functions
Ad

Recently uploaded (20)

PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPT
Project quality management in manufacturing
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
composite construction of structures.pdf
PDF
Digital Logic Computer Design lecture notes
PPTX
OOP with Java - Java Introduction (Basics)
PPT
Mechanical Engineering MATERIALS Selection
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Foundation to blockchain - A guide to Blockchain Tech
Project quality management in manufacturing
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Internet of Things (IOT) - A guide to understanding
Embodied AI: Ushering in the Next Era of Intelligent Systems
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
composite construction of structures.pdf
Digital Logic Computer Design lecture notes
OOP with Java - Java Introduction (Basics)
Mechanical Engineering MATERIALS Selection
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
CH1 Production IntroductoryConcepts.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
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