Sunday 1 January, 2012

IV. FUNCTIONS C++ ENHANCEMENTS



IV. FUNCTIONS C++ ENHANCEMENTS


Paragraph Answers

1.What are the different ways of passing parameters in C++ functions?
  • The call statement communicates with the function through arguments or parameters.
  • Parameters are the channels through which data flows from call statement to function and vice versa.
In C++, functions that have arguments can be invoked by
1.Call by value 2. Call by reference.
Call by value
  • In this method, the called function creates new variables to store the value of the arguments passed to it.
  • This method copies the values of actual parameters into the formal parameters
  • In this method,any change in the formal parameter is not reflected back to the actual parameter.
Ex.
#include <iostream.h>
#include <conio.h>
# include <iomanip.h>
void swap (int a)
{ int b;
b = 8;
a=b;
c
Output
10
8
Back to main value 10
out << ‘\n’<<a;

}
void main ( )
{
int m1 = 10;
cout <<m1 ;
swap (m1);
cout << “\n Back to main.. Value” << m1;
getch ( );
}
Call by reference.
  • In this method, formal parameters become alias to the actual parameters.
  • It is working on the original data.
  • Any change made in the formal parameter is reflected back in the actual parameter
Ex.
#include <iostream.h>
#include <conio.h>
# include <iomanip.h>
void swap (int &a)
{
Output
10
8
Back to main value 8
int b;

b = 8;
a=b;
cout << ‘\n’<<a;
}
void main ( )
{
int m1 = 10;
cout <<m1 ;
swap (m1);
cout << “\n Back to main.. Value” << m1;
getch ( );
}
2.Explain the working of a calling function with suitable example.
  • A function can be called or invoked from another function by using its name.
  • The function name may include a set of actual parameters, enclosed in parentheses separated by commas.
Working of a function
#include <iostream.h>
#include <conio.h>
# include <iomanip.h>
void swap (int a)
{ int b;
b
Output
10
8
Back to main value 10
= 8;

a=b;
cout << ‘\n’<<a;
}
void main ( )
{
int m1 = 10;
cout <<m1 ;
swap (m1);
cout << “\n Back to main.. Value” << m1;
getch ( );
}
3.Explain the call by value method in function with suitable example.
Ref. D Q.No: 1
4.Explain the call by reference method in which the changes in formal parameter in reflected back in the actual parameter.
Ref. D.No.: 1.
5.Write the rules for actual parameters in function prototype with suitable example.
In Value Type
  • The actual parameters can be passed in the form of constants or variables or expressions to the formal parameters
Ex.
For Function prototype : int add (int n1, int n2);
The call statements may be as follows :
x = add (5, 10);
x = add (a1, a2); where a1 and a2 are variables
In Reference Type
  • The actual parameters can be passed only as variables to the formal parameters
Ex.
For Function prototype int add (int & n1, int & n2);
The call statement is x = add (a1, b1) ; where a1 and b1 are variables
The following call statements are invalid:
x = add ((a1 + b1), a1);
x = add (5,10);

6.Explain default arguments with an example.
  • The default value is given in the form of variable initialization.
  • The default arguments facilitate the function call statement with partial or no arguments.
  • The default values can be included in the function prototype form right to left
Example
# include <iostream h>
#
Output:
80

include <conio.h>

int area (int l = 10, int b=20)
{ return (l * b); }
void main ( )
{ int s1 = 4, s2 = 6;
clrscr ( ) ;
cout << area (s1) << ‘\n’;
getch ( ); }
7.Explain Returning values with example.
  • The functions that return no value are declared as void.
  • The data type of a function is treated as int, if no data type is explicitly mentioned.
  • Default return value of a function in C++ is of type int.
Ex.
int add (int, int);
add (int , int);
Sl.No. Function Prototype Return type
1 float power (float, int) float
2 char choice ( ) char
3 char * success ( ) pointer to character
4 double fact (int) double
8.Explain Returning by reference with example.
  • A function returning a reference can appear on the left-hand side of an assignment.
  • The formal parameters for a reference function should always be of reference parameter type in the sense.
#include <iostream.h>
#include <conio.h>
# include <iomanip.h>
int &swap (int &a)
{ int b;
b
Output
10
8
Back to main value 8
= 8;

a=b;
cout << “\n”<<a;
return a;
}
void main ( )
{
int m1 = 10;
cout <<m1 ;
swap (m1);
cout << “\n Back to main.. Value” << m1;
getch ( );
}
In the above program,
  • The function swap returns a reference to int type of variable.
  • The function call swap(m1) will return a reference to either a or b depending upon which one is bigger of the two.
  • Hence, the variable m1 gets the value of the variable y.
9.Explain inline function with example
  • An inline looks like a normal function in the source file but inserts the function’s code directly into the calling program.
  • Inline functions execute faster but require more memory space
  • To make a function inline, one has to insert the keyword inline in the function header
  • inline keyword is just a request to the compiler
  • Sometimes the compiler will ignore the request and treat it as a normal function and vice versa
Example
inline swap (int a)
{ int b;
b = 8;
a=b;
cout << ‘\n’<<a;
}
void main ( )
{
int m1 = 10;
cout <<m1 ;
swap (m1);
cout << “\n Back to main.. Value” << m1;
getch ( );
}
10.write about the different scopes of a variable in C++.
Scope refers to the accessibility of a variable.
There are four types of scopes in C++.
They are:
1. Local scope, 2. Function scope, 3. File scope and 4. Class scope

1. Local scope
  • A local variable is defined within a block.
  • A local variable cannot be accessed from outside the block of its declaration.
  • Local variables are not known outside their own block.
  • A block of code begins and ends with curly braces { }.
  • Local variable is created upon entry into its block and destroyed upon exit
2. Function scope
  • The function scope of variable declared within a function
  • The scope of variable is extended to the function block, and all sub-blocks.
  • The life time of a function scope variable, is the life time of the function block.
  • The scope of formal parameters is function scope
3. File scope
  • The file scope of variable declared above main ( ).
  • The scope of variable is to the entire program.
  • The life time of a file scope variable is the life time of a program.

Ex.
#include <iostream.h>
#include<conio.h>
Int a=10;
main()
{
Output
30

clrcsr();
int b=5;
{
Int c=15,;
cout<<a+b+c;
}
getch();
}
Here,
a - File scope variable
b - Function scope variable
c - Local variable


4. Class scope
  • The class body has three access specifiers, They are ,
  • Private , protected and public.
Class name
{
Private:
{ declaration; }
Protected:
{ declaration; }
Public:
{ declaration; }
};
11.Explain local scope with an example.
Ref.: D.No 10

Short Answers

1. What are functions? (or) What are the advantageous of functions?
  • Functions are the building blocks of C++ programs.
  • Functions are also the executable segments in a program.
Advantage of function
Reduce the size of the program.
Reusability of code
A function can be shared by other programs by compiling it separately and loading them together.
2. Write the general syntax of a function prototype. Given an example.
The general syntax of a function prototype
<type> <function identifier> <arguments>;
For example:
void fun (char);
int max (int, int);
int max (int a, int b);

3. What is the main purpose of using function prototype?
  • Functions should be declared before they are used in a program.
  • Declaration of a function is made through a function prototype.
  • It is to help the compiler to check the data requirement of the function.
  • With function prototyping, a template is used to ensure that proper arguments are passed, and the return value is treated correctly.
4. What are the informations provided by prototype to the compiler?
The prototype provides the following information to the compiler:
1. Number and type of arguments
2. The type of return values .
5. What is calling a function? How a function can be called from another function?
  • A function can be called or invoked from another function by using its name.
  • The function name may include a set of actual parameters, enclosed in parenthesis separated by commas.
6. What is the use of parameters passing in function? (or) What is parameters or arguments?
  • The call statement communicates with the function through arguments or parameters.
  • Parameters are the channels through which data flows from call statement to function and vice versa.
7. What are the two methods used in functions? How the function that have arguments can be invoked in C++?
In C++, functions that have arguments can be invoked by
Call by value
Call by reference.
8. What is call by value method in functions?
  • In this method, the called function creates new variables to store the value of the arguments passed to it.
  • This method copies the values of actual parameters into the formal parameters
  • In this method ,any change in the formal parameter is not reflected back to the actual parameter.
9.What is meant by actual parameters and formal parameters?
  • Parameters associated with call statement is called actual parameters.
  • The parameters associated with function header is called formal parameter.
10. What is call by reference method in function?
  • In this method, formal parameters become alias to the actual parameters.
  • In call by reference method, any change made in the formal parameter is reflected back in the actual parameter.





11.What are the rules for actual parameters?
In Value Type
The actual parameters can be passed in the form of constants or variables or expressions to the formal parameters Ex. fun(a,b); or fun(10,5);
In Reference Type
The actual parameters can be passed only as variables to the formal parameters
Ex. X=fun(a,b)
12. What is meant by void function? (or) when the function can be defined as void? (or) Define returning values
  • The functions that return no value are declared as void.
  • The data type of a function is treated as int, if no data type is explicitly mentioned.
  • Default return value of a function in C++ is of type int.

13.Define default arguments.
  • The default value is given in the form of variable initialization.
  • The default arguments facilitate the function call statement with partial or no arguments.
  • The default values can be included in the function prototype form right to left,
14.State the return type for the following function prototype.
1. float power (float, int)
2. char choice ( )
3. char * success ( )
4. double fact (int)
Return type
1. float,
2. char,
3. pointer char and
4. double.
15.What is the output of the given program?
#include <iostream.h>
#include <conio.h>
void main ( )
{ int i = 5;
int &count = i;
cout<< “\n count: “<<count;
count ++;
cout<< “\ni: “<<i;
getch ( ); }
Output:
Count : 5
i = 6
16.Why is the variable i updated to 6, when count was incremented?
The reason being that the variables count and i refer to the same data in the memory. Reference variables also behave the same way.
17.Define Returning by reference.
  • A function returning a reference can appear on the left-hand side of an assignment.
  • The formal parameters for a reference function should always be of reference parameter type in the sense.
18.What is an inline function?
  • An inline looks like a normal function in the source file but inserts the function’s code directly into the calling program.
  • Inline functions execute faster but require more memory space.
19. How many types of variable scopes are there? What are they?
Scope refers to the accessibility of a variable. There are four types of scopes in C++.
They are:
1. Local scope, 2. Function scope, 3. File scope and 4. Class scope
20. What is file scope?
File scope
  • The file scope of variable declared above main ( ).
  • The scope of variable is to the entire program.
  • The life time of a file scope variable is the life time of a program.
21.Define return statement.
  • Return statement marks the end of the function
  • It transfers control to the statement after call statement.
  • The starting point for the execution of a program is main ( ).
22.What is local scope?
  • A local variable is defined within a block.
  • A local variable cannot be accessed from outside the block of its declaration.
  • Local variables are not known outside their own block.
  • A block of code begins and ends with curly braces { }.
  • Local variable is created upon entry into its block and destroyed upon exit
23.What is function scope?
Function scope
  • The function scope of variable declared within a function
  • The scope of variable is extended to the function block, and all sub-blocks.
  • The life time of a function scope variable, is the life time of the function block.
  • The scope of formal parameters is function scope
24.What is the use of scope operator?
  • :: is called scope resolution operator
  • It is used to refer variables declared at file level.
  • This is helpful only under situations where the local and file scope variables have the same name.
25.What are parameters?
  • Parameters are the channels through which data flows from the call statement to the function and vice versa.
26.Why do you think the prototype int max(int,int) is valid?
In a function declaration,
  • The names of the arguments are dummy variables and therefore they are optional.
  • The variables in the prototype act as place holders.






Book Exercises:
2.Identify errors in the following function prototypes:
Solution
a) The arguments must be declared with their data types.
Ex.float average (int a,int b);
b)Type of data missing for variable b
Ex. Float prd(int a,intb);
c)Default values cannot be given
Ex.int default-arg(int a,intb);
d) Default values cannot be given
Ex.int fun(int,int,double);
d) No error
3.Solution
void main()
{
clrscr();
int t=5;
char c=’a’;
line(t,c);
getch();
}
5)
Here ::val =10 ,val =1 so
Output is 101
6)Solution
  • Divide (400) yields a value 40
  • Divide (400) == 40 is interpreted as 40 ==40 since the condition is true val gets 1
Output is 1

No comments:

Post a Comment