Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Functions in c++
 Like c, c++ also contain a special function called
main().
 The main() function is an entry point of program
execution, it contain the code that tell computer what
to do as a programme execution.
 Because main() is function, you can write other
function within your source file and main() can call
them.
c++ lecture note by hansa halai
c++ lecture note by hansa halai
 Executable program
main()
{
// call functions..
someFunction1();
someFunction2();
}
someFunction2();
someFunction1();
Operating system
runs program
Return to OS
 In C++ , the main() returns value of type int to
operating system, so in c++ the main() function is
written as ,
int ()
int main(int arg1,char *argv[])
 So , if we write a return type int , we have to use return
statement inside the main() function, which are
written as:
int main()
{
…
return 0;
}
c++ lecture note by hansa halai
 Advantage of using functions:
o It help in reducing both physical and executable file size
in large program.
o Save memory, because all the calls to a function tell the
compiler to execute the same block of code.
o Enhances the program’s readability, understandability,
and maintainability.
o It helps in the reusability of the code.
c++ lecture note by hansa halai
 A function prototype tells the compiler the name of
function, the type of data returned by the
function, the number of parameter the function
expect to receive ,the type of parameters, and the
order in which these parameters are expected.
 The compiler use function prototypes to validate
function call.
 Syntax:
return_type function_name(argument_list);
c++ lecture note by hansa halai
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
int max(int n1,int n2);
int main()
{
int n1;
int n2;
int a;
cout<<"Enter Number1: "<<"n";
cin>>n1;
cout<<"Enter Number2: "<<"n";
cin>>n2;
a=max(n1,n2);
cout<<"max value is "<<a<<"n";
return 0;
}
c++ lecture note by hansa halai
int max(int n1,int n2)
{
int result;
if(n1>n2)
result=n1;
else
result=n2;
return result;
}
 The c++ provides easy and effective use of reference
variable.
 When function is called, the caller creates a reference
for each argument and using references the actual
argument are accessed.
 This method of passing the arguments or parameters
to the function is called call by reference.
c++ lecture note by hansa halai
 To use the call by reference , function is written as:
int exch(int& a,int& b)
{
int t;
t=a;
a=b;
b=t;
}
When this function is called as :
exch(x , y);
It passes the arguments as
int & a=x;
int & b=y
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
int exch(int&,int&);
int main()
{
int num1,num2;
cout<<"Enter Number1: ";
cin>>num1;
cout<<"Enter Number2: ";
cin>>num2;
cout<<"nBefore Swap:"<<"nn";
cout<<"Number1 is:"<<num1<<"n";
cout<<"Number2 is:"<<num2<<"n";
c++ lecture note by hansa halai
c++ lecture note by hansa halai
exch(num1,num2);
cout<<"nAfter Swap:"<<"nn";
cout<<"Number1 is:"<<num1<<"n";
cout<<"Number2 is:"<<num2<<"n";
return 0;
}
int exch(int& a,int& b)
{
int t;
t=a;
a=b;
b=t;
}
 Not only c++ function accepts the arguments as a
reference , but it can also return a reference.
 Like,
int& min(int& a, int& b)
{
return(a<b ? a:b);
}
c++ lecture note by hansa halai
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
int& max(int&,int&);
int main()
{
int num1,num2,m;
cout<<"Enter Number1: ";
cin>>num1;
cout<<"Enter Number2: ";
cin>>num2;
c++ lecture note by hansa halai
m= max(num1,num2);
cout<<"max number is "<<m;
return 0;
}
int& max(int& a,int& b)
{
return(a>b?a:b);
}
c++ lecture note by hansa halai
 Values of actual
argument are passed to
dummy argument.
 Need extra memory to
store copy of value.
 Called function can not
access actual values in
caller function
 Reference are created for
actual argument.
 No need for extra
memory as only aliases
are created.
 Called function can
access the actual values
in caller function using
reference.
Call By Value Call By Reference
c++ lecture note by hansa halai
 Provide only one way
communication from
caller to called function.
 Slower as values are to be
copied.
 Simple and easy to
understand.
 Provide two way
communication between
caller and called function.
 Faster as no memory
operation required.
 Simple and easy to
understand
 We know that functions save memory space because all
calls to function cause the same code to be executed,
the function body need not to be duplicate in memory.
 When a compiler see a function call, it normally
generate a jump to the function, at end of the function,
it jump back to the instruction after the call.
 But when every time a function is called, it takes a lot of
extra time in executing a series of instructions for task
as jumping to the instruction.
c++ lecture note by hansa halai
 One solution for speedy execution and saving memory
in c++ is use inline function , it is used only for short
function.
 When use inline function , the function code in the
inline function body is inserted into the calling
function, instead of the control going to the function
definition and coming back.
 So function can not jump from its calling place. So it
will save the jumping time program execution is faster
compare to normal function. But it is used only for
short function, not use any loop inside it.
c++ lecture note by hansa halai
 You can declare an inline function before main() by
just placing the keyword inline before the normal
function.
 Syntax:
inline function_name(argument_list)
{
function_body
}
c++ lecture note by hansa halai
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
inline float mul(float x, float y)
{
return(x*y);
}
inline float div(float a, float b)
{
return(a/b);
}
c++ lecture note by hansa halai
int main()
{
float a,b;
cout<<"Enter number1:";
cin>>a;
cout<<"Enter number2:";
cin>>b;
cout<<"Answer of multiplication is "<<mul(a,b)<<"n";
cout<<"Answer of Division is "<<div(a,b)<<"n";
}
 Normally function is called with same number of
argument listed in function header.
 C++ differ in this matter and allows calling of function
with less number of argument then listed in its header.
 C++ make it possible by allowing default argument.
 Ex:
int max(int a,int b, int c=10);
c++ lecture note by hansa halai
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
int max(int,int,int c=10);
int main()
{
int a,b,p;
cout<<"Enter value:";
cin>>a>>b;
p=max(a,b);
cout<<"Max num is: "<<p<<endl;
return 0;
}
c++ lecture note by hansa halai
int max(int x,int y,int z)
{
int max =x;
if(y>max)
max=y;
if(z>max)
max=z;
return max;
}
 We can use const to make the arguments to the
function constant so that function can not change the
value of that argument.
 Like,
int fun(const int x)
{
int y;
x= x+1; // invalid
y=x; // valid
Return(y);
}
c++ lecture note by hansa halai
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
float circle(float,const float pi=3.14);
int main()
{
float r,area;
cout<<"Enter r:";
cin>>r;
area=circle(r);
cout<<"Area of circle is "<<area<<endl;
}
float circle(float r,float pi)
{
float c_area;
c_area=pi*r*r;
return c_area;
}
 The function overloading is ,same function name is
used for different task or different purpose it is also
called function polymorphism in oop.
 Using this concept, we can design a family of function
with same function name but it have different number
of arguments, different types of arguments and
different return type.
 All the function perform different operation
depending on matching of argument list in the
function call.
c++ lecture note by hansa halai
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
const float pi=3.14;
float area(float r)
{
return(pi*r*r);
}
float area( float l, float b)
{
return(l*b);
}
int main()
{
float radius;
cout<<"Enter radius: ";
cin>>radius;
c++ lecture note by hansa halai
cout<<"Area of circle is : "<<area(radius)<<endl;
float length,breadth;
cout<<"Enter length: ";
cin>>length;
cout<<"Enter breadth: ";
cin>>breadth;
cout<<"Area of rectangle is: "<<area(length,breadth)<<endl;
return 0;
}
Functions in c++

More Related Content

Functions in c++

  • 2.  Like c, c++ also contain a special function called main().  The main() function is an entry point of program execution, it contain the code that tell computer what to do as a programme execution.  Because main() is function, you can write other function within your source file and main() can call them. c++ lecture note by hansa halai
  • 3. c++ lecture note by hansa halai  Executable program main() { // call functions.. someFunction1(); someFunction2(); } someFunction2(); someFunction1(); Operating system runs program Return to OS
  • 4.  In C++ , the main() returns value of type int to operating system, so in c++ the main() function is written as , int () int main(int arg1,char *argv[])  So , if we write a return type int , we have to use return statement inside the main() function, which are written as: int main() { … return 0; } c++ lecture note by hansa halai
  • 5.  Advantage of using functions: o It help in reducing both physical and executable file size in large program. o Save memory, because all the calls to a function tell the compiler to execute the same block of code. o Enhances the program’s readability, understandability, and maintainability. o It helps in the reusability of the code. c++ lecture note by hansa halai
  • 6.  A function prototype tells the compiler the name of function, the type of data returned by the function, the number of parameter the function expect to receive ,the type of parameters, and the order in which these parameters are expected.  The compiler use function prototypes to validate function call.  Syntax: return_type function_name(argument_list); c++ lecture note by hansa halai
  • 7. c++ lecture note by hansa halai #include<iostream> using namespace std; int max(int n1,int n2); int main() { int n1; int n2; int a; cout<<"Enter Number1: "<<"n"; cin>>n1; cout<<"Enter Number2: "<<"n"; cin>>n2; a=max(n1,n2); cout<<"max value is "<<a<<"n"; return 0; }
  • 8. c++ lecture note by hansa halai int max(int n1,int n2) { int result; if(n1>n2) result=n1; else result=n2; return result; }
  • 9.  The c++ provides easy and effective use of reference variable.  When function is called, the caller creates a reference for each argument and using references the actual argument are accessed.  This method of passing the arguments or parameters to the function is called call by reference. c++ lecture note by hansa halai
  • 10.  To use the call by reference , function is written as: int exch(int& a,int& b) { int t; t=a; a=b; b=t; } When this function is called as : exch(x , y); It passes the arguments as int & a=x; int & b=y c++ lecture note by hansa halai
  • 11. #include<iostream> using namespace std; int exch(int&,int&); int main() { int num1,num2; cout<<"Enter Number1: "; cin>>num1; cout<<"Enter Number2: "; cin>>num2; cout<<"nBefore Swap:"<<"nn"; cout<<"Number1 is:"<<num1<<"n"; cout<<"Number2 is:"<<num2<<"n"; c++ lecture note by hansa halai
  • 12. c++ lecture note by hansa halai exch(num1,num2); cout<<"nAfter Swap:"<<"nn"; cout<<"Number1 is:"<<num1<<"n"; cout<<"Number2 is:"<<num2<<"n"; return 0; } int exch(int& a,int& b) { int t; t=a; a=b; b=t; }
  • 13.  Not only c++ function accepts the arguments as a reference , but it can also return a reference.  Like, int& min(int& a, int& b) { return(a<b ? a:b); } c++ lecture note by hansa halai
  • 14. c++ lecture note by hansa halai #include<iostream> using namespace std; int& max(int&,int&); int main() { int num1,num2,m; cout<<"Enter Number1: "; cin>>num1; cout<<"Enter Number2: "; cin>>num2;
  • 15. c++ lecture note by hansa halai m= max(num1,num2); cout<<"max number is "<<m; return 0; } int& max(int& a,int& b) { return(a>b?a:b); }
  • 16. c++ lecture note by hansa halai  Values of actual argument are passed to dummy argument.  Need extra memory to store copy of value.  Called function can not access actual values in caller function  Reference are created for actual argument.  No need for extra memory as only aliases are created.  Called function can access the actual values in caller function using reference. Call By Value Call By Reference
  • 17. c++ lecture note by hansa halai  Provide only one way communication from caller to called function.  Slower as values are to be copied.  Simple and easy to understand.  Provide two way communication between caller and called function.  Faster as no memory operation required.  Simple and easy to understand
  • 18.  We know that functions save memory space because all calls to function cause the same code to be executed, the function body need not to be duplicate in memory.  When a compiler see a function call, it normally generate a jump to the function, at end of the function, it jump back to the instruction after the call.  But when every time a function is called, it takes a lot of extra time in executing a series of instructions for task as jumping to the instruction. c++ lecture note by hansa halai
  • 19.  One solution for speedy execution and saving memory in c++ is use inline function , it is used only for short function.  When use inline function , the function code in the inline function body is inserted into the calling function, instead of the control going to the function definition and coming back.  So function can not jump from its calling place. So it will save the jumping time program execution is faster compare to normal function. But it is used only for short function, not use any loop inside it. c++ lecture note by hansa halai
  • 20.  You can declare an inline function before main() by just placing the keyword inline before the normal function.  Syntax: inline function_name(argument_list) { function_body } c++ lecture note by hansa halai
  • 21. c++ lecture note by hansa halai #include<iostream> using namespace std; inline float mul(float x, float y) { return(x*y); } inline float div(float a, float b) { return(a/b); }
  • 22. c++ lecture note by hansa halai int main() { float a,b; cout<<"Enter number1:"; cin>>a; cout<<"Enter number2:"; cin>>b; cout<<"Answer of multiplication is "<<mul(a,b)<<"n"; cout<<"Answer of Division is "<<div(a,b)<<"n"; }
  • 23.  Normally function is called with same number of argument listed in function header.  C++ differ in this matter and allows calling of function with less number of argument then listed in its header.  C++ make it possible by allowing default argument.  Ex: int max(int a,int b, int c=10); c++ lecture note by hansa halai
  • 24. c++ lecture note by hansa halai #include<iostream> using namespace std; int max(int,int,int c=10); int main() { int a,b,p; cout<<"Enter value:"; cin>>a>>b; p=max(a,b); cout<<"Max num is: "<<p<<endl; return 0; }
  • 25. c++ lecture note by hansa halai int max(int x,int y,int z) { int max =x; if(y>max) max=y; if(z>max) max=z; return max; }
  • 26.  We can use const to make the arguments to the function constant so that function can not change the value of that argument.  Like, int fun(const int x) { int y; x= x+1; // invalid y=x; // valid Return(y); } c++ lecture note by hansa halai
  • 27. c++ lecture note by hansa halai #include<iostream> using namespace std; float circle(float,const float pi=3.14); int main() { float r,area; cout<<"Enter r:"; cin>>r; area=circle(r); cout<<"Area of circle is "<<area<<endl; } float circle(float r,float pi) { float c_area; c_area=pi*r*r; return c_area; }
  • 28.  The function overloading is ,same function name is used for different task or different purpose it is also called function polymorphism in oop.  Using this concept, we can design a family of function with same function name but it have different number of arguments, different types of arguments and different return type.  All the function perform different operation depending on matching of argument list in the function call. c++ lecture note by hansa halai
  • 29. c++ lecture note by hansa halai #include<iostream> using namespace std; const float pi=3.14; float area(float r) { return(pi*r*r); } float area( float l, float b) { return(l*b); } int main() { float radius; cout<<"Enter radius: "; cin>>radius;
  • 30. c++ lecture note by hansa halai cout<<"Area of circle is : "<<area(radius)<<endl; float length,breadth; cout<<"Enter length: "; cin>>length; cout<<"Enter breadth: "; cin>>breadth; cout<<"Area of rectangle is: "<<area(length,breadth)<<endl; return 0; }