CP 03 Functions
CP 03 Functions
Functions
Swaroop Joshi
2023
Area of a rectangle given two sides
Area of a rectangle given two sides
#include <stdio.h>
int main() {
int length = 4; int width = 6;
printf("Length: %d, Width: %d, ", length, width);
printf("Area = %d\n", length * width);
return 0;
}
Area of a rectangle given two sides
#include <stdio.h>
int main() {
int length = 4; int width = 6;
printf("Length: %d, Width: %d, ", length, width);
printf("Area = %d\n", length * width);
return 0;
}
Output:
Length: 4, Width: 6, Area: 24
Area of a rectangle given two sides
Output:
Length: 4, Width: 6, Area: 24
Length: 12, Width: 14, Area: 168
Area of a rectangle given two sides
Output:
Length: 4, Width: 6, Area: 24
Length: 12, Width: 14, Area: 168
Area of a rectangle given two sides
To compute the area of 100 rectangles,
repeat the expression length * width
that many times
int length = 4; int width = 6;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", length * width);
Output:
Length: 4, Width: 6, Area: 24
Length: 12, Width: 14, Area: 168
Area of a rectangle given two sides
✤ A meaningful abstraction
✤ A meaningful abstraction
✤ A meaningful abstraction
4 area_rectangle(2, 2)
Examples 24 area_rectangle(4, 6)
168 area_rectangle(12, 14)
area_rectangle
4 area_rectangle(2, 2)
Examples 24 area_rectangle(4, 6)
168 area_rectangle(12, 14)
*
* @retval 4 area_rectangle(2, 2)
* @retval 24 area_rectangle(4, 6)
* @retval 168 area_rectangle(12, 14)
*
*/
int area_rectangle(int length, int width) {
}
A function to compute area of a rectangle
/**
* @brief Computes the area of a rectangle
given its length and width.
* Requires: length > 0, width > 0
* @return Product of length and width
*
* @retval 4 area_rectangle(2, 2)
* @retval 24 area_rectangle(4, 6)
* @retval 168 area_rectangle(12, 14)
*
Data
*/
int area_rectangle(int length, int width) {
}
A function to compute area of a rectangle
Purpose
/**
* @brief Computes the area of a rectangle
given its length and width.
* Requires: length > 0, width > 0
* @return Product of length and width
*
* @retval 4 area_rectangle(2, 2)
* @retval 24 area_rectangle(4, 6)
* @retval 168 area_rectangle(12, 14)
*
*/
int area_rectangle(int length, int width) {
}
A function to compute area of a rectangle
/**
* @brief Computes the area of a rectangle
given its length and width.
* Requires: length > 0, width > 0
* @return Product of length and width
Contract
*
* @retval 4 area_rectangle(2, 2)
* @retval 24 area_rectangle(4, 6)
* @retval 168 area_rectangle(12, 14)
*
*/
int area_rectangle(int length, int width) {
}
A function to compute area of a rectangle
/**
* @brief Computes the area of a rectangle
given its length and width.
* Requires: length > 0, width > 0
* @return Product of length and width
*
* @retval 4 area_rectangle(2, 2)
* @retval 24 area_rectangle(4, 6)
* @retval 168 area_rectangle(12, 14)
*
Header
*/
int area_rectangle(int length, int width) {
}
A function to compute area of a rectangle
/**
* @brief Computes the area of a rectangle
given its length and width.
* Requires: length > 0, width > 0
* @return Product of length and width
* Examples
* @retval 4 area_rectangle(2, 2)
* @retval 24 area_rectangle(4, 6)
* @retval 168 area_rectangle(12, 14)
*
*/
int area_rectangle(int length, int width) {
}
A function to compute area of a rectangle
/**
* @brief Computes the area of a rectangle
given its length and width.
* Requires: length > 0, width > 0
* @return Product of length and width
*
* @retval 4 area_rectangle(2, 2)
* @retval 24 area_rectangle(4, 6)
* @retval 168 area_rectangle(12, 14)
*
*/
int area_rectangle(int length, int width) {
return length * width;
}
A function to compute area of a rectangle
/**
* @brief Computes the area of a rectangle
given its length and width.
* Requires: length > 0, width > 0
* @return Product of length and width
*
* @retval 4 area_rectangle(2, 2)
* @retval 24 area_rectangle(4, 6)
* @retval 168 area_rectangle(12, 14)
*
*/
Types int area_rectangle(int length, int width) {
must return length * width;
match! }
A function to compute area of a rectangle
/**
* @brief Computes the area of a rectangle
given its length and width.
* Requires: length > 0, width > 0
* @return Product of length and width
*
* @retval 4 area_rectangle(2, 2)
* @retval 24 area_rectangle(4, 6)
* @retval 168 area_rectangle(12, 14)
*
*/
int area_rectangle(int length, int width) {
return length * width;
}
A function to compute area of a rectangle
/**
* @brief Computes the area of a rectangle
given its length and width.
* Requires: length > 0, width > 0
* @return Product of length and width
*
* @retval 4 area_rectangle(2, 2)
* @retval 24 area_rectangle(4, 6)
* @retval 168 area_rectangle(12, 14)
*
*/
int area_rectangle(int length, int width) {
return length * width;
} Function Body
Area of a rectangle given two sides
Area of a rectangle given two sides
Output:
Length: 4, Width: 6, Area: 24
Length: 12, Width: 14, Area: 168
Area of a rectangle given two sides
Output:
Simple, manual testing using the
Length: 4, Width: 6, Area: 24
examples you already thought of
Length: 12, Width: 14, Area: 168
Test with user input
Test with user input
/** ...
*/
int area_rectangle(int length, int width) {
return length * width;
}
int main() {
int length, width;
return 0;
}
Just like variables, functions must be
Test with user input declared before use
/** ...
*/
int area_rectangle(int length, int width) {
return length * width;
}
int main() {
int length, width;
return 0;
}
Test with user input
/** ...
*/
int area_rectangle(int length, int width) {
return length * width;
} You can input multiple variables in a
single scanf, just like you can print
int main() { multiple variables in a single printf
int length, width;
return 0;
}
Test with user input
/** ...
*/
int area_rectangle(int length, int width) {
return length * width;
$ gcc … }
$ ./area
$ ./area int main() {
$ … int length, width;
return 0;
}
Tracing over a function call
Tracing over a function call
✤ That’s how you reason about programs – not by going inside each function call
Tracing over a function call
✤ That’s how you reason about programs – not by going inside each function call
✤ If you try that, you will end up in a much deeper rabbit hole than Alice in the
wonderland!
Program State
Program State
int length = 4;
Program State
int length = 4;
length = 4
Program State
int length = 4;
length = 4
int width = 6;
Program State
int length = 4;
length = 4
int width = 6;
length = 4
width = 6
Program State
int length = 4;
length = 4
int width = 6;
length = 4
width = 6
int area = area_rectangle(length, width);
Program State
int length = 4;
length = 4
int width = 6;
length = 4
width = 6
int area = area_rectangle(length, width);
length = 4
width = 6
area = 24