BITS Pilani
K K Birla Goa Campus
CSF111: Computer Programming
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
int length = 4; int width = 6;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", length * width);
length = 12; width = 14;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", length * width);
Area of a rectangle given two sides #include, in main, return
removed for the slide;
You still need them in the program!
int length = 4; int width = 6;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", length * width);
length = 12; width = 14;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", length * width);
Area of a rectangle given two sides
int length = 4; int width = 6; Reassign the variable to a new value;
printf("Length: %d, Width: %d, ", length,Dowidth);
not declare again!
printf("Area: %d\n", length * width);
length = 12; width = 14;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", length * width);
Area of a rectangle given two sides
int length = 4; int width = 6;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", length * width);
length = 12; width = 14;
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
int length = 4; int width = 6;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", length * width);
length = 12; width = 14;
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
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);
length = 12; width = 14;
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
int length = 4; int width = 6;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", length * width);
length = 12; width = 14;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", length * width);
If we were to change the way we
compute the area, we have to change
Output: it at all those places!
Length: 4, Width: 6, Area: 24
Length: 12, Width: 14, Area: 168
Function
✤ A meaningful abstraction
✤ Captures a task that can take
one or more instructions into
one concise instruction.
Anatomy of a function
Function return_type function_name(parameter_list) {
function_body
}
✤ A meaningful abstraction
✤ Captures a task that can take
one or more instructions into
one concise instruction.
Anatomy of a function
Function return_type function_name(parameter_list) {
function_body
}
What type of value does this function evaluate to.
✤ A meaningful abstraction
✤ Captures a task that can take
one or more instructions into
one concise instruction.
Anatomy of a function
Function return_type function_name(parameter_list) {
function_body
}
What type of value does this function evaluate to.
Name of the instruction. You use it to call the
✤ A meaningful abstraction
function, i.e., to perform the task.
✤ Captures a task that can take
one or more instructions into
one concise instruction.
Anatomy of a function
Function return_type function_name(parameter_list) {
function_body
}
What type of value does this function evaluate to.
Name of the instruction. You use it to call the
✤ A meaningful abstraction
function, i.e., to perform the task.
✤ Captures a task that can take
Input values this function operates on. Zero or
one or more instructions into more. Must have type and name.
one concise instruction.
Anatomy of a function
Function return_type function_name(parameter_list) {
function_body
}
What type of value does this function evaluate to.
Name of the instruction. You use it to call the
✤ A meaningful abstraction
function, i.e., to perform the task.
✤ Captures a task that can take
Input values this function operates on. Zero or
one or more instructions into more. Must have type and name.
one concise instruction.
Zero or more instructions to perform the task.
Enclosed within curly braces {…}
Design recipe
Design recipe
✤ Programming is a creative activity
Design recipe
✤ Programming is a creative activity
✤ However, rules can guide and focus creativity
Design recipe
✤ Programming is a creative activity
✤ However, rules can guide and focus creativity
✤ Poets composing in speci c meters (e.g., Kabir’s dohas, Thiruvalluvar’s
three-liners)
fi
Design recipe
✤ Programming is a creative activity
✤ However, rules can guide and focus creativity
✤ Poets composing in speci c meters (e.g., Kabir’s dohas, Thiruvalluvar’s
three-liners)
✤ We start with a simple recipe and expand it as the course progresses
fi
Design Recipe
Data Understand the input data: int, double, char, etc.
Purpose and Header Describe (but don’t write) the function.
Specify the output in terms of its inputs. Describe any restrictions
Contract
on the input values.
Show what will happen when the function is invoked
Examples
(Also serve as test cases)
Body The most creative step: implement the function body
area_rectangle
area_rectangle
Data Two ints
area_rectangle
Data Two ints
Computes the area of a rectangle given its length and width.
Purpose and Header
int area_rectangle(int length, int width)
area_rectangle
Data Two ints
Computes the area of a rectangle given its length and width.
Purpose and Header
int area_rectangle(int length, int width)
Returns the product of the given length and width.
Contract Requires: length > 0, width > 0
area_rectangle
Data Two ints
Computes the area of a rectangle given its length and width.
Purpose and Header
int area_rectangle(int length, int width)
Returns the product of the given length and width.
Contract Requires: length > 0, width > 0
4 area_rectangle(2, 2)
Examples 24 area_rectangle(4, 6)
168 area_rectangle(12, 14)
area_rectangle
Data Two ints
Computes the area of a rectangle given its length and width.
Purpose and Header
int area_rectangle(int length, int width)
Returns the product of the given length and width.
Contract Requires: length > 0, width > 0
4 area_rectangle(2, 2)
Examples 24 area_rectangle(4, 6)
168 area_rectangle(12, 14)
Body // not shown for now
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) {
}
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
int length = 4; int width = 6;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", area_rectangle(length, width));
length = 12; width = 14;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", area_rectangle(length, width));
Area of a rectangle given two sides
int length = 4; int width = 6;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", area_rectangle(length, width));
length = 12; width = 14;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", area_rectangle(length, width));
Output:
Length: 4, Width: 6, Area: 24
Length: 12, Width: 14, Area: 168
Area of a rectangle given two sides
int length = 4; int width = 6;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", area_rectangle(length, width));
length = 12; width = 14;
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", area_rectangle(length, width));
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;
printf("Enter length and width: ");
scanf("%d %d", &length, &width);
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", area_rectangle(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;
printf("Enter length and width: ");
scanf("%d %d", &length, &width);
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", area_rectangle(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;
printf("Enter length and width: ");
scanf("%d %d", &length, &width);
printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", area_rectangle(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;
printf("Enter length and width: ");
scanf("%d %d", &length, &width);
Compile once,
run multiple times printf("Length: %d, Width: %d, ", length, width);
printf("Area: %d\n", area_rectangle(length, width));
return 0;
}
Tracing over a function call
Tracing over a function call
✤ Go line by line – including over a function call!
Tracing over a function call
✤ Go line by line – including over a function call!
✤ That’s what we did with printf before
Tracing over a function call
✤ Go line by line – including over a function call!
✤ That’s what we did with printf before
✤ Your function is no special!
Tracing over a function call
✤ Go line by line – including over a function call!
✤ That’s what we did with printf before
✤ Your function is no special!
✤ Apply the contract and write the values
Tracing over a function call
✤ Go line by line – including over a function call!
✤ That’s what we did with printf before
✤ Your function is no special!
✤ Apply the contract and write the values
✤ That’s how you reason about programs – not by going inside each function call
Tracing over a function call
✤ Go line by line – including over a function call!
✤ That’s what we did with printf before
✤ Your function is no special!
✤ Apply the contract and write the values
✤ 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