C++ Lec12 IIT
C++ Lec12 IIT
though C++
Abhiram G. Ranade
Write a main program to from which you supply a function which will cause a
dashed square of side length 100 do be drawn.
Basically, this will let you draw “decorated” squares. By supplying the
appropriate function, you should be able to draw a square in which the turtle
goes off from the line, draws something, but gets back on track again.
More complex variable capture
• By placing names of variables in [], you enable the
values of the variables to be used inside the body
of the lambda expression.
• But you can also allow the lambda expression to
access the value of the variable at the time the
lambda expression is evaluated.
• Discussed in the book, but will not be considered
in this course.
What we discussed
• Lambda expressions evaluate to nameless
functions
• We can evaluate these by supplying arguments, or
pass them to other functions.
• Lambda expressions can also capture the values
of variables defined in the function in which the
lambda expression appears.
• Next: Giving default values to parameters
🏓
Default values to parameters
• Suppose we are drawing lots of squares, most of which are
black, but for some we want to specify a colour.
• Wouldnt it be nice if we can say,
– “if I do not tell you what colour to use, make it black?”
• C++ allows this:
– One or more parameters occurring at the end of the parameter list can
be given default values.
– Suppose there are n parameters, and you have specified default values
for last m.
– In the call you must give at least n-m arguments.
– If you give n-m+r arguments, then the last m-r will take default values.
Imprinting a disk
void disk(double x, double y, double r=10,
Color col=COLOR("black"),
bool fill=true){
Circle c(x,y,r);
c.setColor(col);
c.setFill(fill);
c.imprint();
}
Demo
• disk.cpp
Exercise
The k-norm of a math vector (x,y,z,...) is defined to
be the kth root of xk + yk + ... Most commonly the 2
norm is used.
Define a function norm for 2 dimensional vectors
(x,y). The call norm(x,y,k) should give the kth root
of xk + yk. The call norm(x,y) should give the 2
norm i.e. square root of x2 + y2.
You may note that the function pow(x,r) returns xr
for any r.
What we discussed
• How to give default values to the last parameters
in the parameter list.
• Note that if you want to specify a default value for
the rth parameter, you must specify a default
value for all subsequent parameters as well.
• This often provides some convenience.
• Next: Overloading functions, conclusion of lecture
sequence
🏓
Overloading functions
• C++ allows you to define multiple functions with
the same name, provided they have different
argument lists.
• Consider a function to compute the area of a
graphical object.
• It will be nice to give the name Area to the
function even though the arguments could be a
circle or a rectangle.
• Just do it! It is allowed in C++.
Function overloading demo
• Area.cpp
Exercise
Write an additional Area function so that if one
double argument is given it returns the area of a
circle with that radius; if two double arguments are
given it returns the are of a rectangle with those
side lengths.
Concluding Remarks
• It is often useful to pass functions as arguments in a function
call.
– Common example: writing a single function that calculates the roots, or
the numerical integral, ... of math functions.
– Functions can be passed by specifying their name, or by giving a
lambda expression.
– There is a C style method also, discussed in book.
• C++ allows default values for the last arguments in a function.
• C++ also allows defining many functions with same name,
provided the parameter types are different.
🏓🏓