Professional Documents
Culture Documents
Miscellaneous topics
Swaroop Joshi
2023
Scope of a Variable
✤ Area of the code where the variable is valid, i.e., where it can be accessed
✤ From the place where it is declared to the end of the enclosing block
✤ Area of the code within the immediately enclosing pair of braces {…} is
called a block
Parameter passing
✤ When you call a function, the parameters you pass are called actual
parameters or arguments
✤ The expressions that represent the arguments are evaluated and the values
are copied into the variables representing the formal parameters
✤ When a function returns with a value, that value is again copied into the
calling code
fi
Example
int area_rectangle(int length, int width) {
length++;
return (length-1) * width;
}
int main() {
int length = 4;
int width = 6;
return 0;
}
Remember the basics:
Example A variable declaration means “give me a box
big enough to hold a value of the given type
and let me call it by the given name”
int area_rectangle(int length, int width) {
length++;
return (length-1) * width;
}
int main() {
int length = 4;
int width = 6;
return 0;
}
Example
int area_rectangle(int length, int width) {
length++;
return (length-1) * width;
}
int main() {
int length = 4;
int width = 6;
int main() {
int length = 4;
int width = 6;
return 0;
}
Example 4
length (main)
6
width (main)
4 6
length (area_rectangle) width (area_rectangle)
return 0;
}
Example 4
length (main)
6
width (main)
4 6
length (area_rectangle) width (area_rectangle)
int main() {
int length = 4;
int width = 6;
return 0;
}
Example 4
length (main)
6
width (main)
4 6
length (area_rectangle) width (area_rectangle)
int main() {
int length = 4;
int width = 6;
return 0;
}
Example 4
length (main)
6
width (main)
54 6
length (area_rectangle) width (area_rectangle)
return 0;
}
Example 4
length (main)
6
width (main)
54 6
length (area_rectangle) width (area_rectangle)
int main() {
int length = 4; The return value 24 ‘replaces’ the
int width = 6; function call, thus it prints 24
return 0;
}
Example 4
length (main)
6
width (main)
54 6
length (area_rectangle) width (area_rectangle)
int main() {
int length = 4;
int width = 6;
✤ The concept of multiple scopes applies not only to different functions, but
within the same function too
Scopes in the same function
✤ The concept of multiple scopes applies not only to different functions, but
within the same function too
✤ The concept of multiple scopes applies not only to different functions, but
within the same function too
✤ In other words
int x = 0;
printf("Enter an integer: ");
scanf("%d", &x);
if (x > 0) {
int y = 0;
printf("Enter an integer greater than the previous one: ");
scanf("%d", &y);
if (y > x) {
printf("Good choice!\n");
} else {
printf("Ugh! Can't you follow simple instructions!\n");
}
}
printf("You entered %d and %d\n", x, y);
What is the output of this piece of code?
int x = 0;
printf("Enter an integer: ");
scanf("%d", &x);
if (x > 0) {
int y = 0;
printf("Enter an integer greater than the previous one: ");
scanf("%d", &y);
if (y > x) {
printf("Good choice!\n");
Syntax error! y is not available in
} else { this scope!
printf("Ugh! Can't you follow simple instructions!\n");
}
}
printf("You entered %d and %d\n", x, y);
Switch-case
int watts;
printf("Enter wattage for your bulb: ");
scanf("%d", &watts);
int life;
switch (watts)
{
case 25:
life = 2500;
✤ Alternative to if-else chains for break;
case 40:
multiple alternative decisions
case 60:
life = 1000;
✤ The expression after case must be a break;
case 75:
constant → strings not supported
case 100:
life = 750;
✤ Careful with break break;
default:
life = 0;
✤ Use of default }
printf("The bulb should last %d hours\n", life);
char grade
#include<stdio.h>
#include<limits.h>
int main() {
printf("%d to %d\n", INT_MIN, INT_MAX);
printf("%d\n", INT_MAX + 1);
✤ Doubles are meant to represent real numbers (can have precision issues)
int x = 10 * 9.7;
printf("x as an int: %d\n", x);
printf("x as a char: %c\n", x);
Implicit type conversion
int x = 10 * 9.7;
printf("x as an int: %d\n", x); x as an int: 97
printf("x as a char: %c\n", x); x as a char: a
Implicit type conversion
int x = 10 * 9.7;
printf("x as an int: %d\n", x); x as an int: 97
printf("x as a char: %c\n", x); x as a char: a
int x = 10 * 9.7;
printf("x as an int: %d\n", x); x as an int: 97
printf("x as a char: %c\n", x); x as a char: a
int x = 10 * 9.7;
printf("x as an int: %d\n", x); x as an int: 97
printf("x as a char: %c\n", x); x as a char: a
✤ 32: space, 48–57: digits 0–9, 65–90: letters A–Z, 97–122: letters a–z
Implicit type conversion
int x = 10 * 9.7;
printf("x as an int: %d\n", x); x as an int: 97
printf("x as a char: %c\n", x); x as a char: a
✤ 32: space, 48–57: digits 0–9, 65–90: letters A–Z, 97–122: letters a–z
char ch = 'a';
printf("The second letter from '%c' is '%c'\n", ch, ch + 2);
Implicit type conversion
char ch = 'a';
printf("The second letter from '%c' is '%c'\n", ch, ch + 2);
✤ User of the function must satisfy the Requires clause to get a meaningful
result
/**
* @brief Returns the non-negative square-root
Contracts * of the given non-negative number.
* Requires n >= 0
*/
double sqrt(double n)
✤ Two-way relation:
✤ User of the function must satisfy the Requires clause to get a meaningful
result
fi
/**
* @brief Returns the non-negative square-root
Contracts * of the given non-negative number.
* Requires n >= 0
*/
double sqrt(double n)
✤ Two-way relation:
✤ User of the function must satisfy the Requires clause to get a meaningful
result
fi
/**
* @brief Returns the non-negative square-root
Contracts * of the given non-negative number.
* Requires n >= 0
*/
double sqrt(double n)
✤ Two-way relation:
✤ User of the function must satisfy the Requires clause to get a meaningful
result
fi
Meaning of contract
✤ What should the function do if the precondition is not satis ed by the caller?
✤ Anything?
✤ …
fi
Meaning of contract
✤ What should the function do if the precondition is not satis ed by the caller?
✤ Anything?
All of these are valid responses
✤ Return a default value Since the user (i.e., the calling code) of the
function did not hold its end of the contract
✤ Print an error message (passing only the valid values), the implementer
(i.e., the function code) is not responsible for
✤ Abort the program
anything speci c
✤ Not end the program at all!
✤ …
fi
fi
Separation of concerns
Separation of concerns
✤ As long as you do that, you can apply the contract to reason about the
program
Separation of concerns
When calling sqrt, you make sure you pass a
non-negative value
And you are guaranteed to get the expected
✤ When you are calling a function, result
✤ As long as you do that, you can apply the contract to reason about the
program
Separation of concerns
When calling sqrt, you make sure you pass a
non-negative value
And you are guaranteed to get the expected
✤ When you are calling a function, result
✤ As long as you do that, you can apply the contract to reason about the
program