5a)
#include<iostream.h>
int val=10;
divide(int);
void main()
{
int val=5;
val=divide(::val/val);
cout<<::val<<val;
}
divide(int v)
{
return(v/2);
}
Explanation:
val is declared as a global variable. A global variable is one which is declared outside main. The variable can be accessed from any function. val is again declared inside main as a local variable. local variables have precedence over global variables. The :: operator is called scope resolution operator. When the local and global variable have same name, the scope resolution operator is used, to tell the compiler that we are accessing the global variable.
The statement,
val=divide(::val/val);
can be understood as val=divide(10/5)
The argument passed to the function divide() is 2. it returns 1. The value stored in val is 1 and the value stored in ::val is 10..
Output:
101

#include<iostream.h>
divide(int v)
{
return v/10;
}
void main()
{
int val=-1;
val=divide(400)==40;
cout<<"\nVal:"<<val;
}
Explanation
Let’s separate the statement val=divide(400)==40;
There is a function call in this statement with argument 400. Inside the function it gets divided by 10 and returned. The value returned by the function is 40.
Now, our statement becomes
val=40==40.
40==40 The equality operator is used here. Remember 1 is returned when any condition is true and 0 is returned when any condition is false. Since the condition is true, 1 gets stored in val.
Output:
Val:1

#include<iostream.h>
int incre(int a)
{
return a++;
}
void main()
{
int x=10;
x=incre(x);cout<<x;
}
Explanation:
10 is passed as argument to the incre() function. Postfix operation is used to increment and returned. When postfix is used the value is returned before getting incremented. Therefore, the value stored in x is 10.
Output:
10.

#include<iostream.h>
#include<conio.h>
void line()
{
static int v=5;
int x=v--;
while(x)
{
cout<<'*';x--;
}
cout<<"\n";
}
void main()
{
clrscr();
for(int i=1;i<=5;i++)
line();
getch();
}
Explanation:
The function line() gets called each time the loop is executed. The loop is executed 5 times.
Iteration 1:
v is declared as static int inside the function line(). So, it retains its value when the function is called during successive calls. Initially the value of v is 5.
x stores the value in v and then v is incremented. The while loop executes till the value stored in x != 0. Therefore * is printed 5 times.
*****
The control returns to main.
Iteration 2:
v=4. x=4
Loop executes 4 times.
****
Iteration 3:
v=3. x=3
Loop executes 3 times.
***
Iteration 4:
v=2 x=2
Loop executes 2 times.
**
Iteration 5:
v=1 x=1
Loop executes 1 time.
*
i=6. The for loop condition is violated. Loop terminates.
Output:
*****
****
***
**
*

#include<iostream.h>
first(int i)
{
return i++;
}
second(int x)
{
return x--;
}
void main()
{
int val=50;
val=val*val/val;
val=second(val);
val=first(val);
cout<<"\nVal:"<<val;
}
Explanation:
val=val*val/val. 50 gets stored in val.
The second() function is called. val is passed as argument. Postfix decrement is used in the return statement. But x gets returned before getting decremented. So 50 gets stored in val. The first() function is called with val as argument. Postfix increment is used in the return statement. i gets returned before it gets incremented. So, again 50 gets stored in val.
Output:
Val:50
