Ch11 User - Defined Functions2016
Ch11 User - Defined Functions2016
1
8. When can a function prototype be omitted?
Ans: A function prototype can be omitted if the function definition comes before the function call statement. Or function is
defined before the use of the function.
9. Construct function prototypes for descriptions given below:
(i) Rarb() takes no arguments and has no return type: void Rarb(void);
(ii) mains() takes a float argument and return an int int mains( float);
(iii) san() takes two double arguments and return a double double san(double, double);
(iv) sum() takes an integer array and an an int value and long sum(int [], int);
returns a long result.
(v) check() takes a string argument and returns an int. int check(char []); or int check(char *);
10. What is the condition of using a function in an expression? When a function returns a value, the entire function call
can be assigned to a variable. True or false?
Ans: A function can be used in an expression if it returns a value.
Yes, when a function returns a value, the entire function call can be Eg. int check(char str[]); // function prototype.
int len=check(str); // function call statement assigned to a variable.
1. The scope of a local var. is the function/block The scope of a global variable
which declares it. is the entire program.
2. The life time is the function/block run. It remains in the memory till the program run.
3. Example:
#include<iostream.h>
int x; // variable x is global variable
void check()// global prototype
void main()
{ int x , y; // variable x ,y are local to main()
Void check() // local prototype
cout<<x<<y<< ::x ; // to access global variable x, we use ::
}
void check()
3
{ int z ; // variable z is local to check()
cout<<x<<z;// variable x is global so directly accessible here
}
24. Write a function having prototype : int replace(char* string, char ch1, char ch2); have the function replace every
occurrence of ch1 in the string with ch2, and have the function return number of replacements it makes.
int replace( char string, char ch1, char ch2)
{ int count=0;
for( int i=0; string[i]!=’\0’ ; i++)
{
if( string[i]==ch1)
{
string[i]=ch2;
count++;
}
}
return count;
}
25. Complete the following function power() by filling up the correct symbols/expressions/variable at place indicated
by -------. The function power() is declared as follows.
long power(int x, int n); The power( ) works as given below:
power( 0,n)=0 for each n
power(x,n)=xn if x!=0 and n>0
power (x,0)=1 for each x
The function definition is as follows:
long power( int x, int n)
long res=1;
if(x == 0 )
res=0;
else if( n > 0)
res=1;
else if (n ! = 0)
for(int i=0; i<n ; i++)
res*=x;
else
{ for( int j=0; j>=-n; i--)
res*=x;
res=1/res;
}
return res;
}
26. Write a C++ function to sum n natural numbers starting from a given number.
Ans: void sumnatural( int start, int n)
{ for(int i=1 , sum=0 ; i<=n ; i++)
{
sum+=start;
start++;
}
return sum;
}
27. Write a C++ function to find least common divisor of two integers.
Void LCM(int n1, int n2)
{ int small;
if(n1>n2) small=n2
else small=n1;
for(int i=small; i<=n1*n2; i++)
{ if(n1%i==0 && n2%i==0)
{ break; }
return i;
}
4
28. Write a C++ function that compares two strings and returns 0 if the two strings are equal and -1 if the strings are
unequal.
void compstrings( char str1, char str2)
{ if(strcmp( str1,str2)==0 )
return 0;
else return -1;
}
Type C: Long Answer questions:
1. Write a complete C++ program that reads a float array having 15 elements. The program uses a function reverse()
to reverse this array. Make suitable assumptions wherever required.
#include<iostream.h> void reverse(int arr[],int n)
#include<conio.h> {
void reverse(int arr[],int n); int i,j,temp;
void main() for(i=0,j=n-1;i<n/2;i++,j--)
{ {
int i,arr[40],n; temp=arr[i];
clrscr(); arr[i]=arr[j];
cout<<"How many numbers you want array with arr[j]=temp;
?:"; }
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Arr["<<" ]:"; cin>>arr[i];
}
cout<<"\nThe array before reverse is :\n";
for(i=0;i<n;i++)
cout<<arr[i]<<", ";
reverse(arr,n);
cout<<"\nThe array after reverse is :\n";
for(i=0;i<n;i++)
cout<<arr[i]<<", ";
getch();
}
2. Write a complete C++ program that invokes a function satis() to find whether four integers a, b, c, d sent to satis( )
satisfy the equation a3+b3+c3=d3 or not. The function satis() returns 0 if the above equation is satisfied with the
given four numbers otherwise it returns -1.
#include<iostream.h> int satis(int a,int b, int c, int d)
#include<conio.h> {
int satis(int a,int b, int c, int d); if( (pow(a,2)+pow(b,2)+pow(c,2)) ==
void mian() pow(d,2))
{ int A,B,C,D, ans; return 0;
clrscr(); else return -1
cout<<”Enter the 4 numbers :”; }
cin>>A>>B>>C>>D;
ans=satis(A,B,C,D);
if(ans== 0) cout<<”The equation is
satisfied”;
else cout<<”The equation is not
satisfied”;
getch();
}
3. Write a complete C++ program that uses a function called carea() to calculate area of a circle. The function carea()
receives the radius of type float type and returns area of double type. The function main() gets a radius value from
user, calls carea(), and displays the result. The function carea() is local to main().
5
#include<iostream.h> double carea(float r)
#include<conio.h> {
const float pi=3.14159; return (pi*r*r);
void main() }
{
double carea(float r);/// local to main()
float rad; double area;
clrscr();
cout<<"Enter the radius :"; cin>>rad;
area=carea(rad);
cout<<"The area of the circle is :"<<area;
getch();
}
4. Write a C++ program that uses a function smallo() that is passed two int argument by value to receive reference of
the smaller value. Then using this reference the smaller value is set to 0. Write a main() function also to exercise
this function.
#include<iostream.h> int & smallo(int &a, int &b )
#incldue<conoi.h> { if( a>b) return b;
int & smallo(int &,int &); else return a;
void main()
}
{
int x,y;
cout<<”enter the value of x and y:”;
cin>>x>>y;
smallo(x,y)=0;
cout<<the value of x=”<<x<<” and value
of y=”<<y;
}
5. Write a C++ program that uses following functions:
(i) sqlarge() that is passed two int arguments by reference and then sets the larger of the two numbers
to its square
(ii) sum() that is passed an int argument by value and that returns the sum of the individual digits of
the passed number.
(iii) main() that exercises above two functions by getting two integers from the user and by printing the sum of
the individual digits of the square of the larger number.
7. Write a program that uses a function power( ) to raise a number m to power n. The function takes integer values for
m and n and returns the result correctly. Use a default value of 2 for n and make the function calculate squares
when this argument is omitted. Write a main( ) to get the value of m and n from the user and display the result.
#include<iostream.h> long power(int m,int n)
#include<conio.h> {
long power(int m,int n=2); long p=1;
void main() for(int i=1;i<=n;i++)
{ {
int m,n;char ch; p*=m;
long ans; }
clrscr(); return p;
cout<<"Enter the number :";cin>>m; }
cout<<"Do you want to enter the power (y/n) :";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cout<<"Enter the power :";cin>>n;
}
if(ch=='y'||ch=='Y')
ans=power(m,n);
else
ans=power(m);
cout<<"The result is :"<<ans;
getch();
}
Theoretical questions for the chapter of Function
Note: The following questions are important for Class XI and XII(Board Exam)
Q.1 What is a function declaration? How a function declaration is different from function definition?
7
e.g. int sum(int x, int y)// function definition
int sum(int ,int);// function declaration
void main() {
{
int a,b,c; cin>>a>>b; return x+y;
c=sum(a,b);// function call
cout<<”The sum of A and B is =”<<c; }
}
Q.2 How is a global prototype/global variable different from a local prototype/local variable?
Ans: The Differences are:
Local Prototype/Local Variable Global Prototype/Global Variable
1. The scope of a local var. is the function/block The scope of a global variable is
which declares it. the entire program.
2. The life time is the function/block run. It remains in the memory till the program run.
3. Example:
#include<iostream.h>
int x; // variable x is global variable
void check()// global prototype
void main()
{ int x , y; // variable x ,y are local to main()
Void check() // local prototype
cout<<x<<y<< ::x ; // to access global variable x, we use ::
}
void check()
{ int z ; // variable z is local to check()
cout<<x<<z;// variable x is global so directly accessible here
}
Q.3 What do you mean by default arguments? When is a default argument value used inside a function?
Default arguments
1. Default arguments is the value(s) which compiler inserts automatically in case a matching argument(s) .
2. Default arguments is not passed in the function call statement.
3. The default values are specified in the function declaration.
4. All the arguments to the right side of the default argument must be default.
5. E.g. float interest(float p=1000, float t=5; float r=10); //function prototype with all the arguments
with default values.
Q.4 What do you mean by constant arguments.
3. The constant arguments can’t be modified by the function.
4. We can declare const argument by using a keyword const in function definition as well as function definition.
5. int sum( const int a[] , int n);
//function prototype with constant argument
8
3. The name of variables in Actual and formal Arguments may or may not be the same.
Example:
void interchange(int &,int& );
void main( )
{
int a=5,b=10 ;
cout<< ″ a= ″ <<a << ″ b= ″ <<b ;
change(a,b) ; // actual arguments are used in function call
cout<< ″ a= ″ <<a << ″ b= ″ <<b ;
}
void interchange(int x ,int y) // formal arguments are used in fun. Definition
{ int t=x;
x=y;
y=t;;
}
Q.6 How is call by value method of function invoking is different from call by reference method? Give appropriate example. Or
Differentiate between call by value & call by reference with a suitable example.
Ans. The Difference between Call by value and Call by reference are:
S.# Call by value Call by reference
1. The formal arguments are copy of actual arguments. The formal arguments are reference of actual
arguments.
2. It is a safe method of function call as changes in This is not safe as the changes are reflected on original
arguments are not reflected back. values.
3. void change(int ); void change(int &);
void main( ) void main( )
{ {
int a=5 ; int a=5 ;
cout<< ″ a= ″ <<a ; cout<< ″ a= ″ <<a ;
change(a) ; change(a) ;
cout<< ″\n a= ″<<a ; cout<< ″\n a= ″<<a ;
} }
void change(int b) void change(int &b)
{ b=10 ; } { b=10 ; }
Output will be : Output will be :
a=5; a=5;
a=5; a=10;
Q.7 What is the role of keyword void in declaring functions?
Ans: The key word void has the following uses
1. It specifies the return type of the function. E.g. void sum( );
2. It is also used to specify empty argument list. If the function does not accept any argument list
e.g.void sum(void);
1. Default arguments is the value(s) which compiler Constant arguments are arguments which can be modified
inserts automatically in case a matching argument(s) is by the function.
not passed in the function call statement..
2. The default values are specified in the function We can define constant arguments by adding a keyword
declaration. const in the function parameter list in the declaration as well
as function definition.
3. All the arguments to the right side of the default We can define constant arguments in any order i.e. no
argument must be default. restriction over the order of parameter list.
4. float interest(float p=1000, float t=5; float int sum( const int a[] , int n);
r=10); //function prototype with all the arguments
with default values. //function prototype with constant argument