2. FUNCTION??
y All languages have a construct to separate and package
blocks of code. C uses the "function" to package blocks
of code.
y A function is self-contained block of statements that
perform a coherent task of some kind.
3. Why Use Functions?
y Writing functions avoids rewriting the same code
over and over.
y By using functions it becomes easier to write
programs and keep track of what they are
doing(separate the code into modular units).
4. LOOK LIKE??
y A function has a name, a list of arguments which it
takes when called, and the block of code it executes
when called.
y #include<stdio.h>
void message(); /*function prototype declaration*/
void main()
{
message(); /*function call*/
printf(Dzn Friendsǥǥdz);
}
5. void message() /* function definition */
{
printf(Dzn Hiǥǥǥ.dz);
}
It is necessary to mention the prototype of every function that
we intend to define in the program.
A function can be called from another function, Ǯbut a function
cannot be defined in another functionǯ.
6. Communication??
y Between calling and called functions
y Mechanism used to convey information to the
function is the Ǯargumentǯ(parameter).
y Parameter:
y External value used for processing is called a parameter.
y Parameters are of two types:
i. Actual parameters
ii. Formal parameters
y Parameters can passed in two ways:
i. Pass by value
ii. Pass by reference
7. Types of parameters͙
Actual parameters: The expression passed to a function by its caller
is called the actual parametersdz
Formal parameters: The parameter storage local to the
function is called the formal parameter
8. Calling Convention͙
y Indicates two things:
i. The order in which the arguments are passed to the function
ii. Which function(calling or called)performs the cleanup of
variables when the control returns from the function
y When a function call is encountered and the arguments
are to be passed to a function two possibilities exist:
i. Arguments can be passed from left to right
ii. Arguments can be passed from right to left
‡ Most common calling convention is Dzstandard calling
conventiondz
10. Note:
y Always ǮCǯ uses positional correspondence in functions
while passing parameters
y Example:
y void f(int a, int b, int c)
{
printf(%d %d %ddz,a,b,c);
} z=x+y 7
main() y=z+5 12
{ x+y 15
int x=3,y=4,z=5;
f(x+y,y=z+5,z=x+y);
}
11. Pass by Value͙
y We pass Ǯvaluesǯ of variables to the Ǯcalledǯ function
y C passes parameters by value which means that the actual
parameter values are copied into local storage.
y The caller and callee functions do not share any memory(--
they each have their own copy)
y Examples:
y sum=area(a,b);
y f=fact(a);
Practice: Consider the following C function
int a=3;
void f(int a)
{
void g();
a=a+2;
printf(Dz%ddz,a);
g();
}
13. NOTE͙
y DzPass by Valuedz is more secure than DzPass by Referencedz because actual data is
not affected and a copy of it is maintained.
y Ex: void f(int i)
{
i=i+1;
}
main()
{
int i=5;
f(i);
printf(Dz%ddz,i);
}
Output: 5
Explanation: Here value of i passed as pass by value. So value of i local to
main is not changed.
14. Disadvantage!!!
This scheme is fine for many purposes, but it has two
disadvantages:
Because the callee has its own copy, modifications to
that memory are not communicated back to the
caller. Therefore, value parameters do not allow the
callee to communicate back to the caller.
Sometimes it is undesirable to copy the value from
the caller to the callee because the value is large and
so copying it is expensive
15. Solution͙͙.
The alternative is to pass the arguments by reference.
Instead of passing a copy of a value from the caller to
the callee, pass a pointer to the value.
In this way there is only one copy of the value at any
time, and the caller and callee both access that one
value through pointers.
16.
17. POINTERS???
Def.: A variable which stores the address of other
variable.
Represented by *
Also called Indirection operator
There are two pointers based on memory:
Near pointer(occupies 2bytes)
Far pointer(occupies 4bytes)
Pointer declaration is as ,
i location name
6 value at location
100 location number(address)
18. Types of pointers͙
y Single pointer(*)
Ex: int x=5; int *ptr; ptr=x;
y Double pointer(**)
Ex: int **pptr; pptr=ptr;
y Triple pointer(***)ǥǥǥ
y N pointer(*ǥǥǥ*)
y NULL pointer: A pointer which holds null value
Ex: int *ptr=NULL;
‡ WILD pointer: An uninitialized pointer
Ex: int *ptr;
char *p;
float *f;
19. DANGLING pointer: A pointer that points to an
address that doesnǯt exist.
Ex: n1 n2 n3 nodes
200 300 NULL
100 200 300
If we delete 200 ptr, then
n1 n3
200 NULL
100 300
20. CONSTANT pointer:
int *const ptr=variable;
FUNCTIONAL pointer:
int f; variable
int f(); function
int *f; pointer
int *f(); function
int (*f)(); functional pointer
21. SPECIALIZED pointers:
int *p;
float *fp;
char *cp;
long *lp;
GENERIC pointer: (void *)
void *vp;
NOTE::The arithmetic operations that are allowed on
pointers are +,-
24. Pass by Reference͙
y Here we pass the location number(also called
address) of the variable to a function.
y Example:
y void swap(int*,int*); //prototype
y Swap(a,b); //calling function
y Void swap(int *x,int *y){} //definition
26. Recursion?...
A function is said to be Ǯrecursiveǯ if a statement within
the body of a function calls the same function
(OR)
Recursion is the process of defining something in
terms of itself
Recursion internally uses stack
27. Example:
Q::What is the value of f(5)?
int f(int n)
{
static int r=0;
if(n=0)
return 1;
if(n3)
{
r=n;
return(f(n-1)+2);
}
return f(n-1)+r;
}
Output:: 18
32. #includestdio.h
int fun(int n, int *f_p)
{
int t, f;
if(n=1)
{
*f_p=1;
return 1;
}
t=fun(n-1,f_p);
f=t+*f_p;
*f_p=t;
return f;
}
int main()
{
int x=15;
printf(Dz%dndz,fun(5,x));
return 0;
}
The value printed is???
Output:: 8
34. Output of the following C program isǥǥǥǥǥǥ?
main()
{
char s[]={'a','b','c','n','c','0'};
char *p,*str,*str1;
p=s[3];
str=p;
str1=s;
printf(%d,++*p + ++*str1-32);
}
NOTE:ASCII value of n(10),0(0)
Output:: 77(ǮMǯ)
35. main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=a[2][2][2];
*q=***a;
printf(%d----%d,*p,*q);
}
36. y Output: some garbage valueǥǥ
y Explanation:p=a[2][2][2] you declared only two 2D
arrays, but you are trying to access the third 2D(which
you are not declared) it will print garbage values.
*q=***a ǥstarting address of Ǯaǯ is assigned to integer
pointer. Now q is pointing to starting address of a. If
you print *q, it will print first element of 3D array..
38. Output isǥǥǥ.?
main()
{
printf(%p,main);
}
Ans :Some address will be printed.
Explanation:Function names are just addresses (just like
array names are addresses). main() is also a function.
So the address of function main will be printed. %p in
printf specifies that the argument is an address. They
are printed as hexadecimal numbers..
39. main()
{
static char names[5][20]={pascal,ada,cobol,fortran,perl};
int i;
char *t;
t=names[3];
names[3]=names[4];
names[4]=t;
for (i=0;i=4;i++)
printf(%s,names[i]);
}
Ans ::Compiler error: Lvalue required in function main
Explanation::Array names are pointer constants. So it cannot be
modified..
40. Output for the following program is??????
main()
{
int i=-1;
+i;
printf(i = %d, +i = %d n,i,+i);
}
41. Ans : i = -1, +i = -1
Explanation:Unary + is the only dummy operator in C.
Where-ever it comes you can just ignore it just because
it has no effect in the expressions (hence the name
dummy operator)..
42. main()
{
char name[10],s[12];
scanf(Dz %[^],s);
}
How scanf will execute?
Ans :First it checks for the leading white space and
discards it. Then it matches with a quotation mark and
then it reads all character upto another quotation
mark..
43. Output of the following program isǥǥǥ?
void main()
{
char *ptr=*******;
int i;
clrscr();
for(i=0;i8;i++)
{
printf(%*.*sn,8,i,ptr);
}
getch();
}
45. y Explanation:
Meaning of %*.*s in the printf function:
-First * indicates the width i.e. how many spaces will
take to print the string.
-Second * indicates how many characters will print of
any string.
48. y Ans: che
y Explanation: Here
ptr: is pointer to array of pointer to string.
P1, p2, p3: are pointers to array of string.
array[3]: is array which contain pointer to array of string.
As we know p[i]=*(p+i)
(***ptr[0])[2]=(*(***ptr+0))[2]=(***ptr)[2]
=(***(array))[2] //ptr=array
=(**array)[2] //From rule *p=p
=(**(p1))[2] //array=p1
=(*p1)[2]
=(*s)[2] //p1=s
=s[2]=dzchedz
49. What will be output if you will compile and execute the following c code?
#includeconio.hDz
int display();
int(*array[3])();
int(*(*ptr)[3])();
void main()
{
array[0]=display;
array[1]=getch;
ptr=array;
printf(%d,(**ptr)());
(*(*ptr+1))();
}
int display()
{
int x=5;
return x++;
}
50. y Ans: 5
y Explanation:
In this example: array []: It is array of pointer to such function which
parameter is void and return type is int data type.
ptr: It is pointer to array which contents are pointer to such function which
parameter is void and return type is int type data.
(**ptr)() = (** (array)) () //ptr=array
= (*array) () // from rule *p=p
=array [0] () //from rule *(p+i)=p[i]
=display () //array[0]=display
(*(*ptr+1))() =(*(*array+1))() //ptr=array
=*(array+1) () // from rule *p=p
=array [1] () //from rule *(p+i)=p[i]
=getch () //array[1]=getch
53. y In C three continuous dots is known as ellipsis which is
variable number of arguments of function. In this
example ptr is generic pointer which is pointing to first
element of variable number of argument. After
incrementing it will point third element.
54. y What will be output if you will compile and execute the following
c code?
void main()
{
int i;
float a=5.2;
char *ptr;
ptr=(char *)a;
for(i=0;i=3;i++)
printf(%d ,*ptr++);
}
55. y Ans: 102 102 -90 64
y Explanation:
In c float data type is four byte data type while
char pointer ptr can point one byte of memory at a time.
ptr pointer will point first fourth byte then third byte then
second byte then first byte.
Content of fourth byte:
Binary value=01100110
Decimal value= 64+32+4+2=102
Content of third byte:
Binary value=01100110
Decimal value=64+32+4+2=102
56. Content of second byte:
Binary value=10100110
Decimal value=-128+32+4+2=-90
Content of first byte:
Binary value=01000000
Decimal value=64
y Note: Character pointer treats MSB bit of each byte
i.e. left most bit of above figure as sign bit.