KEMBAR78
Chapter 4 (Function in C++) | PDF | Parameter (Computer Programming) | Pointer (Computer Programming)
0% found this document useful (0 votes)
10 views18 pages

Chapter 4 (Function in C++)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

Chapter 4 (Function in C++)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

FUNCTIONS

Introduction
• Function is like a reusable recipe in your program – write once, use
it anywhere, anytime without rewriting it.
• Functions are the main tool of Structured Programming.
– See the Syntax in C:
void show (); //function declaration
main() {
show (); //function call
}
void show (){
……………… //function definition: actual code
}
• Basically, functions in C and C++ have the same importance and
the applications.
• C++ has added many new features to functions to make them
more reliable and flexible. Like C++ operators, a C++ functions
can be overloaded.
• main() Function:
– In C++, the main() returns a value integer to the operating
system.
– That is why we always have a return() statement at the end of
the main().
– int main() {
…………
return 0; } \\generate warning if no return
– Operating system checks the return value as the function has
been successfully executed or not.
Function Prototyping
• Prototype describes the function interface to the compiler by
giving details such as number and type of arguments and return
value.
• Function Prototype is a kind of template that is used by compiler
to ensure that proper arguments are passed and return value is
treated correctly.
• Earlier C did not have the prototyping, it was firstly introduced
in C++ then it was adopted in ANSI C. However, it is optional
in C but compulsory in C++.
– <return type> <function name> (argument list);
float volume (int x, float y, float z);
• The names of the arguments are optional in declaration but must
in the function definition.
– float volume (int, float, float);
Call by Value (Copy)
• Call by Value means that only a copy of the value is passed to the
function.
• Changes made to the parameter inside the function do not affect the
original value in the calling function.
– Example: Swapping
void swap(int a, int b)
{
int t;
t=a;
a=b;
b=t;
}
swap(m,n);
Call by Reference (Original)
• Call by Reference means the actual variable (not a copy) is passed to
the function.
• Changes made to the parameters inside the function will affect the
original variable in the calling function.
– Example: Swapping
void swap(int &a, int &b)
{
int t=a;
a=b;
b=t;
}
swap(m,n);
Call by Passing (Address or Pointer)
• Call by Address means passing the memory address (pointer) of the
variables to the function.
• Like Call by Reference, changes made inside the function will affects
the original variables however, in this case you work with pointers to
access the memory locations. (Preferred in C in place of Call by
Reference). This method is also acceptable in C++.
– Example: Swapping
void swap (int *a, int *b)
{ int t;
t=*a;
*a=*b;
*b=t;
}
swap (&x, &y); //call by passing
//addresses of variables
Return by Reference
• A function can also return a reference.
– int& max(int &x, int &y)
{
if (x>y)
return x;
else
return y;
}
max (x, y)
• This function will return the address of the larger of x or y.
Inline Functions
• When a function is called substantial time is wasted in shifting of
the control. It adds more overheads when the size of the function is
very small. One alternative to it is Macros (like text replacement
before compilation), but their errors are not checked during the
compilation.
• In C++, we have inline functions. Here the compiler pastes the code
of the function directly at the place where you call it, instead of
jumping to the function’s address.
inline function-header {
function body }
inline float cube(float a) {
return(a*a*a); }
• Calling:
c=cube(3.0); \\inline function cube(3*3*3);
d=cube(2.5+1.5); \\inline fun cube(4*4*4)
Example
• #include <iostream>
using namespace std;
inline int square (int x) {
return x * x;}
int main() {
cout <<square (5) <<endl;
count <<square (10) <<endl;
• Normal function: jump to square function, calculate x*x and come
back. cout << square(5);
• Inline function: no jumping around, just direct calculation. cout
<<(5*5);
Inline Functions contd..
• The Inline functions are advantageous only when the size of the
function is too small, if we use the same technique for the bigger
functions the benefits of the Inline functions will be lost.
• In case of Inline function, the control doesn't go any where.
• The Inline Keyword is just a request to the compiler not the
command, the compiler may ignore the request if the size of the
function is too large, then it will be treated as normal functions.
Inline Functions contd..
• The Inline will not work in the following cases;
1. Functions having the loop, switch or goto statement.
2. Functions not returning any values if a return statement exists.
3. If functions have static variables.
4. If functions are recursive.
• Using the Inline functions every time the function is invoked new
memory is allocated so trade-off becomes necessary.
Default Arguments
• We can call a function in C++, without specifying all the arguments.
• We can give some default values in the function prototype.
– float amount(float p, int t, float rate = 0.15);
• Now if we call value = amount(5000,7);
• Then internally it will be converted into
value = amount(5000,7,0.15);
• Important here is that the default values must be added from right to
left. We cannot provide a default value to a particular argument in
the middle of function declaration.
– int mul ( int i, int j = 5, int k = 10): //legal
– int mul (int i = 5, int j ) //illegal
– int mul (int i = 2, int j, int k = 10); //illegal
Const Arguments
• The quantifier const tells the compiler that the function should not
modify the argument.
<return type> <function name> (const type arguments) { …. }
int strlen (const char *p); \\ can’t modify the char in the string
int length (const string &s); \\ return number of character
• The compiler will generate an error when this condition is violated.
• It is significant only when we pass arguments as reference or
pointers.
Function Overloading
• Overloading: Using the same thing for different purposes.
• When a same function name performs a variety of tasks is also
called function polymorphism in OOP.
– int add (int a, int b) //prototype 1
– int add (int a, int b, int c) //prototype 2
• The operation to be performed by the function will depend upon the
type and the no. of arguments passed to the function. It is done like
this :
– The compiler will try to find the exact match, actual type and no.
of arguments.
– If not, the compiler uses integral promotions
char to int; float to double
– Then built in conversions (implicit) are done, and if there is no
match then compiler will generate an error message.
Function Overloading
Precautions:
1. Unrelated functions should not be overloaded.
2. Default arguments can be used sometimes at place of
overloading.
Function Overloading Example
#include<iostream.h>
int volume (int);
double volume (double, int);
long volume (long, int, int);
int main() {
cout<<volume(10)<<“\n”;
cout<<volume(2.5,8)<<“\n”;
cout<<volume(100L,75,15)<<“\n”;
return 0;
}
Cont.
int volume(int s) //cube
{ return(s*s*s);
}
int volume(double r, int h) //cylinder
{ return(3.14519*r*r*h);
}
int volume(long l, int b, int h) //rectangular box
{ return(l*b*h);
}
Output
1000
157.26
112500

You might also like