Functions: Lect-04
Functions: Lect-04
Lect-04
Returning Values from Functions
#include <iostream> float lbstokg(float pounds)
using namespace std; {
float lbstokg(float); float kilograms = 0.453592 * pounds;
int main() return kilograms;
{ }
float lbs, kgs;
cout << “\nEnter your
Enter your weight in pounds: 182
weight in pounds: “;
Your weight in kilograms is 82.553741
cin >> lbs;
kgs = lbstokg(lbs);
cout << “Your weight in
kilograms is “ << kgs <<
endl;
return 0;
}
Returning Structure Variables
#include <iostream>
using namespace std;
struct Distance
cout << “Enter feet: “;cin >>d2.feet;
{
cout << “Enter inches: “; cin >>d2.inches;
int feet;
d3 = addengl(d1, d2);
float inches;
cout << endl;
};
engldisp(d1); cout << “ + “;
Distance addengl(Distance,
engldisp(d2); cout << “ = “;
Distance);
engldisp(d3); cout << endl;
void engldisp(Distance);
return 0;
int main()
}
{
Distance d1, d2, d3;
cout << “\nEnter feet: “;cin
>>d1.feet;
cout << “Enter inches: “; cin
>>d1.inches;
Distance addengl( Distance dd1, Distance void engldisp( Distance dd )
dd2 ) {
{ cout << dd.feet << “\’-” <<
Distance dd3; dd.inches << “\””;
dd3.inches = dd1.inches + dd2.inches; }
dd3.feet = 0;
if(dd3.inches >= 12.0) Enter feet: 4
{ Enter inches: 5.5
dd3.inches -= 12.0; Enter feet: 5
dd3.feet++; Enter inches: 6.5
} 4’-5.5” + 5’-6.5” = 10’-0”
dd3.feet += dd1.feet + dd2.feet;
return dd3;
}
#include <iostream>
using namespace std; void intfrac(float n, float&
void intfrac(float, float&, float&); intp, float& fracp)
int main() {
{ long temp =
float number, intpart, fracpart; static_cast<long>(n);
do { //convert to long,
cout << "Enter a real number: "; intp =
//number from user static_cast<float>(temp);
cin >> number; //back to float
intfrac(number, intpart, fracpart); fracp = n - intp;
cout << "Integer part is " << intpart<< ", fraction }
part is " << fracpart << endl; Enter a real number: 99.44
} while( number != 0.0 ); Integer part is 99, fractional
//exit loop on 0.0 part is 0.44
return 0;
}
Different Numbers of Arguments
#include <iostream> void repchar(char ch)
using namespace std; {
void repchar(); for(int j=0; j<45; j++)
void repchar(char); cout << ch;
void repchar(char, int); cout << endl;
}
int main() void repchar(char ch, int n)
{ {
repchar(); for(int j=0; j<n; j++)
repchar(‘=’); cout << ch;
repchar(‘+’, 30); cout << endl;
return 0; }
}
void repchar()
{
Different Kinds of Arguments
#include <iostream> cout << “Enter entire distance in
using namespace std; inches: “; cin >> d2;
struct Distance engldisp(d1);
{ engldisp(d2);
int feet; cout << endl;
float inches; return 0;
}; }
void engldisp( Distance ); void engldisp( Distance dd )
void engldisp( float ); {
int main() cout << dd.feet << “\’-” << dd.inches
{ << “\””;}
Distance d1; void engldisp( float dd )
float d2; {int feet = static_cast<int>(dd / 12);
cout << “\nEnter feet: “; cin >> d1.feet; float inches = dd - feet*12;
cout << “Enter inches: “; cin >> cout << feet << “\’-” << inches <<
d1.inches; “\””;}
Recursion
The existence of functions makes possible a programming technique called
recursion.Recursion involves a function calling itself. This sounds rather
improbable, and indeed a function calling itself is often a bug. However, when
used correctly this technique can be surprisingly powerful.
#include <iostream>
using namespace std; unsigned long factfunc(unsigned long
unsigned long factfunc(unsigned long); n)
int main() {
{ if(n > 1)
int n; return n * factfunc(n-1); //self call
unsigned long fact; else
cout << “Enter an integer: “; return 1;
cin >> n; }
fact = factfunc(n);
cout << “Factorial of “ << n << “ is “ << fact
<< endl;
return 0;
}
Inline Functions: function save memory
We mentioned that functions save memory space because all the calls to the
function cause the same code to be executed; the function body need not be
duplicated in memory. When the compiler sees a function call, it normally
generates a jump to the function. At the end of the function it jumps back to the
instruction following the call
Inline Functions: small function can slow the process
While this sequence of events may save memory space, it takes some extra time.
There must be an instruction for the jump to the function (actually the assembly-
language instruction CALL or something similar), instructions for saving registers,
instructions for pushing arguments onto the stack in the calling program and
removing them from the stack in the function(if there are arguments), instructions
for restoring registers, and an instruction to return to the calling program. The
return value (if any) must also be dealt with. All these instructions slow down the
program.
Inline Functions can save excution time
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.
#include <iostream>
using namespace std;
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
Surprisingly, a function can be called without specifying all its arguments. This
won’t work on just any function: The function declaration must provide default
values for those arguments that are not specified.
#include <iostream>
using namespace std; void repchar(char ch, int n)
void repchar(char=’*’, int=45); //defaults supplied
int main() {
{ //
repchar(); if necessary
repchar(‘=’); for(int j=0; j<n; j++)
repchar(‘+’, 30); cout << ch;
return 0; cout << endl;
} }
Local Variables
Variables defined within a function body are
void somefunc()
called local variables because they have
{
local scope. However, they are also
int somevar;
sometimes called automatic variables,
float othervar;
because they have the automatic storage
}
class.
A local variable is not created until the function in which it is defined is called.
(More accurately, we can say that variables defined within any block of code are
not created until the block is executed.
The function returns and control is passed back to the calling program, the
variables are destroyed and their values are lost. The name automatic is used
because the variables are automatically created when a function is
called and automatically destroyed when it returns.
The time period between the creation and destruction of a variable is called its
lifetime (or sometimes its duration)
The idea behind limiting the lifetime of variables is to save memory space.
Scope of the variables
A variable’s scope, also called visibility, describes the locations within a program
from which it can be accessed. It can be referred to in statements in some parts of
the program; but in others, attempts to access it lead to an unknown variable error
message. The scope of a variable is that part of the program where the variable is
visible.
void somefunc()
{
int somevar; //local variables
float othervar;
somevar = 10; //ok
othervar = 11; //ok
nextvar = 12; //illegal: not visible in somefunc()
}
void otherfunc()
{
int nextvar; //local variable
somevar = 20; //illegal: not visible in otherfunc()
othervar = 21; //illegal: not visible in otherfunc()
nextvar = 22; //ok
}