0% found this document useful (0 votes)
11 views24 pages

C++ Lec11 IIT

The document discusses the organization of large programs in C++, emphasizing the importance of breaking code into smaller, manageable functions. It covers the structure of the main function, the benefits of using functions, and how to organize code across multiple files with function declarations and header files. Additionally, it introduces namespaces to avoid naming conflicts when multiple programmers collaborate on the same project.

Uploaded by

abhishekhamida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views24 pages

C++ Lec11 IIT

The document discusses the organization of large programs in C++, emphasizing the importance of breaking code into smaller, manageable functions. It covers the structure of the main function, the benefits of using functions, and how to organize code across multiple files with function declarations and header files. Additionally, it introduces namespaces to avoid naming conflicts when multiple programmers collaborate on the same project.

Uploaded by

abhishekhamida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

An Introduction to Programming

though C++
Abhiram G. Ranade

Ch. 11: Program Organization and Functions


How do we write large programs?
• Designing/building anything large becomes easier if
we can think of it as made of small parts.
• Example:
– A book is made of chapters.
– A car is made of many subsystems
• For programs, the smaller parts = functions.
– We break up the overall requirements into separate,
small, somewhat independent computations
– Code up the separate parts as separate functions.
An aside: the main program is also a
function
• In C++ the standard way to write the main program is to write it as a
function.
– The function must be named main.
– Its return type must be int.
– For now, it does not take any arguments.
Instead of main_program{ xxx }, you would write: int main(){ xxx }
• Simplecpp translates main_program into the phrase “int main()”
• Simplecpp provides this feature so that you don’t need to understand
functions etc. on the first day.
• The function main is allowed not to have a return statement.
– The value returned has little consequence anyway.
• From now on we will not use main_program, but use main.
Why dividing code into functions
helps
• Different people can write different functions, so work can be
divided.
• Functions are self documenting:
– Function name, parameters, give clues as to what it does.
– Even better if you write the specification as a comment..
– In a long main program it is not obvious where/how to put comments.
• “Write a little bit of code, test it, write some more, ...”
– Test your program one function at a time
• A function is a good way to package and share code.
– If you have written some code and want to give it to someone else, give
them a file containing a function containing your code.
Physical units of code: files
• Suppose several people write different functions
of the same program.
– Convenient for each to use a different file.
• How will a function in one file call a function in
other files?
– Answers soon.
“But am I going to write large programs in this
course?” - 1
• The amount code you write in any assignment in this course will
be at most 150 lines or so.
• You should not write 150 lines as a single main program.
• Max size of main program or any function < 20 lines
– 20 lines you can see at a glance
– If you can see a function at a glance, you are more likely to understand
it fully and write it without errors
– Several experienced programmers recommend even smaller functions.
• So you should break large code into small functions.
– Details when we consider problems which require large programs.
“But am I going to write large programs in this
course?” - 2
• It is OK to put 150 line program that you write into a single file.
• The Novice IDE of simplecpp requires you to only have single file
programs.
• Full simplecpp IDE allows multiple files, but we will not discuss that.
• On the other hand, when your program runs, you use code written
by others
– Simplecpp functions
– Math library for functions such as sqrt, sin, ...
• So you are really collaborating with others without knowing it!
– Already using functions from multiple files. How does that work?
• So the issues we discuss will be relevant even in this course.
What we discussed
• The main program is a function
• Need for splitting a program into many functions
• Need for splitting a program into many files
• What we will see next:
– How to split programs into files
– How your program uses code written by others
– Using C++ without simplecpp
🎻
Splitting a program into many files
• A program may contain several functions.
– All need not be placed in the same file.
• If code in file F calls a function f:
– function f must be declared inside F, textually before any
call to it.
• A function definition is a declaration.
– But there can be other ways to declare. Next.
• Every function must be defined in just one of the files
that are used for a program.
Function declaration
• A function declaration is essentially the definition without the body.
• Example: declaration of gcd function: int gcd(int m, int n);
• Also acceptable: int gcd(int, int);
• The declaration tells the compiler that if the name gcd appears later, it will
be a function and take 2 arguments of the specified type.
– Compiler can now check if the name is used correctly in the rest of the file.
• If a file contains a call to a function but does not contain its definition:
– It can only be partially compiled into an object module.
– To get an executable program, all the object modules containing all called functions
must be linked together.
• Other names for “declaration” : Signature, Prototype
• Example next.
Example of code split over multiple
files
• File gcd.cpp • Function definitions, function declarations
int gcd(int m, int n){ … } • Each file has declarations of called functions.
• To compile and link all files together:
• File lcm.cpp s++ main.cpp lcm.cpp gcd.cpp
int gcd(int, int); • To compile each file separately:
int lcm(int m, int n){ s++ -c lcm.cpp
return m*n/gcd(m,n);} • -c : produce lcm.o (object module).
• To get executable from Object modules:
• File main.cpp s++ main.o lcm.o gcd.o
What you typically do: s++ pgm.cpp m1.o m2.o
int lcm(int, int);
• Compile your program pgm.cpp
int main(){
• Link it to object modules developed by others
cout << lcm(36,24) << endl;
Your pgm.cpp must have the right declarations...
}
Header files
• Tedious to remember what declaration to include • File gcd.cpp
in each file. #include “gcdlcm.h”
• Instead, we can put all declarations into a header
file, and “include” the header file into every file.
int gcd(int m, int n){ … }
• Header file gcdlcm.h
int gcd(int, int); • File lcm.cpp
int lcm(int,int); #include “gcdlcm.h”
int lcm(int m, int n){ … }
• The directive “#include filename”
– gets replaced by the content of the named file.
– It is acceptable if we declare functions that do not get
• File main.cpp
used. #include <simplecpp>
– It is acceptable if we have both a declaration and then
#include “gcdlcm.h”
the definition of a function in the same file.
int main(){ ...}
More on header files
• Header files customarily have the suffix .h
or .hpp., or no suffix.
• If header file is mentioned in “ “, it is picked up
from the current directory.
• If it is mentioned in < >, it is picked up from some
standard place, e.g. simplecpp
What we discussed
• How to split a program over many files
• How to assemble a program out of functions in
many files
• Function declarations and definitions
• Header files
• Next: Namespaces
🎻
Namespaces: High level ideas
• Suppose many people cooperatively develop a single program.
– Possible that several people may define function with the same
name.
• Creates conflict/ambiguity.
• Can be avoided using namespaces.
• Namespace = catalog of names.
• The “full name” of a function f defined in a namespace N is
N::f
• Suppose f is defined in two namespaces N and P.
– We can specify which we mean by writing N::f or P::f.
Defining a namespace
namespace N{
declarations/definition of names
}
• This creates a namespace with name N, and also
defines/declares names inside it.
• You can add more names to a namespace N simply by
writing namespace N{ } again.
• A name g defined without putting it inside a namespace is
said to belong to the global namespace. Its fullname
is ::g.
Example
namespace N{
int gcd(int m, int n){ … }
int lcm(int m, int n){ … }
}
int main(){
cout << N::lcm(36,24) << endl;
}
The using directive
• Suppose you refer to names defined in some
namespace N very frequently.
– You may find it tedious to write N:: all the time.
• Put the following line at the top of your program
using namespace N;
• Then you will be allowed to use any name from N
without having to write N:: before it.
Example using using
namespace N{
int gcd(int m, int n){ … }
int lcm(int m, int n){ … }
}
using namespace N;
int main(){
cout << lcm(36,24) << endl;
}
What we discussed
• Namespaces
– Helps many people use the same name and yet link
their work together if needed
• The using directive
• Next:
– How to use C++ without simplecpp
– Concluding remarks on this lecture sequence
🎻
Using C++ without simplecpp
• If you use C++ without simplecpp, you will not be able to do graphics.
• Also, when you write #include <simplecpp> it itself includes the following
lines for you:
#include <iostream>
#include <cmath>
using namespace std;
• These lines are useful:
– The names cin, cout, endl are defined in the namespace std, in the standard
header file iostream.
• If you do not include simplecpp, be sure to write these lines, that is
enough!
• The header file cmath includes math functions such as sqrt, sin, abs.
Simple example: Using C++ without
simplecpp
#include <iostream> #include <iostream>
using namespace std;
int main(){
int main(){ int n;
int n; std::cin >> n;
cin >> n; std::cout << n*n*n <<
cout << n*n*n <<
endl; std::endl;
} }
Demo
• withoutSimplecpp.cpp
– Uses using directive
• withoutSimplecpp2.cpp
– Does not use the using directive

• These may be compiled using the basic C++


compiler, without loading any of the simplecpp
header files and libraries.
Concluding Remarks
• Functions are building blocks of programs.
• Functions can be put into many files, provided each file
contains a declaration before the use.
• Declarations go into header files.
• Names can be put inside namespaces
• Different programmers can put their work in different
namespaces; same name may be defined in many
namespaces.
• Details discussed in the book.
🎻🎻

You might also like