Kerala Plus One Computer Science Chapter Wise previous Questions and Answers Chapter 10 Functions
Question 1.
Explain the difference between Call-by-value and Call-by-reference methods with suitable examples. (3)
Call-by-value, Call-by-reference m1 രീതികൾ തമ്മിലുള്ള വ്യത്യാസം ഉദാഹരണ March 2018]
Answer:
Call by value method:
- Ordinary variables are used as formal parameters.
- Actual parameters may be constants, variables or expressions.
- The changes made in the formal arguments are not reflected in actual arguments.
- Exclusive memory allocation is required for the formal arguments.
Call by reference method:
- Reference variables are used as formal parameters.
- Actual parameters will be variables only.
- The changes made in the formal arguments are reflected in actual arguments.
Memory of actual arguments is shared by formal arguments.
Call-by-value:
eg., eg., void change(int n)
n = n + 1;
cout << “n=”<<n<< ‘\n’;
int main()
int x = 20;
change(x): cout << “x = ” << x;
Here only the value of the variable x is passed to the function. Thus the formal parameter in the function will get the value 20. When we increase the value of n, it will not affect the value of the variable x. The output of the above code is n = 21, X = 20 Call-by-reference: void change(int & n)
n = n + 1; cout << “n=”<<n<< ‘\n’;
int main()
int x=20; change(x); cout << “x = ” << x;
Here the output will be n = 21, X = 21
Question 2.
Explain the scope of variables in a C++ program.
ഒരു C++ പ്രോഗ്രാമിൽ വേരിയബിളുകളുടെ (March 2018)
Answer:
Scope of local variables:
- Declared within a function or a block of statements.
- Available only within that function or block.
- Memory is allocated when the function or block is active and freed when the execution of the function or block is completed.
Scope of global variables:
- Declared outside all the functions.
- Available to all the functions of the program.
- Memory is allocated just before the execution of the program and freed when the program stops execution.
eg., #include <iostream> using namespace std; int cube(int n) { int cb; cout<< “The value of x passed to n is” < < x; /*This is an error because the variable x is declared within the main () function. So it cannot be used in other functions. */ cb = n * n * n; return cb; } int main( ) { int x, result; cout << “Enter a number : ”; cin >> x; result = cube(x); cout << “Cube = ” << result; cout << “\nCube = ”<<cb; }
In the above program, scope of the variable cb is in the cube() function because it is declared within that function. Hence this variable cannot be used outside the function. This scope is known as local scope.
Question 3.
Name the built-in function to check whether a character is alphanumeric or not.
തന്നിരിക്കുന്ന ക്യാരക്ടർ ആൽഫാന്യൂമെറിക് ആണോ അല്ലയോ എന്ന് പരിശോധിക്കുവാൻ [March 2017]
Answer:
isalpha()
Question 4.
Read the function definition given below. Predict the output, if the function is called as convert (7) [March 2017]
താഴെ തന്നിരിക്കുന്ന ഫങ്ങ്ഷൻ നിർവ്വചനം UWAGA. convert(7) ലഭിക്കുന്ന ഔട്ട്പുട്ട് എന്തായിരിക്കുമെന്ന് പാ 4.
void convert (int n) { if (n>1) convert (n/2); cout<<n%2; }
Answer:
The output will be 111. This is a recursive function. The process of calling a function by itself is known as recursion and the function is
known as recursive function.
Question 5.
Explain the difference between call-by-value method and call-by-reference method with the help of examples. (3)
Call-by-value, Call-by-reference രീതികൾ തമ്മിലുള്ള വ്യത്യാസം ഉദാഹരണ [March 2017]
Answer:
Call by value method:
- Ordinary variables are used as formal parameters.
- Actual parameters may be constants, variables or expressions.
- The changes made in the formal arguments are not reflected in actual arguments.
- Exclusive memory allocation is required for the formal arguments.
Call by reference method:
- Reference variables are used as formal parameters.
- Actual parameters will be variables only.
- The changes made in the formal arguments are reflected in actual arguments.
Memory of actual arguments is shared by formal arguments.
Call-by-value:
eg., eg., void change(int n)
n = n + 1;
cout << “n=”<<n<< ‘\n’;
int main()
int x = 20;
change(x): cout << “x = ” << x;
Here only the value of the variable x is passed to the function. Thus the formal parameter in the function will get the value 20. When we increase the value of n, it will not affect the value of the variable x. The output of the above code is n = 21, X = 20 Call-by-reference: void change(int & n)
n = n + 1; cout << “n=”<<n<< ‘\n’;
int main()
int x=20; change(x); cout << “x = ” << x;
Here the output will be n = 21, X = 21
Question 6.
Suggest most suitable built-in function in C++ to perform the following tasks: (2)
a. To find the answer for 5
b. To find the number of characters in the string “KERALA”.
c. To convert the character ‘M’ to ‘m’
d. To get back the number 10 if the argument is 100.
താഴെ പ്രസ്താവിച്ചിരിക്കുന്ന പ്രവർത്തികൾ യ്യുവാൻ ഏറ്റവും അനുയോജ്യമായ C++ ലെ ബിൽറ്റ് ഇൻ ഫങ്ഷനുകൾ നിർദ്ദേശിക്കു
a. 53 ന്റെ ഉത്തരം ലഭിക്കാൻ
b. “KERALA” എന്ന സിംഗിലെ അക്ഷരങ്ങ ളുടെ എണ്ണം ലഭിക്കാൻ
c. “M’ എന്ന അക്ഷരത്തെ “m’ ആക്കി മാറ്റു ന്നതിന്
d. 100 എന്ന നമ്പർ ആർക്യുമെന്റായി നൽകു മ്പോൾ 10 ലഭിക്കുവാൻ (March 2016)
Answer:
a. pow(53)
b. strlen(“KERALA”)
c. tolower(‘M’)
d. sqrt(100)
Question 7.
A function can call itself for many times and return a result.
ഒരു ഫങ്ഷൻ അതിനെത്തന്നെ വീണ്ടും വീണ്ടും വിളിച്ച് റിസൽറ്റ് നൽകുവാൻ സാധി ക്കും
a. What is the name given to such function? (1)
ഈ രീതിയിൽ പ്രവർത്തിക്കുന്ന ഫങ്ഷനുക ൾക്ക് നൽകിയിരിക്കുന്ന പേര് എന്ത്?
b. Write a function’s definition of the above type to find the sum of natural numbers from 1 to N. (3)
1 മുതൽ 9 വരെയുള്ള നമ്പരുകളുടെ തുക കണ്ടുപിടിക്കുന്ന ഒരു ഫങ്ഷൻ നിർവ്വചനം എഴുതുക.
(Hint: If the value of N is 5, the answer will be 1+2+3+4+5+=15) (March 2016)
Answer:
a. Recursive function
b.
int sum (int n) { if (n = = 0) return 0; else return (n + sum(n-1)); }
Question 8.
The function which calls itself is called a ………..
ഒരു ഫങ്ഷ ൻ (function) ആ function നെ തന്നെ വിളിക്കുകയാണെങ്കിൽ അത്തരം function നുകളുടെ പേര് പറയുക. [March 2015]
Answer:
Recursive function
Question 9.
Construct the function prototypes for the following functions. (2)
a. The function Display( accepts one argument of type double and does not return any value.
b. Total( accepts two arguments of type int, float respectively and returns a float type value.
താഴെ പറയുന്ന function കൾക്ക് ആവശ്യ മായ function prototypes എഴുതുക.
a. Display() function double type
ൽ ഉള്ള വില സ്വീകരിക്കുന്നു. എന്നാൽ യാതൊരു വിലയും മടക്കി അയക്കുന്നില്ല.
b. Total ) എന്ന function int, float എന്നീ type-ൽ ഉള്ള വിലകൾ സ്വീകരിക്കുകയും float type-ൽ ഉള്ള വില മാത്രം മടക്കി അയ
ക്കുകയും ചെയ്യുന്നു. (March 2015)
Answer:
a. Display(double var); Total(int vari, float var2)
Question 10.
Name the different methods used for passing arguments to a function. Write the difference between them with examples. (3)
ഒരു function ലേക്ക് വിലകൾ അയക്കുന്നതി നുള്ള മാർഗ്ഗങ്ങളുടെ പേരുകൾ എഴുതുക. അവ തമ്മിലുള്ള വ്യത്യാസം ഉദാഹരണ സഹായത്തോടെ വിശദമാക്കുക. [March 2015] Answer:
Call by value method:
- Ordinary variables are used as formal parameters.
- Actual parameters may be constants, variables or expressions.
- The changes made in the formal arguments are not reflected in actual arguments.
- Exclusive memory allocation is required for the formal arguments.
Call by reference method:
- Reference variables are used as formal parameters.
- Actual parameters will be variables only.
- The changes made in the formal arguments are reflected in actual arguments.
Memory of actual arguments is shared by formal arguments.
Call-by-value:
eg., eg., void change(int n)
n = n + 1;
cout << “n=”<<n<< ‘\n’;
int main()
int x = 20;
change(x): cout << “x = ” << x;
Here only the value of the variable x is passed to the function. Thus the formal parameter in the function will get the value 20. When we increase the value of n, it will not affect the value of the variable x. The output of the above code is n = 21, X = 20 Call-by-reference: void change(int & n)
n = n + 1; cout << “n=”<<n<< ‘\n’;
int main()
int x=20; change(x); cout << “x = ” << x;
Here the output will be n = 21, X = 21
Leave a Reply