1.

A program requires functions for adding 2 numbers, 3 numbers and 4 numbers, How can you provide a solution by writing a single functio0n?

Answer»

It can be solved by writing function with default values.

eg: int add (int n1, int n2, int n3 = 0, int n4 = 0) 

{

return (n1 + n2 + n3 + n4); 

}

This function can be invoked by the following function calling 

1. add (5,2);

2. add (5,2,7); 

3. add (5,2,7,10); 

The 1. Call returns 5 + 2 = 7 

2. Call returns 5 + 2 + 7 = 14 

3. Call returns 5 + 2 + 7 + 10 = 24



Discussion

No Comment Found

Related InterviewSolutions