Ans of C Lab Work 6 : User Defined Function

Theory of Functions
Function: A self -contained block of code that performs a particular task is called function. Advantages of Function: 1. Function increases code reusability. 2. The length of the source program can be reduced by using functions at appropriate places. 3. The program development will be faster. 4. The program debugging will be easier. 5. Large number of programmers can be involved. 6. The program can be developed in a short period of time. 7. The program can be developed in different places. 8. The program can be tested and compiled independently by different members of a programming team. A function has 3 elements. (a) Function Declaration (Function Prototype) (b) Function Call (c) Function Definition a) Function Declaration (Function Prototype): A function declaration also known as Function Prototype consists of four parts: - Function type(return type) - Function name - Parameter list - Terminating Semicolon Syntax: Function_type Function_name(Formal Parameter list) Example:

int sum(int a, int b);

or

int sum(int, int);

Points to note:

  1. The parameter list must be separated by commas.

  2. The parameter names do not need to be the same in the prototype declaration and the function definition.

  3. The types must match the types of parameters in the function definition, in number and order.

  4. Use of parameter names in the declaration is optional.

 

Function Parameter:

The parameters used in function declaration and function definition are called formal parameters and those used in function call are called actual  parameters.

The formal and actual parameters must match exactly in type, order and number. Their names, however, do not need to match.

 

Examples: sum=add (a, b, c); // here a, b, c are called actual parameters.

         int add(int x , int y, int z); // here x, y, z are formal parameters.

 

Function Return: Function can be organized into 2 types.

a) Function that has not a return value i.e. void function.

b) Function that has a returned value i.e. int function.

 

b) Function Call

A function can be called by simply using the function name followed by a list of actual parameters, if any, enclosed in parenthesis.

Static variables are created and initialized once, on the first call to the function. Subsequent calls to the function do not recreate or re-initialize the static variable.

Example:

c=sum (a,b);

3. Function Definition

The function definition is an independent program module that is specially written to implement the requirement of the function.


A function definition, also known as function implementation shall include the following elements.

  1. Function type

  2. Function Name

  3. List of parameters

  4. Local variable declarations

  5. Function statements

  6. A return statement

 

All the six elements are grouped into two parts, namely,

  • Function Header(First three elements)

  • Function Body ( Second three elements)

Syntax:

Function_type Function_Name(Formal Parameter list)

{

Local variable declaration;

Executable statement 1;

Executable statement 2;

………………….

………………….

return statement;

}

Note that a semicolon is not used at the end of the Function Header of Function definition.


One program per A4 page [ Write in Single Side ONLY]
It should be in Handwritten form
1. Write a program to find the simple interest using function.

C program to find the simple interest using function

#include<stdio.h> int si(int p,int t,int r); main() { int p,t,r,ans; printf("Enter principal , time and rate "); scanf("%d%d%d",&p,&t,&r); ans=si(p,t,r); printf("Simple interest is %d",ans); } int si(int p,int t, int r) { int i; i=(p*t*r)/100; return (i); }

Output

Enter principal , time and rate 1000 5 10 Simple interest is 500
2. Write a program to find the greatest number among four different numbers using function.

C program to find the greatest number among four different numbers using function

#include<stdio.h> int max(int a,int b,int c,int d); main() { int a,b,c,d,ans; printf("Enter four numbers "); scanf("%d%d%d%d",&a,&b,&c,&d); ans=max(a,b,c,d); printf("Greatest number is %d",ans); } int max(int a,int b, int c,int d) { if(a>b&&a>c&&a>d) return (a); else if(b>a&&b>c&&b>d) return(b); else if(c>a&&c>b&&c>d) return (c); else return (d); }

Output

Enter four numbers 1 2 3 4 Greatest number is 4
3. Write a program to display the multiplication table of given number.

C program to display the multiplication table of given number

#include<stdio.h> void mtable( int n); main() { int n; printf("Enter any number : "); scanf("%d",&n); mtable(n); } void mtable(int n) { int i; for(i=1;i<=10;i++) printf("%d*%d=%d\n",n,i,n*i); }

Output

Enter any number : 3 3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 3*10=30
4. Write a program using a user defined function to calculate y raise power to x.

C program using a user defined function to calculate y raise power to x

#include<stdio.h> int power( int x, int y); main() { int x,y; printf(" The value of x : "); scanf("%d",&x); printf(" The value of y : "); scanf("%d",&y); printf(" The result is %d ",power(x,y)); } int power(int x, int y) { int p=1,i; for(i=1;i<=y;i++) { p=p*x; } return (p); }

Output

The value of x : 2 The value of y : 3 The result is 8
5. Write a program to find factorial of a number using recursion.

C program to find factorial of a number using recursion

#include<stdio.h> int fact(int); main() { int n,ans; printf( " Enter any number : "); scanf("%d",&n); ans=fact(n); printf(" Factorial of %d is %d ",n,ans); } int fact(int n) { if(n<=1) return (1); else return(n*fact(n-1)); }

Output

Enter any number : 5 Factorial of 5 is 120
6. Write a program to display the Fibonacci series upto nthusing recursion.

C program to display the Fibonacci series upto nth using recursion.

#include<stdio.h> int fibo(int n); main() { int i,n; printf( " Enter any number : "); scanf("%d",&n); for(i=1;i<=n;i++) { printf(" %d\t ",fibo(i)); } } int fibo(int n) { if(n==0) return 0; else if(n==1) return 1; else return (fibo(n-1)+fibo(n-2)); }

Output

Enter any number : 5 1 1 2 3 5
7. Write a program to find sum and average of n numbers using array and function.

C program to find sum and average of n numbers using array and function.

#include<stdio.h> void cal (int num[], int n); main() { int num[100],n,i; printf("Enter array size : "); scanf("%d",&n); for(i=0;i<n;i++) { printf(" Enter num[%d] : ",i+1); scanf("%d",&num[i]); } cal(num,n); } void cal(int num[], int n) { int i,sum=0,avg=0; for(i=0;i<n;i++) { sum=sum+num[i]; } avg=sum/n; printf(" Sum is %d\n ",sum); printf(" Average is %d ",avg); }

Output

Enter array size : 5 Enter num[1] : 10 Enter num[2] : 20 Enter num[3] : 30 Enter num[4] : 40 Enter num[5] : 50 Sum is 150 Average is 30
8. Write a program to print the greatest number among n numbers using array and function.

C program to print the greatest number among n numbers using array and function.

#include<stdio.h> int max(int num[], int n); main() { int num[100],n,i,ans; printf("Enter array size : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter num[%d] : ",i+1); scanf("%d",&num[i]); } ans=max(num,n); printf("Greatest number is %d ",ans); } int max(int num[], int n) { int i,m=num[0]; for(i=0;i<n;i++) { if(num[i]>m) m=num[i]; } return(m); }

Output

Enter array size : 5 Enter num[1] : 56 Enter num[2] : 2 Enter num[3] : 3 Enter num[4] : 153 Enter num[5] : 492 Greatest number is 492
9. Write a program to print transpose of matrix ( 2 x 2 type) using array and function.

C program to print transpose of matrix ( 2 x 2 type) using array and function

#include<stdio.h> void transpose(int a[][2]); main() { int a[2][2],i,j; for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("Enter a[%d][%d] : ",i,j); scanf("%d",&a[i][j]); } } transpose(a); } void transpose(int a[][2]) { int i,j; printf("\n Transpose of 2x2 matrix: \n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf(" %d\t ",a[j][i]); } printf("\n"); } }

Output

Enter a[0][0] : 1 Enter a[0][1] : 2 Enter a[1][0] : 3 Enter a[1][1] : 4 Transpose of 2x2 matrix: 1 3 2 4
10. Write a program to print sum of two matrices (2x2 type) using array and function.

C program to print sum of two matrices (2x2 type) using array and function

#include<stdio.h> void sum(int m1[][2],int m2[][2]); main() { int m1[2][2],m2[2][2],i,j; for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("Enter m1[%d][%d] : ",i,j); scanf("%d",&m1[i][j]); } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("Enter m2[%d][%d] : ",i,j); scanf("%d",&m2[i][j]); } } sum(m1,m2); } void sum(int m1[][2],int m2[][2]) { int m3[2][2],i,j; printf("\n sum of two 2x2 matrices: \n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { m3[i][j]=m1[i][j]+m2[i][j]; printf(" %d\t ",m3[i][j]); } printf("\n"); } }

Output

Enter m1[0][0] : 1 Enter m1[0][1] : 2 Enter m1[1][0] : 3 Enter m1[1][1] : 4 Enter m2[0][0] : 5 Enter m2[0][1] : 6 Enter m2[1][0] : 7 Enter m2[1][1] : 8 sum of two 3x3 matrices: 6 8 10 12