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.h>
int main()
{
int num=6; // num is an ordinary variable.
int *ptr; // ptr is a pointer variable.

ptr=&num;
int &ref=num; //ref is a reference variable and it is also get initialized here.
ref++;

cout<<num; // 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.h>
int main()
{
void function_name(); // function declaration.
cout <<"in main";

function_name(); // function call.

void function_name() // function definition.
{
cout<<"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.h>
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);
}
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.h>
int sum(int x, int y)
{
return (x+y);
}

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

Call by Address

When formal arguments are pointer variables, it is function call by address.
#include<iostream.h>
int sum(int *, int *);
void main()
{
int a=5, b=6;
int s=sum(&a,&b);
cout<<"Sum is: "<<s;
}

int sum(int *p, int *q)
{
return (*p+*q);
}

Call by Reference

When formal arguments are reference variables, it is function call by reference.
int sum(int &, int &);
int s=sum(a,b);
int sum(int &x, int &y)
{
return (x+y);
}

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();

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

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

What are Default Arguments?

#include<iostream>
#include<conio.h>
using namespace std;
int add(int, int=0, int=0); // int=0 is called default argument.

void main()
{
clrscr();
int a,b;
cin>>a>>b;
cout<<add(a,b); // here, c=0

int c;
cin>>a>>b>>c;
cout<<add(a,b,c);
getch();
}

int add(int x, int y, int z)
{
return (x+y+z);
}
<< Previous                                                                                      Next >>

Previous
Next Post »