Function In C++

What  is a function

Function is  the small and simple module of a large program. Functions plat an important role in program development.

Feactures of function

  • Function reduces the repetition of lines in the program.
  • Since them is no repetition, the size of program reduces.
  • Compilation time is less because of small programs.
  • One program can call several functions.
  • One function  can be called upon several times.
  • A function can receive 0 or more values.
  • A function can return 0 or 1 value.
  • The function that calls itself is called recursive function.
  • Several function having same name in one program are called  overloaded functions.
  • Functions can  be written in the same program or can be stored in a diffrernt file called as library file.

Types of function

Pre defined function

 Function that are already defined are called Pre defined function or library function.

User defined function

Function defined by the programmers are called User defined function. They can be classified as follows.

  1. No values received and no  value returned.
  2. One or more values received and no values returned.
  3. One or more values received an one value returned.
  4. No value received and one value returned.

 

Declaration of function (function  Prototyping)

The declaration of function is also known as prototype of the function.

Ti includes the following.

 1 the data type of the return value.

2 the name of the function.

3 the data types of the list of arguments.

Colling of function

Calling the function means transferring the control of execution to the body of the function. It includes the following.

1 the  variable that receives value returned by the function.

2 the name of the function.

3 the list of variables or values that to be passed to the function.

Body of function

The function definition or the body of the function contain the  set of statements that are required to be executed. It includes the following .

1 the data types of the return value.

2 the name of the function .

3 the variables that receives the values.

4 the set of statements.

Parameters (arguments) of function

Actual parameters

the list of variables that are passed to the function are called actual parameters.

Formal parameters

The list of variables that are received by the function are called formal parameters.

Overloaded function

One program can defined several function with the same name having different signature. The signature corresponds to the data  types and number of the parameters . such function are called overloaded function and the process is called  function  polymorphism in  object oriented programming .

In other words the overloaded function allow us to perform a variety of different tasks under the same name.

Working of overloaded function
  1. The compiler matches the prototype having the same number and type of arguments and calls the appropriate function.
  2. If the exact match is not found, the compiler promoted the data types such as char is promoted to int, int is promoted to float.
  3. If such promotion does not  work, the compiler tries to use built  in conversion such as int to float or int to double etc.
  4. If all the above steps fail then an error message is generated during compilation.

#include<iostream.h>

#include<conio.h>

Void show() {

Cout<<”OOPS”<<endl;

}

Void show (int a) {

Cout<<”square of”<<a<<”is”<<a*a<<endl;

}

Void show(int a, int b) {

Cout<<”Product of ”<<a<<”and”<<b<< “is”<< a+b << endl;

}

Void show(float a) {

Cout<<”cube of ”<<a<<”is”<<a*a*a<<endl;

}

Void main() {

Clrscr();

Show();

Show(2,8);

Show(2,5);

Show(7);

Getch();

}

Rules for overloaded function:
  1. the function performing the related jobs should be overloaded. This avoid confusion while using them.
  2. There should not be  ambiguity in the prototype of the function.
Advantages of  overloaded functions:
  1. The users need not remember the names  of the functions as they all have the  names of the  functions  as they all have the same name.
  2. The compiler automatically tries to call the function that has  nearest match in case if exact match is not found.
Inline function

 Every time when the function is called, the control of the program is transferred to a different part of the memory where the  function is loaded. After the completion of the  execution of the function, the  control takes extra execution time. It can be prevented using the  inline function.

The inline function  are expanded at the place of its calling. This increases the length of the program but execution speed increases. The inline function is declared as follows.

#include<iostream.h>

#include<conio.h>

Inline void show(int a, int b) {

Int c;

C= a+b;

Cout<<c;

}

Void main() {

Show(7,9);

Show(100,300);

Show(1,2);

}

Advantage of inline function :
  1. The program with inline function  execution executes faster.
  2. The  overhead of function call and return statement is eliminated.
Limitation of inline function:
  1. The inline function cannot work with static variables.
  2. The inline function cannot be made  recursive.
  3. It does not support switch goto etc.
  4. It does not support nested loop.
  5. Inline function should be small in size.
Function with default arguments

Function can be called w without specifying all its arguments.
The arguments that are written along with   pre-defined set of values are know as default arguments. 
In absence of the values, the default arguments are automatically taken by the function. 
Correct use of the default arguments may reduce the number of function.
Correct in one program. It is suggested to use the default arguments in case of overloaded function.

Rules for defining the default arguments
  1. One or more arguments can be  defined with default arguments.
  2. The default arguments must be specified only after the arguments that are having no default values.
  3. No default value can be assigned to the argument that occurs in the middle of the argument list.
  4. Only the trailing arguments can have their default values.

#include<iostream.h>

#include<conio.h>

Void add(int a = 10, int b = 20, int c = 30) {

Int s;

S= a+b+c;

Cout<<”the sum is”<<s;

}

Void main() {

Clrscr();

Add(1,2,3);

Add(1,2);

Add(1);

Add();

Getch();

}

Advantages of default arguments:

  1. We can use the default arguments  to add new parameters to the existing functions.
  2. Default arguments can be used to combine similar functions into one.
  3. Default arguments are useful in situations where some arguments always have the same value.

FUNCTION WITH CONST ARGUMENTS

The qualifier const written with the function arguments defines constant arguments.
They are not allowed to be changed in the function.

Advantages  of const arguments

  1. This types of arguments are significant when we do not want the parameter values to be changed.
  2. Const arguments are used when the values are passed by reference or pointers.

#include<iostream.h>

#include<conio.h>

 

Void sum(int a, const int b) {

Cout<<a<<” “<<b<<endl;

A =20;

B =30; // this statement will not compile because variable  b is constant

Cout<<a<< “ “ <<b<< endl;

}

Void main() {

Clrscr();

Sum(111,222);

Getch();

}

FUNCTION WITH REFERENCE ARGUMENTS

Reference is another name given to the memory variable.
Reference does not occupy space in the memory.
Any change made to the variable deliberately changes the values of the reference and vice versa.
A value can be passed to the function via refrence.
The change made to the reference changes the original value of the variable.

Features of reference arguments:

  1. The actual parameters are changes when the formal parameters are changed if the formal parameters are defined as references
  2. It is used when we want the changes permanent.

#include<iostream.h>

#include<conio.h>

Void change(int a, int&b) {

A = 20;

B = 200;

}

Void main() {

Int a = 10;

Int b = 100;

Clrscr();

Change(a, b);

Cout<<” a =“<<” b =“<<b;

Getch();

}