0
0
Cprogramming~20 mins

Function prototypes - Mini Project: Build & Apply

Choose your learning style9 modes available
Function prototypes
📖 Scenario: You are writing a simple C program that calculates the area of a rectangle. To organize your code better, you will use a function prototype before defining the function.
🎯 Goal: Build a C program that declares a function prototype for calculateArea, defines the function, and uses it to find the area of a rectangle.
📋 What You'll Learn
Create a function prototype for calculateArea that takes two int parameters and returns an int.
Define the function calculateArea after main.
In main, declare two int variables length and width with values 5 and 10 respectively.
Call calculateArea with length and width and store the result in area.
Print the value of area.
💡 Why This Matters
🌍 Real World
Function prototypes are used in C programs to organize code and allow functions to be called before they are defined. This helps in building larger programs with multiple files.
💼 Career
Understanding function prototypes is essential for C programming jobs, embedded systems development, and any role involving low-level programming or performance-critical applications.
Progress0 / 4 steps
1
Create variables for rectangle dimensions
In the main function, declare two int variables called length and width and set them to 5 and 10 respectively.
C
Need a hint?

Use int length = 5; and int width = 10; inside main.

2
Add function prototype for calculateArea
Above the main function, write a function prototype for calculateArea that takes two int parameters and returns an int.
C
Need a hint?

Write int calculateArea(int length, int width); before main.

3
Define the calculateArea function
After the main function, define the calculateArea function that takes two int parameters length and width and returns their product.
C
Need a hint?

Define int calculateArea(int length, int width) { return length * width; } after main.

4
Call calculateArea and print the result
Inside main, call calculateArea with length and width, store the result in an int variable called area, and print area using printf.
C
Need a hint?

Use int area = calculateArea(length, width); and printf("Area: %d\n", area); inside main.