Programming in C++
Prof. Partha Pratim Das
Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
Lecture – 03
Recap of C (Part III)
We will continue on Module 01, recapitalization of C. This is the third part. In the first
two parts we have talked about the data types, variables, expressions, statements. And, in
the second part we have talked about different derived types, arrays, structure, union and
pointer.
In the spot, we will start with the basic modular concept of C, which is a function.
(Refer Slide Time: 00:51)
So, as you all know a function performs a specific task of computation. A function is like
can be treated as a black box, which can take a number of parameters and will give you a
result. Now, while usually we will expect a function to take one or more parameters, it is
possible that you write a function which does not take any parameter. And, it is typical
that it gives you a value at the end of computation; we say it returns a result. But, it is
also possible that it may not return any result.
about:blank 1/18
,6/10/24, 12:49 PM Lec 03 Module 01 Recap of C (Lecture -03)
The number of parameters that a function has; each parameter also called argument, we
will have a type. And, if we do not want to specify the type we can use void. Return will
also have a type. And, we will use void if there is nothing to return.
A typical declaration will look like this; funct is the name of the function. On the left, a
return type and on the right within this pair of parenthesis, we have the parameters. If
there are more than one parameter, they are separated by comma. And, at the end of this
parenthesis if you put a semicolon, then we know that you are just interested to talk
about what parameters the function takes and what type of value it returns. But, you are
not interested to specify how actually this funct function computes the result integer from
the parameters x and y.
In such cases if you just dominate the list of arguments with the semicolon, we will say
this is a function header or a function prototype or a function signature. And more and
more, the signature or prototype kind of terms will keep on occurring in C++. And since
in this case, in case of a signature we are not actually specifying how x and y will be
used to compute the result. It is optional whether you specify x or y or both of them. You
could just write it as int funct (int , int); that will also be a valid header.
So, what this tells us? It tells us that there are two parameters. First parameter is an
integer; second parameter is another integer. It tells that the name of the function is funct,
it tells us that the type of value it will return is int. This is the purpose of the function
declaration or the function header.
Now, when we are ready to specify as to what this function will compute or how this
function will compute the result from the parameters, then we provide the function body;
which is the whole function body is a compound statement. So, it is a pair of parenthesis,
curly braces, within which the function body has to be return. So, within that there could
be multiple declarations and statements specifying the function body.
A function will have a return statement, which returns an expression of the return type as
a final result. If the function is not returning anything, if the function return type is void,
then the return statement will not have an expression associated with it. Please note that
about:blank 2/18
, 6/10/24, 12:49 PM Lec 03 Module 01 Recap of C (Lecture -03)
in C 89, it was allowed that if a function does not return anything, then it is not necessary
to specify the return statement. That protocol still continues. But, for several reasons that
will become clear, when we do more of C++. It is strictly avoidable that you write a
function and do not put a return. So, even though you may not return anything from the
function that is return type is void, please provide a return statement.
(Refer Slide Time: 05:04)
Now, functions get their parameters by a mechanism, which is known as call by value. I
assume that you all know this where at the call site, the example is here. On top, you can
see the function body, the whole definition of the function. And, here you see the
function invocation or function call, where we are using two parameters that are local to.
That their variables local to main to call this function. So the result of this, the first
parameter ‘a’ is copied to the first formal parameter x. So, ‘a’ ‘b’ are called actual
parameters at the call site; ‘x’, ‘y’ are called the formal parameters at the definition site.
And, as I had mentioned in reference to accessing different components of an array and
accessing different components of a structure that there are different conventions. Here C
follows a positional parameter convention to call function.
So, the first actual parameter corresponds to the first formal parameter; the second actual
about:blank 3/18