Professional Documents
Culture Documents
OBJECT-ORIENTED PROGRAMMING
(OOP) WITH C++
Lecture-4
Chapter -5
Bilal Munir Mughal
FUNCTIONS
2
1. Simple Functions
2. Passing Arguments to Functions
3. Returning Values from Functions
4. Reference Arguments
5. Overloaded Functions
6. Recursion
7. Inline Functions
8. Default Arguments
9. Scope and Storage Class
10. Returning by Reference
11. const Function Arguments
FUNCTION?
3
When a function returns a value, the data type of this value must be
specified. The function declaration does this by placing the data type
before the function name in the declaration and the definition.
RETURNING VALUES FROM
FUNCTIONS
#include <iostream>
using namespace std;
float lbstokg(float); // data/return type float
int main()
{
float lbs, kgs;
cout << "\nEnter your weight in pounds: ";
cin >> lbs;
kgs = lbstokg(lbs); //the value returned by the function assigned to variable
cout << "Your weight in kilograms is " << kgs << endl;
return 0;
}
//--------------------------------------------------------------
// lbstokg() converts pounds to kilograms
float lbstokg(float pounds) //data/return type float
{
float kilograms = 0.453592 * pounds;
return kilograms; }
RETURNING VALUES FROM
FUNCTIONS (The return Statement)
A return statement causes execution to leave the current function and
resume at the point in the code immediately after where the function
was called
To save execution time in short functions, you may elect to put the
code in the function body directly inline with the code in the calling
program.
That is, each time there’s a function call in the source file, the actual
code from the function is inserted, instead of a jump to the function.
INLINE FUNCTIONS
#include <iostream>
using namespace std;
// lbstokg() 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
In C++ scope and storage class are features related to the interaction of
variables and functions
The scope (visibility) of a variable determines which parts of the
program can access it,
and its storage class determines how long it stays in existence.
Scope
Local Scope Variables
Variables with local scope are visible only within a block.
are not created until that block is executed.
Storage Class
The time period between the creation and destruction of a variable is
called its lifetime (or sometimes its duration).
The lifetime of a local variable coincides with the time when the function in
which it is defined is executing.
There are two storage classes: automatic and static, that determine the
lifetime of a local variable
Variables with storage class automatic exist during the lifetime of the
function in which they’re defined. All variables declared within a block of
code are automatic by default, but this can be made explicit with the
auto keyword
Variables with storage class static exist for the lifetime of the
program.
SCOPE AND STORAGE CLASS
27
#include <iostream>
using namespace std;
int x; // global variable
int& setx(); // function declaration
int main()
{ // set x to a value, using
setx() = 92; // function call on left side
cout << "x=" << x << endl; // display new value in x
return 0;
}
//--------------------------------------------------------------
int& setx()
{
return x; // returns the value to be modified }
const FUNCTION ARGUMENTS
Q&A
?