0% found this document useful (0 votes)
2 views9 pages

Exp 5

This document outlines an experiment focused on creating a program to calculate the area of various shapes using functions in C programming. It explains the theory behind functions, including their declaration, calling, and definition, as well as concepts like nested functions and recursion. Additionally, it provides sample programs and questions related to the use of functions in C.

Uploaded by

dubemeera714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Exp 5

This document outlines an experiment focused on creating a program to calculate the area of various shapes using functions in C programming. It explains the theory behind functions, including their declaration, calling, and definition, as well as concepts like nested functions and recursion. Additionally, it provides sample programs and questions related to the use of functions in C.

Uploaded by

dubemeera714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Experiment No.

Title: Program for calculating area of circle, triangle, rectangle, square by passing
argument to a function.

Outcome: Student will able to handle problem statement by passing argument to


functions.

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.

Three things are necessary while using a function:

1. Declaring a function or prototype:

The general structure of a function declaration is as follows:

return_type function_name(arguments);

Before defining a function, it is required to declare the function i.e. to specify


the function prototype. A function declaration is followed by a semicolon ; . Unlike the
function definition only data type are to be mentioned for arguments in the function
declaration.

2. Calling a function:

The function call is made as follows:

function_name(arguments separated by comma);

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:

• Functions that take nothing as argument and return nothing.


• Functions that take arguments but return nothing.
• Functions that do not take arguments but return something.
• Functions that take arguments and return something.
A function that returns nothing must have the return type “void”.
Functions inside Functions (Nested function):

Some programmer thinks that defining a function inside an another function is


known as “nested function”.
The nested function's name is local to the block where it is defined. For example,
here we define a nested function named square, and call it twice:
1. foo (double a, double b)
2. {
3. double square (double z) { return z * z; }
4. return square (a) + square (b);
5. }
The nested function can access all the variables of the containing function that are visible
at the point of its definition. This is
called lexical scoping.

Recursion:
A function that calls itself is known as a
recursive function. And, this technique
is known as recursion.

List of similar programs: Assignment No. 05


1. Write a C program to demonstrate calculator using function. Use one category of
function for one operation of calculator.
2. Write a C program to display L of ‘*’ using function.
3. Write a C program to demonstrate nested function
(Hint: Area of rectangle=multiplication of two numbers)
4. Write a C program to print first 10 numbers of fibonacii series using recursion.
5. Write a C program to find factorial of number using 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.

1.Write a program to demonstrate calculator using function. Use one category of


function for one operation of calculator.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
float cal(float a,float b,char ope);

void main()
{
float a,b;
char ope;

printf(“Enter two numbers:”);


scanf(“ %f%f”,&a,&b);
printf(“Enter operator:”);
scanf(“ %c”,&ope);

cal(a,b,ope);

float cal(float a,float b,char 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);
}

char L(int I,int j)


{
for(i=1;i<=7;i++)
{
for(j=1;j<=7;j++)
{
if(j==1 ||i==7)
{
printf(“*”,j);
}
else
{
}
}

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));

int rectangle_area(int l,int b)


{
return (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;

printf(“Enter the number:\n “);


scanf(“%d”,&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);

printf(“Factorial of the given number\n:%d”, factorial(n));


return 0;
}

int factorial (int n)


{
if(n==0)
{
return 1;
}
else
{
return (n* factorial(n-1));
}
}

OUTPUT:

Conclusion:

You might also like