UNIT IV

1. What are functions? Explain the types of functions in detail with an example program for each type.

A function is a self-contained block or a sub program of one or more statements

that performs a special task when called.

Types:

· Library Functions

· User Defined functions

(a) Function Declaration

returntype function-name(Parameters);

Example:

int square(int, int);

(b) Function calling

function-name(actual parameters);

Example:

int square(a,b);

(c) Function Definition:

returntype function-name(formal parameters)

{

}

Example:

local variable declaration;

statement 1; statement 2; return(value);

void square(int a, int b)

{

printf(“%d”,(a*b));

}

2. What are the predefined or inbuilt functions available in C?

sqrt(x):

It is used to find the square root of x

Example: sqrt(36) is 6

abs(x):

It is used to find the absolute value of x

Example: abs(-36) is 36

pow(x,y):

It is used to find the value of xy

Example: pow(5,2) is 25

ceil(x):

It is used to find the smallest integer greater than or equal to x

rand():

It is used to generate a random number.

sin(x):

It is used to find the sine value of x

Example: sin(30) is 0.5

cos(x):

It is used to find the cosine value of x

Example: cos(30) is 0.86

tan(x):

It is used to find the tan value of x

Example: tan(30) is 0.577

toascii(x):

It is used to find the ASCII value of x

Example: toascii(a) is 97

toupper(x):

It is used to convert lowercase character to uppercase.

Example: toupper(‘a’) is A

toupper(97) is A

tolower(x):

It is used to convert uppercase character to lowercase.

Example: tolower(‘A’) is a

#include<stdio.h

#include<conio.h

#include<math.h

#include<ctype.h

void main()

{

int x,y=2;

printf("\nEnter the number:");

scanf("%d",&x);

printf("\nThe squareroot of %d is %f",x,sqrt(x));

printf("\nThe value of %d power%dis%f ",x,y,pow(6,2));

printf("\nThe ceiling of 6.7 is %f",ceil(6.7));

printf("\nThe floor of 6.7 is %f",floor(6.7));

printf("\nThe absolute value of -6 is %d",abs(-6));

printf("\nThe value of sin 45 is %f",sin(45));

printf("\nThe uppercase of 'a' is %c",toupper('a'));

printf("\nThe uppercase of 97 is %c",toupper(97));

getch();

}

Enter the number:6

The squareroot of 6 is 2.449490

The value of 6 power 2 is 36.000000

The ceiling of 6.7 is 7.000000

The floor of 6.7 is 6.000000

The absolute value of -6 is 6

The value of sin 45 is 0.850904

The uppercase of 'a' is A

The uppercase of 97 is A

3. What are pointers? When and why they are used? Explain in detail with sample programs. (JAN 2009/MAY 2009)

Pointer variable is needed to store the memory address of any variable. Denoted by

(*) asterisk. Pointer is a variable that contains the memory address of another variable.

Example:

int *a;

#include<stdio.h

#include<conio.h

void main()

{

int x=5;

printf("\n The Address of x = %u",&x);

printf("\n The Value of x = %d",x);

}

Output

The Address of x = 8714

The Value of x = 5

•  Syntax

data-type *pointer-name;

data-type - Type of the data to which the pointer points.

pointer-name - Name of the pointer

•  Example: int *a;

· Pointers and Arrays

•  The elements of the array can also be accessed through a pointer.

•  Example

int a[3]={2,3,7};

int *b;

b=a;

#include<stdio.h

#include<conio.h

void main()

{

int a[3]={2,3,7};

int *b;

b=a;

printf("\n The Value of a[0] = %d",a[0]);

printf("\n The Address of a[0] = %u",&a[0]);

printf("\n The Value of b = %d",b);

}

Output

The Value of a[0] = 2

The Address of a[0] = 8744

The Value of b = 8744

· Pointer as function arguments

#include <stdio.h

#include<conio.h

void main()

{

int x,y,change(int*,int*);

printf("\nEnter value of x:");

scanf("%d",&x);

printf("\nEnter value of y:");

scanf("%d",&y);

change(x,&y);

printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);

}

int change(int *a,int *b)

{

int c;

c=*a;

*a=*b;

*b=c;

printf("\nValues in the Function -->x=%d,y=%d",*a,*b);

}

Output:

Enter value of x:5

Enter value of y:6

Values in the Function -->x=6,y=5

Values in the Main()-->x=6,y=5

· Pointer to pointer

•  Here one pointer stores the address of another pointer variable.

•  Example:

int x=10,*a,**b;

a=&x;

b=&a;

#include<stdio.h

#include<conio.h

void main()

{

int a=10;

int *b,**c;

b=&a;

c=&b;

printf("\n The Value of a = %d",a);

printf("\n The Address of a = %u",&a);

printf("\n The Value of b = %d",b);

printf("\n The Address of b = %u",&b);

printf("\n The Value of c = %d",c);

printf("\n The Address of c = %u",&c);

}

Output

The Value of a = 10

The Address of a = 5001

The Value of b = 5001

The Address of b = 8000

The Value of c = 8000

The Address of c = 9000

4. Brief call by value and call by reference in detail. (MAY 2009)

Call by value:

In call by value the value of the actual arguments are passed to the formal arguments and the operation is done on formal arguments.

Example program:

· To send two integer values using “call by value”.

#include <stdio.h

#include<conio.h

void main()

{

int x,y;

int change(int*,int*);

printf("\nEnter value of x:");

scanf("%d",&x);

printf("\nEnter value of y:");

scanf("%d",&y);

change(x,y);

printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);

}

int change(int a,int b)

{

int c;

c=a;

a=b;

b=c;

printf("\nValues in the Fuction -->x=%d,y=%d",a,b);

}

Output

Enter value of x:5

Enter value of y:6

Values in the Fuction -->x=6,y=5

Values in the Main()-->x=5,y=6

Call by reference:

In call by reference the address of actual argument values are passed to formal argument values.

Example program:

· To send a value by reference to user defined function.

#include <stdio.h

#include<conio.h

void main()

{

int x,y;

int change(int*,int*);

printf("\nEnter value of x:");

scanf("%d",&x);

printf("\nEnter value of y:");

scanf("%d",&y);

change(x,&y);

printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);

}

int change(int *a,int *b)

{

int c;

c=*a;

*a=*b;

*b=c;

printf("\nValues in the Function -->x=%d,y=%d",*a,*b);

}

Output:

Enter value of x:5

Enter value of y:6

Values in the Function -->x=6,y=5

Values in the Main()-->x=6,y=5

]

5. Discuss about function prototypes in detail. (or)

Explain about the different parameter passing methods with examples (JAN 2009)

· Function with arguments and return type.

•  Here data transfer take place between the calling function and the called function as well as between called function and calling function .

•  It is a two way data communication, i.e. the called program receives data from calling program and it return some value to the calling program.

• 

#include <stdio.h

#include<conio.h

void main()

{

int a,b,c;

int add(int,int);

printf("\nEnter two number:");

scanf("%d%d",&a,&b);

c=add(a,b);

printf("\nSum is:%d",c);

}

int add(int x,int y)

{

int z;

z=x+y;

return(z);

}

Output

Enter two number:6

7

Sum is:13

· Function without arguments and return type.

•  Here data transfer take place between the called function and the calling function.

•  It is a one way data communication, i.e. the called program does not receives data from calling program but it return some value to the calling program.

• 

#include <stdio.h

#include<conio.h

void main()

{

int add(),d;

d=add();

printf("\nSum is:%d",d);

}

int add() //function wit no argument

{ int a,b,c;

printf("\nEnter two number:");

scanf("%d%d",&a,&b);

c=a+b;

return(c);

}

Output

Enter two number:5

8

Sum is:13

· Function with arguments and no return type.

•  Here data transfer take place between the calling function and the called function.

It is a one way data communication, i.e. the called program receives data from calling program but it does not return any value to the calling program.

#include <stdio.h

#include<conio.h

void main()

{

int a,b;

void add(int,int);

printf("\nEnter two number:");

scanf("%d%d",&a,&b);

add(a,b);

}

void add(int x,int y) //function with arguments

{

int z;

z=x+y;

printf("\nSum is:%d",z);

}

Output

Enter two number:2

4

Sum is:6

Function with no arguments and no return values

•  Here no data transfer take place between the calling function and the called function.

•  These functions act independently, i.e. they get input and display output in the same block.

#include <stdio.h

#include<conio.h

void main() //calling function

{

void add();

add();

}

void add() //called function

{

int a,b,c;

printf("\nEnter two number:");

scanf("%d%d",&a,&b);

c=a+b;

printf("\nSum is:%d",c);

}

Output

Enter two number:3

4

Sum is:7

6. What is a recursive function? Write a c program to find factorial of n number using recursive function.

•  It is a process of calling the same function itself again and again until some condition is satisfied.

•  Syntax:

func1()

{

………..

func1();

}

#include<stdio.h

#include<conio.h

void main()

{

int a;

int rec(int);

printf("\nEnter the number:");

scanf("%d",&a);

printf("The factorial of %d! is %d",a,rec(a));

}

int rec(int x)

{

int f;

if(x==1)

return(1);

else

f=x*rec(x-1);

return(f);

}

Output:

Enter the number:5

The factorial of 5! is 120

8. Write a program in c for Tower of Hanoi using recursive function.

#include<stdio.h

#include<conio.h

void TOH(int n,char x,char y,char z);

void main() {

int n;

printf("\nEnter number of plates:");

scanf("%d",&n);

TOH(n,'A','B','C');

getch();

}

void TOH(int n,char x,char y,char z) {

if(n>0)

{

TOH(n-1,x,z,y); // Recursive call 1

printf("\n%c -> %c",x,y);

TOH(n-1,z,y,x); // Recursive call 2

}

}