14-User Defined Functions-PU 1
14-User Defined Functions-PU 1
It should be noted that, a program can have the same name for local and global variables but value of
local variable inside a function will take preference.
For example,
#include <iostream>
‘int g_value =0; :
int mamo
{
++g_value;
cout <<g_value <<endl; :
int g_value= 100;
cout <<“g_value local variable which hides the global one: “<< g_value << endl;
cout <<“g_value global variable: “<<::g_value <<endl; /* In case we have a local variable with the
same name as a global one, we can access the global variable using the global scope operator. */
return 0;
} (1 mark)
In the above example, we have declared a global variable, g_value, at the beginning of our program,
outside any other block. As you can see, we can increment it in the function main and print its value to
the screen.
The variables a, b and g_value are local variables in the above program. The g_value variable is present
in main() also. Now this variable g_value is a local to main() function which will hide the global
variable. We can then access the local variable by its name, and as you can see it hides the global
variable. Suppose we have to access global variable then we have to use the global scope operator,::,
before the variable’s name::g_value.
9. Explain functions with no argument and no return value with an example.
Answer: In the care of functions, where calling function gives function call to called function without
any parameters, then called function processes statements of its own and returns back without any
value to the calling function. Such called function returns type is normally declared with void data
type. (2 marks)
For example,
#include <iostream>
void printmessage ( )
{
cout <<“I’m a function!”;
}
int main ()
{
{
int sumv;
sumv=sum();
cout <<“The sum of two numbers” <<sumv;
return 0;
}
int sum()
{.
int a,b,sum; .
cout << “enter the two numbers”;
cin >> a >> b;
sum=a+b;
return(sum)
} (2 marks)
In the above example, sum() is a user defined function. The calling function is main() gives the function
call without passing any argument sumv=sum(); and called function sum() accept the values and gives
out result back to calling function. The main() function displays the output. (1 mark)
13. Explain passing default argument to the function, with an example.
Answer: When we define a function we can specify a default value for each of the parameters. This
value will be used if the corresponding argument is left blank during function call to the called
function. (2 marks)
This is done by using the assignment operator and assi3nin3 values for the arguments in the function
definition. During the function call, if a value for that parameter is not passed then default value is
used, but if a value is mentioned then this default value is ignored and the passed value is used. .
Consider the following example:
#include <iostream>
int sum(int a, int b=20)
{
int result;
result = a + b;
return (result);
}
int main ( )
{
// local variable declaration:
int x = 100;
is it y = 200;
int result;
// calling a function to add the values.
result = sum(x, y);
cout <<“Total value is :“ <<result << endl;
//calling a function again as follows.
result = sum(x);
cout <<“Total value is :“ << result << endl;
return 0;
} (2 marks)
In the above program, the statement int sum(int a, int b = 20) in the function header indicate the default
argument to the variable b is assigned with the value 20. During the first function call, the statement
result = sum(x, y); copies the value of x and y to the called function and processes the result.
During second time function call i.e., result = sum(x); , only one argument is mentioned and the value
for second argument is obtained from the default argument which is mentioned in the function
definition i.e., b=20, and gives the processed result. (1 mark)
14. Explain function call using pass by value technique with an example.
Answer: In pass by value mechanism, during the function call actual arguments send the copy of
values to formal arguments that are present in called function. These formal arguments are copies of
the actual arguments and are stored in the temporary locations of the memory. During the process by
the called function, the changes made to the formal arguments do not change the actual arguments.
(2 marks)
For example,
# include <iostream>
void sum(int x, int y)
{
int sum;
sum = x + y;
return(sum);
int main ()
{
void sum(int, int); .
int a,b, sumv;
cout <<“enter the two number”;
cin >> a >> b;
sumv=sum (a, b);
y = temp;/*put x into y */ .
return;
} (2 marks)
Before the function call values of a and b were 100 and 200 respectively. After the function, though no
values are returned back to the main(), it shows the value of a and b as 200 and 100 respectively. This is
due to call by reference in which changes made in the formal argument affect the values in the actual
argument.
The output of the above program is given below.
Before swap, value of a: 100
Before swap, value of b: 200
After swap, value of a: 200
After swap, value of B: 100 (1 mark)