0% found this document useful (0 votes)
16 views9 pages

14-User Defined Functions-PU 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views9 pages

14-User Defined Functions-PU 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PU–1–IMP User Defined Functions

Multiple Choice Questions


1. Where user defined function can be invoked/called in a program?
(a) Declaration (b) Input statement (c) Anywhere (d) Break statement
Sol: Anywhere
Ans: (c)
2. Select how user defined functions are involved/called, from the following
(a) Declaration (b) Input statement
(c) Assignment statement (d) Break statement
Sol: Assignment statement
Ans: (c)
3. How many user defined function can be included in a program?
(a) Only one (b) More than one (c) Zero (d) Limited
Sol: More than one
Ans: (b)
4. What does the keyword void represent?
(a) returns a single value (b) returns 2 values
(c) returns 0 values (d) returns many values
Sol: returns many values
Ans: (d)
5. An actual arguments and formal arguments should
(a) Same in number and order (b) Different in number and order
(c) Same in number and different order (d) Different in number and same order
Sol: Same in number and order
Ans: (a)

Five Marks Questions


6. Explain the various advantages of functions.
Answer: (each point 1 mark)
(a) Modularity: Functions can be used to divide a large bulky program into functionally independent
modules or sub programs.
(b) Universal use: User defined functions can be made a part of a library, which in turn, facilitates the
usage of that function across other ‘C++’ programs.
(c) Code Reusability: Writing the same sequence of code at two or more locations in the program can
be avoided with the use of functions and universal use feature reduces repeated rewriting of codes.
(d) Teamwork: Functions promote teamwork. They divide a large bulky program into functionally
independent modules and each person in the team can develop a separate subprogram.

Copyright © Ace Creative Learning Pvt Ltd.,| www.deekshalearning.com


PU–1–IMP User Defined Functions

(e) Reduction in program size: Reduces the overall program size.


(f) Easy Debugging: Easy to detect errors and correct them in a module.
7. Explain the structure of a function.
Answer: Return_type function_name (data-type arg1, data-type arg2, ... data-type argn)
{
variable declarations;
statement 1;
statement 2;
statement n;
return(expression);
} (2 marks)
Function_type: The function-type identifies the type of return value, which will be sent back after the
function has performed its task. E.g.: function type may be a int, float, char,, void.
Function name: Function name followed by a pair of open and close parentheses ’( )’ helps to identify
name of the function and its arguments.
( ) The parentheses can contain an optional list of arguments that are passed to the function.
Arguments are used to transfer/copy (pass) the values from calling function to called function.
Body of the function: It is enclosed within braces. A function should have at least one executable
statement and rest of the things are same as in main ( ) function. The body of the function can also have
an optional declaration for the variables to be used by the function only also called as local variables.
Return (expression): The return statement can return the value of any expression. The return statement
can contain an optional expression or value that is to be returned back to the calling function.
(3 marks)
8. Write a short note on scope of variables.
Answer: A scope is a region of the program and there are three places where variables can be declared:
• Inside a function or a block which are called local variables.
• Outside of all functions which are called global variables. (1 mark)
Local variables: ( 1 / 2 mark)
Variables that are declared inside a function or block are local variables. They can be used only by
statements that are inside that function or block of code. Local variables are not known to functions
outside their own. (1 mark)
Global variables: ( 1 / 2 mark)
Global variables are defined outside of all the functions, usually on top of the program. The global
variables will hold their value throughout the lifetime of a program. (1 mark)
A global variable can be accessed by any function. That is, a global variable is available for use
throughout the entire program after its declaration.
Copyright © Ace Creative Learning Pvt Ltd.,| www.deekshalearning.com
PU–1–IMP User Defined Functions

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 ()
{

Copyright © Ace Creative Learning Pvt Ltd.,| www.deekshalearning.com


PU–1–IMP User Defined Functions

void printmessage(); //function declaration


printmessage ();
return 0;
} (2 marks)
In the above program, the main() and printmessage() which is a user defined function are the two
functions. The function call is given by main() and called function printmessage() display message” I’m
a function” without returning any value. The return type of printmessage() is void because function
has no argument and no return value. The user defined function printmessage() is defined with empty
parentheses indicates the function is without any parameter. Such type of functions are called as
function with no argument and no return value. (1 mark)
10. Explain functions with argument and no return value with an example.
Answer: The calling function main() gives the function call to the called function by passing arguments
or values. Then the called function takes the values and performs the calculations and gives the output
in itself but no value will be sent back to the calling function. (2 marks)
For example,
#include <iostream>
void sum(int x, int y)
{
int sum;
sum = x + y;
cout << “The sum is “<<sum;
}
int main ()
{
void sum(int x, int y)
cout <<“enter the two numbers”;
cin >> a >> b;
sum (a, b);
return 0;
} (2 marks)
In the above example, sum() is a user defined function. main() is a calling function gives a function call
sum (a, b); with actual arguments a, b. The copy of values of a and b are sent to formal arguments x
and y in the called function sum(). The function sum() performs addition and gives the result on the
screen. Here no value being sent back to the calling function main() can be observed. These kinds of
functions are called “Function with argument but no return values”. (1 mark)

Copyright © Ace Creative Learning Pvt Ltd.,| www.deekshalearning.com


PU–1–IMP User Defined Functions

11. Explain Functions with argument and with return value.


Answer: The calling function gives a function call to the called function by passing arguments or
values.
Then the called function takes those values and performs the calculations and returns a value, which
will be sent back to the calling function. This is done using the return statement. (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 numbers”;
cin >> a >> b;
sumv=sum (a, b);
cout <<“The sum of two numbers “ << sumv;
return 0;
} (2 marks)
In the above example, sum() is a user defined function void sum(int x, int y)having two arguments.
The statement int a,b,sumv; declare local variables in main() function. The statement sumv=sum (a, b);
is a function call and actual arguments a and b values are sent to formal arguments x and y which is
the local variable to sum() function. The statement return(sum); takes the control back along with the
value present in the sum variable and assign it to the sumv variable in the calling function main(). This
is how the function with argument and with return value works. (1 mark)
12. Explain the working of function with no arguments and with return values.
Answer: In this type of function, the calling function gives the function call to the called function
without sending any values. Then called function executes its own statements and return back to the
calling function with a return value. (2 marks)
For example,
#include <iostream>
int sum(); .
int main ()

Copyright © Ace Creative Learning Pvt Ltd.,| www.deekshalearning.com


PU–1–IMP User Defined Functions

{
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;

Copyright © Ace Creative Learning Pvt Ltd.,| www.deekshalearning.com


PU–1–IMP User Defined Functions

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);

Copyright © Ace Creative Learning Pvt Ltd.,| www.deekshalearning.com


PU–1–IMP User Defined Functions

cout << “The sum of two numbers” <<sumv;


return 0; .
} (2 marks)
In the above example, sum() is a user defined function void sum(int x, intt y)having to arguments. The
statement mt a,b,sumv; declare local variables in main() function. The statement sumv=sum (a, b); is a
function call and actual arguments a and b values are sent to formal arguments x and y which is the
local variable to sum() function. The statement return(sum); takes the control back along with the value
present in the sum variable and assign it to the sumv variable in the calling function main().
(1 mark)
15. Explain function call using pass by reference technique with an example.
Answer: . Pass by reference is another way of passing parameters to the function. Pass by reference or
Call by reference is the method by which the address of the variables (actual arguments) are passed to
the called function. The “&“ symbol is used to reference the address of the variables to the called
function. The changes made to the formal parameter will change values in the actual arguments.
(2 marks)
For example
#include <iostream>
void swap(int &x, int &y); //function declaration
int main ()
{
// local variable declaration:
int a= 100;
int b= 200;
cout << “Before swap, value of a :“ << a <<endl;
cout << “Before swap, value of b :“ << b << endl;
// calling a function to swap the values using variable reference.
swap(a, b);
cout << “After swap, value of a :“ << a << endl;
cout << “After swap, value of b :“ << b << endl;
return 0;
}
void swap(int &x, int &y) // function definition to swap the values.
{
int temp;
temp = X; /* save the value at address x */
x = y; /*put y into x */

Copyright © Ace Creative Learning Pvt Ltd.,| www.deekshalearning.com


PU–1–IMP User Defined Functions

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)

Copyright © Ace Creative Learning Pvt Ltd.,| www.deekshalearning.com

You might also like