0% found this document useful (0 votes)
9 views4 pages

Index 2 (Udf Other 3 Types)

Uploaded by

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

Index 2 (Udf Other 3 Types)

Uploaded by

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

INDEX 1B

DATE: LAB REPORT NO.: 1 SET: B


TITLE OF THE PROGRAM: USER-DEFINED FUNCTIONS IN C

OBJECTIVES
Area Calculation of a Rectangle using different types of User-defined Functions
I. To demonstrate the use of user-defined functions in C programming.
II. To calculate and display the area of a rectangle using different types of user-defined
functions.
III. To understand the concepts of return values and arguments in user-defined functions.
REQUIREMENTS
1. C Compiler (e.g., GCC) 2. Computer System
3. IDE or Text Editor 4. OS compatible with the software

THEORY
In C programming, user-defined functions allow us to create our own functions apart from the
built-in functions provided by the language. These functions can be customized to perform
specific tasks. In this project, we will focus on calculating and displaying the area of a rectangle
using three different types of user-defined functions.
1. Yes Return and No Argument: This type of function returns a value but does not require any
arguments. The function declaration would look like: `int aor(void);`.
2. No Return and Yes Argument: This type of function does not return a value but requires one
or more arguments. The function declaration would look like: `void aor(int, int);`.
3. Yes Return and Yes Argument: This type of function both returns a value and requires one or
more arguments. The function declaration would look like: `int aor(int, int);`.
Here “aor” stands for “Area of Rectangle”.

PROCEDURE (Program Code, Comment, and Output)


1. Yes Return and No Argument:
Program Code:
#include <stdio.h>

// Function prototype
int aor(void);

int main()

1
INDEX 1B

{
int result;

// Call the aor() function and store the returned value in 'result'
result = aor();

// Print the result


printf("The area of the rectangle is: %d\n", result);

return 0;
}

// Function to calculate area of rectangle


int aor(void)
{
int length, width, area;

// Prompt the user to enter the length of the rectangle


printf("Enter the length of the rectangle: ");
scanf("%d", &length);

// Prompt the user to enter the width of the rectangle


printf("Enter the width of the rectangle: ");
scanf("%d", &width);

// Calculate the area of the rectangle


area = length * width;

// Return the calculated area to the calling function


return area;
}

Output:

2. No Return and Yes Argument:


Program Code:
#include <stdio.h>

// Function declaration

2
INDEX 1B

void aor(int, int);

int main()
{
int length, width;

// Prompt user for length of the rectangle


printf("Enter the length of the rectangle: ");
scanf("%d", &length);

// Prompt user for width of the rectangle


printf("Enter the width of the rectangle: ");
scanf("%d", &width);

// Call the function to calculate area


aor(length, width);

return 0;
}

// Function to calculate the area of the rectangle


void aor(int length, int width)
{
int area;

// Calculate the area of the rectangle


area = length * width;

// Print the calculated area


printf("The area of the rectangle is: %d\n", area);
}
Output:

3. Yes Return and Yes Argument:


Program Code:
#include <stdio.h>

// Function prototype declaration


int aor(int, int);

3
INDEX 1B

int main()
{
int length, width, result;

printf("Enter the length of the rectangle: ");


scanf("%d", &length);

printf("Enter the width of the rectangle: ");


scanf("%d", &width);

// Call the function to calculate the area of the rectangle


result = aor(length, width);

printf("The area of the rectangle is: %d\n", result);

return 0;
}

// Function definition to calculate the area of a rectangle


int aor(int length, int width)
{
int area;

// Calculate the area by multiplying the length and width


area = length * width;

return area;
}
Output:

CONCLUSION
In this project, we successfully implemented three types of user-defined functions to calculate
and display the area of a rectangle. We observed that each type of function has its own syntax
and usage. The program demonstrated the concepts of return values and arguments in user-
defined functions. By using user-defined functions, we can modularize our code and perform
specific tasks efficiently.

You might also like