Functions in C++ with examples.

What are reference variable?

  • Reference means address.
  • Reference variable is an internal pointer.
  • The declaration of reference variable is preceded with '&' (do not read it as "address of").
  • Reference variable must be initialized during its declaration.
  • Reference variable can only be initialized with already declared variables.
  • Reference variable cannot be updated once it is initialized.

Example: programs declaring reference variable

#include<iostream>
using namespace std;

int main()
{
  int num = 6;      // num is an ordinary variable.
  int *ptr;         // ptr is a pointer variable.
  
  ptr = &num;
  
  //ref is a reference variable and it is also get initialized here.
  int &ref = num;
  ref++;
  
  cout << num;      // 7
}
Output:
7

Functions in C++

  • Function is a block of code performing a unit task.
  • Function has a name, return type and argument.
  • Function is a way to achieve modularization.
  • Functions may be predefined and user defined.
  • Predefined functions are declared in the header files and defined in the library files.

Definition, Declaration and Function call

#include<iostream>
using namespace std;

void function_name()        // function definition.
{
  cout << "inside function";
}

int main()
{
  void function_name();       // function declaration.
  cout << "in main" << endl;
  
  function_name();            // function call.
}
Output:
in main
inside function

  • Function declaration is also known as function prototype.
  • Function need to be declared before use (just like variable).
  • Function can be declared locally or globally.
  • Syntax: RETURN_TYPE  FUNCTION_NAME  (argument_LIST);
  • Function definition is a block of code.

Function nature or type according to argument list

  • Takes Nothing, Returns Nothing.
  • Takes Something, Returns Nothing.
  • Takes Nothing, Returns Something.
  • Takes Something, Returns Something.

What are formal argument and Actual argument?

The arguments that are given to the function at the time of function definition are called formal arguments while the arguments that are given to the function during calling  a function are called actual arguments.
#include<iostream>
using namespace std;

int sumofnum(int,int);      // function declaration globally.

int main()
{
  int a = 5, b = 6;
  int sum = sumofnum(a,b);    // a and b are actual arguments.
  
  cout << "Sum is: "<< sum;    // Sum is: 11
}

int sumofnum(int x,int y)
{
  return (x+y);
}
Output:
Sum is: 11

Formal arguments can be of three types

  1. Ordinary Variables
  2. Pointer Variables
  3. Reference Variables

Call by Value

When formal arguments are ordinary variables, it is function call by value.
#include<iostream>
using namespace std;

int sum(int x, int y)
{
  return (x+y);
}

int main()
{
  int a = 5, b = 6;
  int s = sum(a,b);      //Call by Value
  cout << "Sum is: " << s;
}
Output:
Sum is: 11

Call by Address

When formal arguments are pointer variables, it is function call by address.
#include<iostream>
using namespace std;

int sum (int *, int *);

int main()
{
  int a = 5, b = 6;
  int s = sum(&a, &b);
  
  cout << "Sum is: " << s;
}

int sum(int *p, int *q)
{
  return (*p+*q);
}
Output:
Sum is: 11

Call by Reference

When formal arguments are reference variables, it is function call by reference.
#include<iostream>
using namespace std;

int sum(int &, int &);

int sum(int &x, int &y)
{
  return (x+y);
}

int main()
{
  int num1 = 5, num2 = 16;
  int S = sum(num1, num2);
  
  cout << S;
  
}
Output:
21

What are inline function in C++

  • As we all know that functions save memory (i.e., Functions in a program are to save memory space which becomes appreciable when a function is likely to be called many times.)
  • Function is time consuming (i.e., every time a function is called it takes a lot of extra time in executing a series of instructions for tasks such as jumping to the function, saving registers, pushing arguments into the stack and returning to the calling function.)
  • So, when function is small it is worthless to spend too much time in such tasks in cost of saving comparatively small space.
  • To eliminate the cost of calls to small function, C++ proposes a new feature called inline function.
  • An inline function is a function that is expanded in line when it is invoked. Compiler simply replaces the function call with the corresponding function code.
  • Remember that inline is request not a command.
  • Benefits of speed of inline function reduces as function grows in size. So, the compiler may ignore the request in some situations. Few of them are as follows:
  • function containing loops, switch and goto.
  • function with recursion.
  • function containing static variables.
#include<iostream>
using namespace std;

inline void func();

int main()
{
  cout << "You are in Main function" << endl;
  func();
}

void func()
{
  cout << "You are in function";
}
Output:
You are in Main function
You are in function

What are Default Arguments?

#include<iostream>
using namespace std;

int add(int, int = 0, int = 0);   // int=0 is called default argument.

int main()
{
  int a = 6, b = 15;
  
  cin >> a >> b;
  cout << add(a, b) << endl;     // here, c=0
  
  int c = 25;
  cin >> a >> b >> c;
  cout << add(a, b, c);
}

int add(int x, int y, int z)
{
  return (x+y+z);
}
Output:
21
46


<< Previous                                                                                      Next >>

Previous
Next Post »