Sunteți pe pagina 1din 2

Chapter -11

User Defined Functions

Q1. What is difference between call by Value and Call by Reference? Give an
example in c++ to illustrate both.

Call by Value Call By Reference


In call by value method, the called In call by reference method, the called
function creates its own copies of the function accesses and works with the
original values sent to it. Any changes , original values using their references.
that are made, occur on the called Any changes, that occur, take place on
function’s copy of values and are not the original values and are reflected
reflected back to the calling function. back to the calling code.
void example(int a,int &b) // a is passed by using call by value and b is passed by
reference
{
a=a+5;
b=b*4; cout<<a<<”:”<<b;
}
void main()
{ int x=9,y=6;
cout<<x<<y;
example(x,y);
cout<<x<<y;
}

Q2. What is difference between Global Variable and Local Variable?

Global Variable Local Variable


• It is a variable, which is declared • It is a variable, which is declared
outside all the function definitions. within a function or within a
• It is accessible throughout the compound statement.
program. • It is accessible only within a
function /compound statement in
which it is declared.
#include<iostream.h>
float num=900; // num is a global variable
void Local(int T)
{
int Total =0;//Total is local variable
Total+=T;
cout<<num+Total;
}
void main()
{ Local(45); }
Q3. What is difference between Actual Parameters and Formal Parameters? Give an
example in C++ to illustrate both types of parameters.

Actual Parameter Formal Parameter


A parameter used in a function call is A parameter used in a function
known as actual parameter. It is used to definition is known as formal parameter.
send data to function. It is used to accept data from actual
parameters.
Void abc(int a) // a is formal parameter
{
Int x=9;
x=x+a;
}
Void main()
{
Int p=6;
abc(p); } // p is actual parameter

Q4. What do you mean by function prototyping? Write down the advantages of
function prototypes in C++?

Ans. Function prototyping means writing the function prototypes before their
function call occurs. A function prototype is a declaration of the function tells the
program about the type of the value returned by the function and the number and
type of arguments. The advantage of function prototyping is that it enables a
compiler to compare each use of function with the prototype to determine whether
the function is invoked properly or not. The number and types of arguments can be
easily compared and any wrong number of type of the argument is reported.
Therefore, function prototyping makes C++ straightaway point to the error.

Q5. How is global prototype different from a local prototype?

Ans. If a function prototype is placed in the body of another function, it is local


prototype and the function is locally available to the function that declares it.

If a function prototype is placed outside all the functions, it is global prototype and
the function is globally available to all the functions.

S-ar putea să vă placă și