Functions in C++

Single or set of statements that are enclosed under particular name is called function. Function is normally identified by small parenthesis after function name. In C++ it has following properties.

Properties of functions:

  1. Identifer (Name)

  2. Parameters (Arguments/Input)

  3. Return Type (Output)

1. Identifier:

Identifier is the unique and descriptive name of the function. This identifier is used to get/call function in program. Name of the function is defined at the time of function declaration.

It is best practice that function name should be descriptive.

2. Parameters:

Parameters/Arguments are the input of the function, which can be used inside function defination. Function defination is body of the function containing valid C++ statements. A function can take multiple parameters as per requirement. User must provide complete parameters at the time of function call otherwise compiler will raise error.

3. Return Type:

In C++ function may have return type, but this is not mandatory. A function can also have void return type. A function has void return type cannot return any data value.

A function having void return type cannot used in assignment statement.

Types of functions:

There are two types of functions in C++.

  1. Built-in/Standard functions
  2. User defined functions

1. Standard functions:

Standard functions which are also known as built-in functions are ready-made functions provided by the language. These functions are already defined in C++ library files. Programmers have to just include the corresponding library in program and call these funtions by its names.

Examples:

Some examples of standard functions are getch(), getche(), sin(), cos(), tan(), clrscr() and many other.

2. User defined functions:

Sometime these functions are also called custom functions. USer defined functions are created by the programmer according to the problem requirement. These functions are provided descriptive and rememberable names.

Be careful that User defined function name must be not same as Standard function name.

Function Declaration in C++

Function declaration is the announcement of a function in program. It is also called function signature. In declaration programmer have to specify all three properties of function as discussed above. A function is normally declared before main() function. In function declaration we have to provide parameters data types separated by comma. We can also provide parameter names in function declaration but these are optional.

If function defination is written before main() function, then function declaration will be omitted.

Examples:

Some examples of function declaration having two parameters and int return type.

int getSum(int,int);
int getSum(int a,int b);

Examples of function declaration having void return type.

void getSum(int,int);
getSum(int,int);

Function Defination:

A function can be define before or after the main() function. Function defination has following two parts.

  1. Function Header
  2. Function Body

1. Function Header:

Function header is approximately similar statement with function declaration. Function header must contains complete parameters list with identifiers. Semicolon is not used after function header.

Examples:

int getSum(int x,int y)
void getSum(int x,int y)

2. Function Body:

Body of function contain statements that will execute whenever function is called. These statements are enclode in curly brackets. Function return statement is also include in function body.

Example:

{
  cout<<"Hello Print Lines";
}
{
  return a + b;
}

Function Call:

Statement that activate function for execution is called function call. Function is always called by its name. Whenever function is called all statements inside function body executed. A function call can be used in assignment statement if it return value which is of same type as LValue.

Parameter Passing Techniques:

Parameters are the input of the functions. These are also commonly known as arguments. In C++ parameters can be passed using three different techniques.

  1. Pass by values
  2. Pass by reference
  3. Pass by pointer

1. Pass by Value:

In pass by value technique only value of the parameter is passed from call of function. The parameter in function header receive only data value. There will no connection of actual and formal parameters. It means if any change made in formal parameters it will not effect actual parameters.

Example:

#include <iostream>
using namespace std;
int getSum(int x, int y); // function declaration
int main(int argc, char** argv) {
	
	int a = 10;
	int b =20;
	int c;
	c = getSum(a,b);
	cout<<c;
	
	return 0;
}
//function defination
int getSum(int x, int y) //function header
{
	return x + y;
}

2. Pass by Reference:

In pass by reference technique function header use reference (address) operator to accept parameters address. Whenever function is called the address of actual parameter is used in function defination. So that the change in formal parameters effect values of actual parameters.

Pass by reference is used for processing huge data set such as arrays

Example:

#include <iostream>
using namespace std;
int getSum(int &x, int &y); // function declaration
int main(int argc, char** argv) {
	
	int a = 10;
	int b =20;
	int c;
	c = getSum(a,b);
	cout<<c;
	return 0;
}
//function defination
int getSum(int &x, int &y) //function header
{
	return x + y;
}

3. Pass by Pointer:

This technique is much similar to pass by reference except some syntactically differences. In this technique function header contains pointer arguments so that at the time of function call we have to provide address of argument. Similarly to pass by reference if there will any change in formal parameter then actual parameters will also change.

Example:

#include <iostream>
using namespace std;
int getSum(int *x, int *y); // function declaration
int main(int argc, char** argv) {
	
	int a = 10;
	int b =20;
	int c;
	c = getSum(&a,&b);
	cout<<c;
	return 0;
}
//function defination
int getSum(int *x, int *y) //function header
{
	return *x + *y;
}

Difference between Pass by Reference, Pass by Value and Pass by Pointer

Sr. Pass by Value Pass by Reference Pass by Pointer
1 Only value of actual parameters are passed to function call. Actual parameters are passed and function header accept its address. Address of actual parameter is passed at time of function call.
2 Function header contains independent formal parameters. Function header contains address operators with formal parameters. Function header contains pointer formal parameters.
3 Change in formal parameters does not effect actual parameters. Change in formal parameters also change actual parameters. Change in formal parameters also change actual parameters.

Types of Parameters:

There are two types of paramters used in functions. Thses are as follows.

  1. Actual Parameter
  2. Formal Parameter

1. Actual Parameter:

The parameters that are passed to the function at the time of function call are called actual parameters. These parameters may be in the following forms.

1. Value

In this way a constant data value is provided in function call this value may be of any type such as number or string. eg: 10, 20.6, "Hello IMS".

Example:
cout<<getSum(10,20);

2. Variable:

In this technique variable is provided in function call and the value is passed to the function header. Similarly this variable may be of any type such as int, float, double or char.

Example:
int a = 10;
int b = 20;
cout<<getSum(a,b);

3. Expression:

Expressions can also used as actual parameters. When expression is passed to function call then first expression is evaluated and its value is passed to function header.

Example:
int a = 20;

cout<<getSum(a + 10 , a);

4. Function:

Function call can also be actual paramter for any function. When a function call is used as actual parameter then first function call result is get and then it is passed to the parent function call.

Example:
int a = 60;
cout<<getSum(getSum(20,30),a);

2. Formal Parameter:

The parameters that are written in function header are called formal parameters. These parameters are of following types.

1. Variable

2. Pointer

Comments
Login to TRACK of Comments.