You are on page 1of 5

Computer Programming(CSI101)Session : Winter 2019-20

II Common Section C Date : 27.01.2020

Tutorial-4 (Functions)
1. Find the output of following program.
#include<stdio.h>
int fun(int a, int b)
{
int a=0;
a=a+b;
b=a-b;
a=a-b;
return a;
}
int main()
{
int a=10, b=20 , c;
c = fun(b,a);
printf(“%d”,c);
return 0;
}
Hint:-
Output: Error (‘a’ re-declared as different kind of symbol.)

2. Find the output of the following program.


#include<stdio.h>
int fun(int a, int b)
{
a=0;
a=a^ b;
b=a^ b;
a=a^ b;
return b;
}
int main()
{
int a=10, b=20, c;
c=fun(b ,a);
printf(“%d %d %d”, c ,a, b);
}
Hint:- use the concept of call by value.
Output: 0 10 20
3. Find the output of the following program.
#include<stdio.h>
int fun( int x)
{
x>10? return (1): return(0);
}
int main()
{
int x=20, y;
y= fun(x);
printf(“%d”, y);
return 0;
}
Hint:- return statement cannot be used as shown with the conditional operator;
instead the following statement can be use .
return(x>10? 1: 0)
Output: Error (Expected expression before ‘return’.)

4. Predict the output of the questions given below.


#include<stdio.h>
int main()
{
int x=10;
if(x<0)
printf(“%d”, x+10);
else
{
x=x-10;
printf(“%d”, x);
main();
}
return 0;
}
Hint :- main() function can call itself in a program.
Output: 0000000……..(infinite loop).

5. Print the output of the program given below


#include<stdio.h>
void fun1(int n)
{
if(n==0)
return;
printf(“%d”, n);
fun2(n-2);
printf(“%d”,n);
}
void fun2(int n)
{
if(n==0)
return ;
printf(“%d”, n);
fun1(++n);
printf(“%d”, n);
}
int main()
{
int n=3;
fun1(n);
return 0;
}
HInt:- use the concept of call by value.
Output: 312223

6. What is the output of the program given below?


#include<stdio.h>
int fun();
int i;
int main()
{
while(i) {
fun();
main();
}
Printf(“IIT”);
return 0;
}
int fun()
{
printf(“ISM”);
return 0;
}
Hint:- The global variables are initialized with value zero.
Output: IIT

7. Find the output of the following program


#include<stdio.h>
void fun(int n)
{
if(n<=1)
printf(“%d”, n);
else {
fun(n/2);
printf(“%d”,n%2); } }
void fun1(int n)
{
if(n/2)
fun(n/2);
printf(“%d”,n%2);
}
int main()
{
int x=10;
fun(x);
printf(“\n”);
fun1(x);
return 0;
}
Hint:- Use the concept of call by value.
Output: 1010
1010

8. Find the output of following program.


#include<stdio.h>
int fun(int , int);
int main()
{
int a=10, b=10*2, c;
printf(“%d”, fun(a, b));
return 0;
}
int fun(int x, int y)
{
return (x-(x==y));
}
Hint:-The expression ‘x==y’ will return the value 1 if x is equal to y else 0.
Output: 10

9. Given the size of float is 4, Find the output of the following program.
#include<stdio.h>
int returns(int returns)
{
returns+=5.60;
return(returns);
}
int main()
{
int n=returns(sizeof(float));
printf(“%d”, n);
return 0; }
Hint:- Use the concept of call by value.
Output: 9

10. Find the output of the following program.


#include<stdio.h>
int _a_123(int n)
{
return (n++);
}
int main()
{
int n=_a_123(4);
printf(“%d”,--n);
return 0;
}
Hint:-Variable name starting with ‘_’ is allowed.
Output: 3

You might also like