Lab overview
Lab 01
Lab 02
Lab 03
Lab 04
Lab 05
Lab 06
Lab 07
Lab 08
Lab 09
Lab 10
Lab 11
Lab 12
Lab 13
Review

[Course home page]

CSC 209 lab 10 solutions, week of 19 July 2022

Question 2:

Problem:
Add three to the parameter.

Code:

int f(int x)
{
    int d = x + 3;
    return(d);
}

Solution:

int f(int x)
{
    return(x + 3);
}

Comment:
Although in this case it's obvious that 'd' is not used later, it's still worth simplifying this and avoiding the unnecessary and meaningless temporary variable. In general, in any place in C in which you can use a variable name to mean its value, you can use a more complex expression instead if useful (and this is true of almost all programming languages).


[press 'back' in your web browser to return to the problems]