C++ language

adplus-dvertising
Functions IN C++
Previous Home Next

A function is a block of code that performs a specific task. It has a name and it is reusable . it can be executed from as many different parts in a Program as required, it can also return a value to calling program. All executable code resides within a function. It takes in input, does something with it, then give the answer. A C++ program consists of one or more functions. A computer program cannot handle all the tasks by it self. It requests other program like entities called functions in C++. We pass information to the function called arguments which specified when the function is called. A function either can return a value or returns nothing.

Function is a subprogram that helps to reduce coding.

Note:If a program has only one function then it must be the main() function.

General form:
return_type function_name(parameter1, parameter2,......)
{
statements
}
where
  1. return_type is data type specifier of the data returned by the function.
  2. function_name is the identifier by which it will be possible to call the function.
  3. parameters each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration and which acts within the function as a regular local variable. They allow to pass argument to the function when it is called. Different parameters are separated by commas.
  4. statements is the function's body. It is a block of statements within braces{}.
A Simple Program Of Subtraction Using Function
#include <iostream.h>
using namespace sub;
int subtraction (int a, int b)
{
int r;
r=a-b;
return (r);
}
int main ()
{
int res;
res = subtraction (5,3);
cout << "The result is " << res;
return 0;
}
Output

The result is 2

Function With No Return Type

If we want to show a message on the screen without returning any value. In this case we use void.

For Example
#include <iostream.h>
void printmsg()
{
cout<<"This IS my first function";
}
int main()
{
printmsg()
{
printmessage();
return 0;
}
Output

This IS my first function

Default Value In Functions

When we declare a function we can also specify a default value for each parameter. If a value for that parameter is not passed when the function is called, the default value is used.

For Example
#include <iostream.h>
void div()
{
int r;
r=a/b;
return(r);
}
int main()
{
cout<<div(20);
cout<<div(20,4);
return 0;
}
Output
10
	  5

Declaration Of A Function

A function declaration establishes the name of the function and the number and types of its parameters. A function declaration consists of a return type, a name, and a parameter list. We have to declare a function before using it.

For Example
#include <iostream.h>
void odd(int a)  //function declaration
void even(int a);
int main() 
{
int i;
do
{
}
while 
{
cout<<"type a number (0 to exit)";
cin>>i;
odd(i);
}
while(i!=o);
return 0;
void odd(int a)
{
if((a%2)!=0)
cout<<"the number is odd";
else even(a);
}
void even(int a)
{
if(a%2)==0)
cout<<"number is even.\n";
else odd(a);
}
Output
Type a number (0 to exit)
7
number is odd
Type a number (0 to exit)
6
number is even
Type a number (0 to exit)
0

Argument passed by value and reference

The arguments passed to a function can be performed in two ways:

  1. Pass by value
  2. Pass by reference
  1. Pass By Value : when we passed the arguments by value the copies of the values of variables and are passed to the function.
  2. For Example
    int a=3, b=5;
    int r;                //function declaration
    r=sum (a, b);
    int sum (int x, int y)  //function call
    r=sum(3,4)
    
  3. Pass By Reference :When a variable is passed by reference, it passes the variable to the function definition and not the copies of the value. We use the & for passing the value by reference.
  4. For Example
    #include <iostream.h>
    void copy(int &a,int &b, int &c)
    {
    a*=2;
    b*=2;
    c*2;
    }
    int main()
    {
    int x=1,y=3;z=7;
    copy(x,y,z);
    cout<<"x="<<x", y"<<y<<", z"<<z;
    return 0;
    }
    

Recursive Function In C++

When the functions are called by itself, these type of functions are called Recursive Function.

For Example
// program of factorial using functuion
#include <iostream>
long fact(long a
{
if(a>1)
return(a*fact(a-1));
else
return(1);
}
int main()
{
long num;
cout<<"please type a number";
cin>>num;
cout<<num<<"!="<<factorial(num);
return 0;
}
Output
Please type a number
5!= 120

Main Function In C++

main() is a function from where the program starts execution. In C++ main() returns a value of type int to the operating system. C++ can define main() as one of the following prototype. It is good to return a value from main().
  1. int main(void);
  2. int main();
  3. int main(int arg, *argv[]);

Friend Function In C++

Friend function is an useful tool in the situation when any data or function is declared as Private. when any function or data is declared as private and we want to access it outside the class or non member function then we use Friend function. Friend Function is used by using Friend keyword. A friend keyword can only be placed in function declaration not in function definition. We can declare a function as friend in any number of classes. We can also declare a friend function as private or public.

#include <iostream>
using namespace std;
class Rectangle 
	{
    int w, h;
  public:
    void set_values (int, int);
    int area () {return (w * h);
	}
    friend Rectangle duplicate (Rectangle);
};
void Rectangle::set_values (int a, int b) {
  w = a;
  h = b;
}
Rectangle duplicate (Rectangle param)
{
  Rectangle rectres;
  rectres.w = param.w*2;
  rectres.h = param.h*2;
  return (rectres);
}
int main () 
	{
  Rectangle rect, rectb;
  rect.set_values (2,3);
  rectb = duplicate (rect);
  cout << rectb.area();
  return 0;
}
Output 24

Inline Function In C++

Normally, a function call transfers the control from the calling program to the function and after the execution of the program returns the control back to the calling program. These concepts of function saved program space and memory space are used because the function is stored only in one place and is only executed when it is called. This concept may be time consuming because the registers and other processes must be saved before the function gets called. The keyword inline is used for Inline function.

Syntax :
inline datatype function_name(arguments)
For Example
#include <iostream.h>
int inlinefun(int);
void main( )
{
int a;
cout << "\n Enter the Input Value: ";
cin>>a;
cout<<"\n The Output is: " << exforsys(a);
} 
inline int inlinefun(int a1)
{
return 5*a1;
} 
Output
Enter input value
20
The output is 
100

Virtual Function In C++

A Virtual Function is a function whose functionality can be overridden in derived class. This is different from Function overloading. Virtual function is a member function of a class. It is declared with virtual keyword. Virtual function have the different functionality in derived class. when we use virtual function a call is resolved at runtime. The different between virtual and non virtual function is that virtual function is resolved at runtime while non-virtual function is resolved at compile time.

Syntax
class classname//This denotes the base class of virtual function
{ 
public: 
virtual void namememberfunction() 
{ 
block of code 
} 
};
Previous Home Next