Coupon Accepted Successfully!


More Programs

7.31 Write a program to convert a float value to integer and char. Invoke overloaded function show() as per the data type.


Explanation: In the above program, the function show() is overloaded for float, int, and char data types, respectively. In for loop, conversion from float to int and float to char data type is carried out and converted values are assigned to variables i and c, respectively. After each conversion show() function is invoked. The compiler invokes the appropriate function that exactly matches the data type argument. Thus, output displays float, int, and corresponding ASCII character.

7.32 Write a program to use function as default arguments.



Explanation: In the above program, the function in() is used to read an integer value through the keyboard. The function in() is used in the prototype of function sum() as a default argument. Thus, when sum() function is called with less arguments, the function in() is executed and user can enter a value to pass in the sum() function. The execution of in() function depends upon the number of arguments passed to the function sum(). In this program, the function sum() is invoked with decreasing arguments from 4 to 1.

First time the function sum() is called with four arguments. Hence, there is no need of default argument and function in() will not be invoked. In second call, three arguments are passed, that is with one less argument. This time sum() needs one default argument and in() function supplies this value. Thus, in the above successive calls the function in() is called based on passed arguments.

7.33 Write a program to use global variable as default argument.

Explanation: In the above program, function show() is used to increase the value passed to it. The function show() returns increased value. The prototype of function show() contains default arguments (v + 5), that is value of variable v is added to 5 and the result is used as a default argument for the function show(). In the first call, the function show() is called with the single argument and it returns value 3. In second call, show() is called without argument. This time function show() takes default argument, that is v + 5 = 7 and returns the result 8. The third time the function show() is again called without argument. This time it returns the result 9 because the global variable v is incremented by 1. Thus, the use of global variable as a default argument changes the default value in different calls. The use of local variable as a default argument is not allowed. If such attempt is made, the compiler shows an error message “Default expression may not use local variables.”

7.34 Write a program to use function as an argument for another function.

Explanation: In the above program the function show() is used as an argument. When function is used as an argument, the argument function is called first and its return value is used as an argument for the first function. In the first call the function uses the default value 7 and it increments by one. The return value 8 is again passed to the function show(). The return value again increases by one and finally the show() function returns the value 9.

7.35 Write a program to overload a function display(). Print the integer, double and string using overloaded show() function.

Explanation: In the above program the function display() is overloaded for displaying integer, double and string. In function main(), integer, double and string are passed to function display(). The compiler executed appropriate function display() and displays the passed value on the screen.

7.36 Write a program to declare a function with default arguments. Execute the function without argument.

Explanation: In the above program character pointer s is initialized with string “ Hearty Welcome” and it is declared as default arguments for function display(). The default argument is used when the function is invoked without argument. Here, in the first call a string “Hello” is passed and it is displayed on the screen by the function display(). In second call, the function display() is called without argument. In this call the default argument is used and the string displayed will be “ Hearty Welcome”.

7.37 Write a program to draw line. Create line function with default arguments.

Explanation: In the above program the line function draws line on the screen. It has two default arguments ‘-’ and 50. These default arguments are used when function is called without arguments. In the above program the line() function is invoked 5 times out of which two times invoked without argument. The first and last lines are drawn using default arguments.

7.38 Program to display the results of addition, subtraction, division, and multiplication of float numbers and modulus of given two integers.




7.39 Program to display function show() with default parameters



7.40 Program to find addition and multiplication of digits of any number.


7.41 Program to convert binary number to decimal.


7.42 Program to overload function addition of integers and floats.

Test Your Skills Now!
Take a Quiz now
Reviewer Name