SlideShare une entreprise Scribd logo
1  sur  12
Télécharger pour lire hors ligne
1
Introduction to programming in C++ for engineers: 13. Functions I
Programming in C++Programming in C++
13. Functions I13. Functions I
! Introduction
! Defining a function
! Calling a function
! Return type
! Function prototypes
! Functions cannot declare functions!!!
! Unused arguments
! Default arguments
! Ignoring the return value
! Summary
2
Introduction to programming in C++ for engineers: 13. Functions I
IntroductionIntroduction
Although control structures enable us to write very powerful programs they
do not give us a means of controlling this power nor do they encourage
reusable code.
The introduction of functions is an essential first step in the writing of
modular, maintanable and reusable code.
In particular testing can be carried out on each function in isolation, rather
than on the whole program.
This is important as the number of different pathways through even a
modest program is often very large indeed.
Functions provide a way of encapsulating relatively self-contained
segments of code.
A function typically carries out some well-defined action.
3
Introduction to programming in C++ for engineers: 13. Functions I
Defining a functionDefining a function
In general a function definition takes the form:
return_type
function_name(type argument, ... , type argument)
{
// function body
}
factorial function is a very instructive example.
int
factorial(int n) // calculates n!
{
int result = 1;
do
result *= n--;
while (n>1);
return result;
}
4
Introduction to programming in C++ for engineers: 13. Functions I
Defining a functionDefining a function
int _____ return type
factorial(int n) _____ argument type and formal argument
{
// code implementing the function
return result _____ value returned
} _____ no semi-colon
There is no terminating column in function!!!
The first line sometimes known as the function header declares that the
function returns type int and defines the name of the function to be
factorial, which is the identifier used to invoke this particular function.
The identifier n is known as formal argument. A function can have any
number of formal arguments separated by commas.
The set of braces { } contains what is called as a function body. This body
contains a return statement which terminates the function.
5
Introduction to programming in C++ for engineers: 13. Functions I
Calling a functionCalling a function
A function is called (invoked or executed) by including its name together
with appropriate arguments within parenthesis.
These arguments are called actual arguments.
The identifiers used in both cases may differ since the actual arguments are
copied to the formal arguments.
The scope of the the formal arguments is limited to the function body and
changes made to these arguments are not propagated back to the calling
program.
This process of copying the value of the actual argument to the formal is
known as pass by value.
In C++ function can also use pass by reference, but pass by value is the
usual technique.
Pass by value encourages more modular, safer code and help to prevent
unexpected changes in the values of variables.
6
Introduction to programming in C++ for engineers: 13. Functions I
Return typeReturn type
The return statement causes a function to terminate but in many cases
this statement also causes a value to be passed back to the calling
environment.
The type returned by a function must agree with that specified in the
function header.
There can be no requirement for a function to return a value in which a
special type called void should be used to specify the return type.
void print_text(void)
{
cout << “Hello!!!n”;
return; // This statement can be omitted!!!
}
If no function return type is specified then it is taken to be int.
It is a good idea to specify return type.
Even if a function takes no arguments the calling program must use ().
7
Introduction to programming in C++ for engineers: 13. Functions I
Function prototypesFunction prototypes
The function can be declared without specifying how it is implemented.
int factorial(int n);
Such a statement is called function prototype. Unlike a function definition a
function prototype must end with a semi-colon and has no function body.
A function with an empty body is not a prototype!!!
Any argument names given in the prototype are ignored by the compiler,
but can be useful documentation technique.
The code implementing a function may be compiled independently to the
code using it.
Such prototypes are often collected together in a header file and since a
function is only known to the compiler after it has been declared.
In C++ prototypes have a fundamental role to play in defining classes and
therefore objects.
8
Introduction to programming in C++ for engineers: 13. Functions I
Functions cannot declare functions!!!Functions cannot declare functions!!!
Declaring functions within other functions is not available in C++.
Since every program must have a function called main() and a function
cannot be defined inside another function the general structure of a program
is a list of function definitions.
The relationship between the function look like a tree.
For some problems functions are a very effective way of controlling
complexity.
In really complicated problems related functions can be encapsulated.
We do not start from this feature because:
- there is no point in attacking a small problem with quite
complicated technique,
- classes are partly built from functions - we need to know them quite
good before introducing object-oriented techniques.
9
Introduction to programming in C++ for engineers: 13. Functions I
Unused argumentsUnused arguments
There is no requirement for all of the formal arguments in a function
declaration to be used in which case an identifier does not have to be
specified, but the function cannot be called without an argument, even
though the argument is not used.
void print(int)
{
cout << “Testn”;
}
print(1); // OK
print(); // WRONG! Incorrect number of param.
Unused arguments may serve to reserve a place in the argument list for
future use.
An argument type without identifier can also be useful when an argument
has been made redundant by a changing function implementation, but the
calling has not been updated
10
Introduction to programming in C++ for engineers: 13. Functions I
Default argumentsDefault arguments
A function declaration can specify expressions which are to be used as the
default values for one or more arguments.
double rectangle(double a = 1.0, double b = 2.0)
{ return a *b }
// three possible function calls
area1 = rectangle; // 1*2=2 is assigned
area2 = rectangle(3.0); // 3*2=6 is assigned
area3 = rectangle(4.0, 5.0) // 4*5=20 is assigned
The default arguments must be supplied right to left.
A default argument cannot be redefined in a subsequent declaration.
It is not allowed to give the same default arguments as in a function
definition (in a prototype). However a subsequent declaration can introduce
one or more additional default arguments.
Default arguments must be provided for any arguments omitted in a
function call.
11
Introduction to programming in C++ for engineers: 13. Functions I
Ignoring the return valueIgnoring the return value
The default arguments do not need to be named in a function prototype,
however omitting argument names is not a good idea since they are a
valuable method of self-documenting code.
There is also the distinction between prototypes and definitions: names
cannot be omitted from function definitions.
Ignoring the return value
The calling function can ignore the value returned by a function.
int status = mkdir(“test”); // create directory test
if (status)
cout < “Failed to create directory.n”;
else
cout << “Directory created.n”;
If we want to ignore the risk that the directory creation can fail we can
simply write:
mkdir(“test);
12
Introduction to programming in C++ for engineers: 13. Functions I
SummarySummary
! A function definition implements the function.
! Functions are executed (called or invoked) by statements like:
x = f(m); or f(m);
! If function is declared to return a value it must do so.
! The order of evaluation of expressions in argument lists is not defined.
x = f(++i, (i+5)); // compiler dependent
! A function prototype defines the function interface.
! A function cannot be declared inside another function.
! Functions can have default arguments which are supplied from right to
left.

Contenu connexe

Tendances

C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREC & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREjatin batra
 
difference between c c++ c#
difference between c c++ c#difference between c c++ c#
difference between c c++ c#Sireesh K
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing TechniquesAppili Vamsi Krishna
 
Structure of C program
Structure of C programStructure of C program
Structure of C programPavan prasad
 
Ch07 Programming for Security Professionals
Ch07 Programming for Security ProfessionalsCh07 Programming for Security Professionals
Ch07 Programming for Security Professionalsphanleson
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C ProgrammingMOHAMAD NOH AHMAD
 
Discussing Fundamentals of C
Discussing Fundamentals of CDiscussing Fundamentals of C
Discussing Fundamentals of Ceducationfront
 
Steps for c program execution
Steps for c program executionSteps for c program execution
Steps for c program executionRumman Ansari
 
First draft programming c++
First draft programming c++First draft programming c++
First draft programming c++藝輝 王
 
C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1Rumman Ansari
 
Embedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerEmbedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerGaurav Verma
 
C programming interview questions
C programming interview questionsC programming interview questions
C programming interview questionsadarshynl
 

Tendances (20)

C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREC & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
 
C_Programming_Notes_ICE
C_Programming_Notes_ICEC_Programming_Notes_ICE
C_Programming_Notes_ICE
 
difference between c c++ c#
difference between c c++ c#difference between c c++ c#
difference between c c++ c#
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
 
Structure of C program
Structure of C programStructure of C program
Structure of C program
 
Ch07 Programming for Security Professionals
Ch07 Programming for Security ProfessionalsCh07 Programming for Security Professionals
Ch07 Programming for Security Professionals
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
C programming
C programmingC programming
C programming
 
Discussing Fundamentals of C
Discussing Fundamentals of CDiscussing Fundamentals of C
Discussing Fundamentals of C
 
Deep C
Deep CDeep C
Deep C
 
C PROGRAMMING
C PROGRAMMINGC PROGRAMMING
C PROGRAMMING
 
Steps for c program execution
Steps for c program executionSteps for c program execution
Steps for c program execution
 
First draft programming c++
First draft programming c++First draft programming c++
First draft programming c++
 
C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1
 
C Language
C LanguageC Language
C Language
 
Embedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerEmbedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontroller
 
C vs c++
C vs c++C vs c++
C vs c++
 
Lesson 1 introduction to programming
Lesson 1 introduction to programmingLesson 1 introduction to programming
Lesson 1 introduction to programming
 
C programming interview questions
C programming interview questionsC programming interview questions
C programming interview questions
 

En vedette

Acb presentation
Acb presentationAcb presentation
Acb presentationdean129
 
Netw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire classNetw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire classEugenioBrown1
 
Functions c++ مشروع
Functions c++ مشروعFunctions c++ مشروع
Functions c++ مشروعziadalmulla
 
Network-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in PersianNetwork-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in PersianMuhibullah Aman
 
Network Security in 2016
Network Security in 2016Network Security in 2016
Network Security in 2016Qrator Labs
 
Functions in c++
Functions in c++Functions in c++
Functions in c++Maaz Hasan
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3Ali Aminian
 
Basic openCV Functions Using CPP
Basic openCV Functions Using CPPBasic openCV Functions Using CPP
Basic openCV Functions Using CPPWei-Wen Hsu
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You ExposedWeb Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You ExposedImperva
 
C++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer CentreC++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer Centrejatin batra
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 
solution-manual-3rd-ed-metal-forming-mechanics-and-metallurgy-chapter-1-3
 solution-manual-3rd-ed-metal-forming-mechanics-and-metallurgy-chapter-1-3 solution-manual-3rd-ed-metal-forming-mechanics-and-metallurgy-chapter-1-3
solution-manual-3rd-ed-metal-forming-mechanics-and-metallurgy-chapter-1-3dean129
 

En vedette (20)

Acb presentation
Acb presentationAcb presentation
Acb presentation
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
 
Netw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire classNetw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire class
 
Functions c++ مشروع
Functions c++ مشروعFunctions c++ مشروع
Functions c++ مشروع
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Network-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in PersianNetwork-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in Persian
 
Network Security in 2016
Network Security in 2016Network Security in 2016
Network Security in 2016
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
The Algebra of Functions
The Algebra of FunctionsThe Algebra of Functions
The Algebra of Functions
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
Basic openCV Functions Using CPP
Basic openCV Functions Using CPPBasic openCV Functions Using CPP
Basic openCV Functions Using CPP
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You ExposedWeb Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
C++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer CentreC++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer Centre
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Network Security Fundamentals
Network Security FundamentalsNetwork Security Fundamentals
Network Security Fundamentals
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
solution-manual-3rd-ed-metal-forming-mechanics-and-metallurgy-chapter-1-3
 solution-manual-3rd-ed-metal-forming-mechanics-and-metallurgy-chapter-1-3 solution-manual-3rd-ed-metal-forming-mechanics-and-metallurgy-chapter-1-3
solution-manual-3rd-ed-metal-forming-mechanics-and-metallurgy-chapter-1-3
 

Similaire à C++

Functional Paradigm.pptx
Functional Paradigm.pptxFunctional Paradigm.pptx
Functional Paradigm.pptxFurretMaster
 
Presentation1.pptx
Presentation1.pptxPresentation1.pptx
Presentation1.pptxAFANJIPHILL
 
Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...Sar
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxSRamadossbiher
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxSRamadossbiher
 
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFaisal Shehzad
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAMAN ANAND
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c languageTanmay Modi
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1YOGESH SINGH
 

Similaire à C++ (20)

Function
FunctionFunction
Function
 
Functional Paradigm.pptx
Functional Paradigm.pptxFunctional Paradigm.pptx
Functional Paradigm.pptx
 
Presentation1.pptx
Presentation1.pptxPresentation1.pptx
Presentation1.pptx
 
Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...Functions in c++, presentation, short and sweet presentation, and details of ...
Functions in c++, presentation, short and sweet presentation, and details of ...
 
Presentation 2.pptx
Presentation 2.pptxPresentation 2.pptx
Presentation 2.pptx
 
Inline function
Inline functionInline function
Inline function
 
Ch9 Functions
Ch9 FunctionsCh9 Functions
Ch9 Functions
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptx
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptx
 
C FUNCTIONS
C FUNCTIONSC FUNCTIONS
C FUNCTIONS
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c language
 
Function in c
Function in cFunction in c
Function in c
 
inline function
inline functioninline function
inline function
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 

C++

  • 1. 1 Introduction to programming in C++ for engineers: 13. Functions I Programming in C++Programming in C++ 13. Functions I13. Functions I ! Introduction ! Defining a function ! Calling a function ! Return type ! Function prototypes ! Functions cannot declare functions!!! ! Unused arguments ! Default arguments ! Ignoring the return value ! Summary
  • 2. 2 Introduction to programming in C++ for engineers: 13. Functions I IntroductionIntroduction Although control structures enable us to write very powerful programs they do not give us a means of controlling this power nor do they encourage reusable code. The introduction of functions is an essential first step in the writing of modular, maintanable and reusable code. In particular testing can be carried out on each function in isolation, rather than on the whole program. This is important as the number of different pathways through even a modest program is often very large indeed. Functions provide a way of encapsulating relatively self-contained segments of code. A function typically carries out some well-defined action.
  • 3. 3 Introduction to programming in C++ for engineers: 13. Functions I Defining a functionDefining a function In general a function definition takes the form: return_type function_name(type argument, ... , type argument) { // function body } factorial function is a very instructive example. int factorial(int n) // calculates n! { int result = 1; do result *= n--; while (n>1); return result; }
  • 4. 4 Introduction to programming in C++ for engineers: 13. Functions I Defining a functionDefining a function int _____ return type factorial(int n) _____ argument type and formal argument { // code implementing the function return result _____ value returned } _____ no semi-colon There is no terminating column in function!!! The first line sometimes known as the function header declares that the function returns type int and defines the name of the function to be factorial, which is the identifier used to invoke this particular function. The identifier n is known as formal argument. A function can have any number of formal arguments separated by commas. The set of braces { } contains what is called as a function body. This body contains a return statement which terminates the function.
  • 5. 5 Introduction to programming in C++ for engineers: 13. Functions I Calling a functionCalling a function A function is called (invoked or executed) by including its name together with appropriate arguments within parenthesis. These arguments are called actual arguments. The identifiers used in both cases may differ since the actual arguments are copied to the formal arguments. The scope of the the formal arguments is limited to the function body and changes made to these arguments are not propagated back to the calling program. This process of copying the value of the actual argument to the formal is known as pass by value. In C++ function can also use pass by reference, but pass by value is the usual technique. Pass by value encourages more modular, safer code and help to prevent unexpected changes in the values of variables.
  • 6. 6 Introduction to programming in C++ for engineers: 13. Functions I Return typeReturn type The return statement causes a function to terminate but in many cases this statement also causes a value to be passed back to the calling environment. The type returned by a function must agree with that specified in the function header. There can be no requirement for a function to return a value in which a special type called void should be used to specify the return type. void print_text(void) { cout << “Hello!!!n”; return; // This statement can be omitted!!! } If no function return type is specified then it is taken to be int. It is a good idea to specify return type. Even if a function takes no arguments the calling program must use ().
  • 7. 7 Introduction to programming in C++ for engineers: 13. Functions I Function prototypesFunction prototypes The function can be declared without specifying how it is implemented. int factorial(int n); Such a statement is called function prototype. Unlike a function definition a function prototype must end with a semi-colon and has no function body. A function with an empty body is not a prototype!!! Any argument names given in the prototype are ignored by the compiler, but can be useful documentation technique. The code implementing a function may be compiled independently to the code using it. Such prototypes are often collected together in a header file and since a function is only known to the compiler after it has been declared. In C++ prototypes have a fundamental role to play in defining classes and therefore objects.
  • 8. 8 Introduction to programming in C++ for engineers: 13. Functions I Functions cannot declare functions!!!Functions cannot declare functions!!! Declaring functions within other functions is not available in C++. Since every program must have a function called main() and a function cannot be defined inside another function the general structure of a program is a list of function definitions. The relationship between the function look like a tree. For some problems functions are a very effective way of controlling complexity. In really complicated problems related functions can be encapsulated. We do not start from this feature because: - there is no point in attacking a small problem with quite complicated technique, - classes are partly built from functions - we need to know them quite good before introducing object-oriented techniques.
  • 9. 9 Introduction to programming in C++ for engineers: 13. Functions I Unused argumentsUnused arguments There is no requirement for all of the formal arguments in a function declaration to be used in which case an identifier does not have to be specified, but the function cannot be called without an argument, even though the argument is not used. void print(int) { cout << “Testn”; } print(1); // OK print(); // WRONG! Incorrect number of param. Unused arguments may serve to reserve a place in the argument list for future use. An argument type without identifier can also be useful when an argument has been made redundant by a changing function implementation, but the calling has not been updated
  • 10. 10 Introduction to programming in C++ for engineers: 13. Functions I Default argumentsDefault arguments A function declaration can specify expressions which are to be used as the default values for one or more arguments. double rectangle(double a = 1.0, double b = 2.0) { return a *b } // three possible function calls area1 = rectangle; // 1*2=2 is assigned area2 = rectangle(3.0); // 3*2=6 is assigned area3 = rectangle(4.0, 5.0) // 4*5=20 is assigned The default arguments must be supplied right to left. A default argument cannot be redefined in a subsequent declaration. It is not allowed to give the same default arguments as in a function definition (in a prototype). However a subsequent declaration can introduce one or more additional default arguments. Default arguments must be provided for any arguments omitted in a function call.
  • 11. 11 Introduction to programming in C++ for engineers: 13. Functions I Ignoring the return valueIgnoring the return value The default arguments do not need to be named in a function prototype, however omitting argument names is not a good idea since they are a valuable method of self-documenting code. There is also the distinction between prototypes and definitions: names cannot be omitted from function definitions. Ignoring the return value The calling function can ignore the value returned by a function. int status = mkdir(“test”); // create directory test if (status) cout < “Failed to create directory.n”; else cout << “Directory created.n”; If we want to ignore the risk that the directory creation can fail we can simply write: mkdir(“test);
  • 12. 12 Introduction to programming in C++ for engineers: 13. Functions I SummarySummary ! A function definition implements the function. ! Functions are executed (called or invoked) by statements like: x = f(m); or f(m); ! If function is declared to return a value it must do so. ! The order of evaluation of expressions in argument lists is not defined. x = f(++i, (i+5)); // compiler dependent ! A function prototype defines the function interface. ! A function cannot be declared inside another function. ! Functions can have default arguments which are supplied from right to left.