A function is a block of statements that together perform certain task.

Defining a Function:

The general form of a C++ function definition is as :
Return_type function_name(parameter list)
body of function
• Return Type: A function may return a value. it is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In such situation, the return_type is the keyword void.
• Function Name: This is the actual name of the function. The function name and the list of parameter together constitute the function signature.
• Parameters: A parameter is act like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameters or arguments. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are not compulsry; that is, a function may contain no parameters.
• Function Body: The function body contains a block of statements that define what the function does.

Example 1

#include <iostresm.h>

// function declaration

int max(int num1, int num2);

int main ()


// local variable declaration: int a = 100;

int b = 200;

int ret;

// calling a function to get max value.

ret = max(a, b);

cout << “Max value is : ” << ret << endl;

return 0;


// function returning the max between two numbers

int max(int num1, int num2)


// local variable declaration

int result;

if (num1 > num2)

result = num1;

else result = num2;

return result;



Max value is:200

Default Values for Parameters:

If a value for that parameter is not passed when the function is called, the default given value is used, but if a value is mention, this default value is ignored and the passed value is used instead.


int sum(int p, int q=20)


int s1; result = p + q;

return (s1);


int main ()


// local variable declaration:

int p = 100;

int q = 200;

int s1; // calling a function to add the values.

result = sum(p, q);

cout << “Sum is :” << s1 << endl; // calling a function again as follows.

result = sum(p);

cout << “Sum is :” << s1 << endl;

return 0;


Sum is :300

Sum is :120

Leave a Reply

Your email address will not be published. Required fields are marked *