Exp 5
Exp 5
Title: Program for calculating area of circle, triangle, rectangle, square by passing
argument to a function.
Theory:
Functions are used normally in those programs where some specific work is
required to be done repeatedly and looping fails to do the same.
return_type function_name(arguments);
2. Calling a function:
3. Defining a function:
All the statements or the operations to be performed by a function are given in the
function definition which is normally given at the end of the program outside the
main.
Function is defined as follows:
return_type function_name(arguments)
{
Statements;
}
There are four types of functions depending on the return type and arguments:
Recursion:
A function that calls itself is known as a
recursive function. And, this technique
is known as recursion.
Program:
Analysis :
1.
2.
3.
4.
5.
List of questions:
1. Explain need for functions in c programming.
2. Explain functions with different return types and arguments.
3. Differentiate between recursion and nesting of function.
4. What is difference between function declaration and function definition?
5. What are different types of return statement? Explain with importance.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
float cal(float a,float b,char ope);
void main()
{
float a,b;
char ope;
cal(a,b,ope);
if(ope==’+’)
{
printf(“Addition=%f”,a+b);
}
else if(ope==’-‘)
{
printf(“Substraction=%f”,a-b);
}
else if(ope==’*’)
{
printf(“Multiplication=%f”,a*b);
}
else if(ope==’/’)
{
printf(“Division=%f”,a/b);
}
else
{
printf(“Invalid Operator.Please enter the correct operator.”);
}
OUTPUT:
2.Write a C program to display L of ‘*’ using function.
SOURCE CODE:
#include<stdio.h>
char l(int I,int j);
int main()
{
int I,j;
l(I,j);
}
printf(“\n”);
}
OUTPUT:
3.Write a C program to demonstrate nested function
(Hint: Area of rectangle=multiplication of two numbers)
SOURCE CODE:
#include<stdio.h>
int rectangle_area(int l,int b);
int main()
{
int l,b;
printf(“Enter the length of rectangle:”);
scanf(“%d”,&l);
printf(“Enter the breadth of rectangle:”);
scanf(“%d”,&b);
printf(“Area of rectangle:%d”, rectangle_area(l,b));
OUTPUT:
4.Write a C program to print first 10 numbers of fibonacii series using recurs
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int fib(int);
int main()
{
int I;
int n;
for(i=0;i<n;i++)
{
printf(“%d\t”,fib(i));
}
return 0;
}
int fib(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return fib(n-1)+fib(n-2);
}
OUTPUT:
5.Write a C program to find factorial of number using recursion.
SOURCE CODE:
#include<stdio.h>
int factorial (int n);
int main()
{
int n;
printf(“Enter the number:\n”);
scanf(“%d”,&n);
OUTPUT:
Conclusion: