Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

c++ Lec 03 Module 01 Recap of C (Lecture -03)Building a Strong Foundation: C Recap for C++

Rating
-
Sold
-
Pages
18
Uploaded on
11-06-2024
Written in
2023/2024

1. Building a Strong Foundation: C Recap for C++ 2. From C to C++: Bridging the Gap 3. C Essentials for C++ Success 4. The Importance of Reviewing C Concepts for C++ 5. C Refresher Course for C++ Programmers

Institution
Course

Content preview

6/10/24, 12:49 PM Lec 03 Module 01 Recap of C (Lecture -03)




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

Written for

Course

Document information

Uploaded on
June 11, 2024
Number of pages
18
Written in
2023/2024
Type
Class notes
Professor(s)
Sumit arora
Contains
All classes

Subjects

$8.29
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
yashvardhansingh2

Get to know the seller

Seller avatar
yashvardhansingh2 EXAMS
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
6
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions