Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Basics of Modular Programming

The document discusses advanced C++ programming concepts including modular programming with functions, user defined data types, recursion, and file operations. It covers topics such as functions, variables, arguments, function templates, structures, and file input/output.

Uploaded by

Tihtina Menilik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Basics of Modular Programming

The document discusses advanced C++ programming concepts including modular programming with functions, user defined data types, recursion, and file operations. It covers topics such as functions, variables, arguments, function templates, structures, and file input/output.

Uploaded by

Tihtina Menilik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Advanced Programing with C++

Prepared by mekonnen
Outline
Modular Programing
 Functions

Variables

 Inter module communication (Global variables, arguments)

Function (Inline & overloaded)

Function templates

 User Defined Data Types


Enumerations, unions & type definations

Structures

5/10/2021 2
Outline
Recursion

 Recursive solution

 Recursive functions

 Files

 File Operations (text & binary file manipulation)

 Files as arguments to functions

5/10/2021 3
Functions

 Using functions we can structure our programs in a more modular way, accessing all the
potential that structured programming can offer to us in C++.

A function is a group of statements that is executed when it is called from some point of the
program.

The following is its format:

type name ( parameter1, parameter2, ...) { statements }

5/10/2021 4
Cont’d

where:

• type is the data type specifier of the data returned by the function.

• name is the identifier by which it will be possible to call the function.

• parameters They allow to pass arguments to the function when it is called. The different

parameters are separated by commas.

• statements is the function's body. It is a block of statements surrounded by braces { } .

5/10/2021 5
Exercise 1
#include <iostream>
using namespace std;
int addition (int a, int b){
int r;
r=a+b;
return (r);
}
int main (){
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}

5/10/2021 6
Variables

a variable is a portion of memory to store a determined value.

A variable can be either of global or local scope.

 A global variable is a variable declared in the main body of the source code, outside all
functions, while a local variable is one declared within the body of a function.

Each variable needs an identifier that distinguishes it from the others,

Example we can use a, b and result as the variable identifiers.

5/10/2021 7
Scope of variables

5/10/2021 8
Identifiers
A valid identifier is a sequence of one or more letters, digits or underscore
characters (_).

Neither spaces nor punctuation marks or symbols can be part of an identifier.

In addition, variable identifiers always have to begin with a letter.

They can also begin with an underline character (_ ), but in some cases these
may be reserved for compiler specific keywords or external identifiers.

The C++ language is a "case sensitive" language.

5/10/2021 9
Declaring variables:
int a, b;

int result;

Initializing variables:
a = 5; b = 2;

a = a + 1; result = a - b;

5/10/2021 10
Exercise 2-Operating with variables
Write a program that shows addition and subtraction of numbers? In simple program and with
modular programing?
For the modular programing
Note that A & B global variables and c and d are local variables of funcation add and e and f are local
variables of funcation sub, their value is as listed below.
A= 5;
B= 2;
C=6;
D=6;
E=12;
F=10;
You can additional variables if it is must

5/10/2021 11
#include <iostream>

using namespace std;

int main ()

// declaring variables:

int a, b;

int result;

5/10/2021 12
a = 5; b = 2;

a = a + 1; result = a - b;

// print out the result:

cout << result;

// terminate the program:

return 0;

What about in a function structure ?

5/10/2021 13
Inter module communication
How can Functions communicate?
1) Global variables
2) Arguments

5/10/2021 14
Global variables
#include <iostream>
using namespace std;
int a; int b; Int c; int d;
int addition (){
Int r;
r= c+d;
cout << "The result is " << r;
return (r);
}
int main (){
int z;
z =a*b;
cout << "The result is " << z;
cout << "The result is " << addition ();
return 0;
}

5/10/2021 15
Arguments
#include <iostream>
using namespace std;
int addition (int a, int b){
int r;
r=a+b;
return (r);
}
int main (){
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}

5/10/2021 16
#include <iostream>
using namespace std;
int subtraction (int a, int b){
int r; r=a-b; return (r);}
int main (){
int x=5, y=3, z;
z = subtraction (7,2);
cout << "The first result is " << z << '\n' ;
cout << "The second result is " << subtraction (7,2) << '\n' ;
cout << "The third result is " << subtraction (x,y) << '\n' ;
z= 4 + subtraction (x,y);
cout << "The fourth result is " << z << '\n' ;
return 0;
}

5/10/2021 17
Arguments
Arguments passed by value and by reference.
by value means that when calling a function with parameters, what we have passed
to the function are copies of their values but never the variables themselves.
For example, suppose that we called our first function addition using the following
code:
int x=5, y=3, z;
z = addition ( x , y );
What we did in this case was to call to function addition passing the values of x and y,
i.e. 5 and 3 respectively, but not the variables x and y themselves.

5/10/2021 18
Arguments

Int addition (int a, int b)


Z= addition (5, 3);

This way, when the function addition is called, the value of its local variables a and b become 5 and
3 respectively, but any modification to either a or b within the function addition will not have any
effect in the values of x and y outside it, because variables x and y were not themselves passed to
the function, but only copies of their values at the moment the function was called.

5/10/2021 19
Arguments
But there might be some cases where you need to manipulate from inside a
function the value of an external variable. For that purpose we can use
arguments passed by reference, as in the function duplicate of the following
example:

5/10/2021 20
Cont’d
#include <iostream>
using namespace std;
void duplicate (int& a, int& b, int& c){
a*=2; b*=2; c*=2;{
int main (){
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}

5/10/2021 21
Cont’d
the declaration the type of each parameter was followed by an ampersand sign (&).

This ampersand is what specifies that their corresponding arguments are to be


passed by reference instead of by value.

When a variable is passed by reference we are not passing a copy of its value, but
we are somehow passing the variable itself to the function and any modification that
we do to the local variables will have an effect in their counterpart variables passed
as arguments in the call to the function.

5/10/2021 22
Cont’d
Default values in parameters.

When declaring a function we can specify a default value for each of the last parameters.

This value will be used if the corresponding argument is left blank when calling to the function.

To do that, we simply have to use the assignment operator and a value for the arguments in the
function declaration.

If a value for that parameter is not passed when the function is called, the default value is used, but if
a value is specified this default value is ignored and the passed value is used instead. For example:

5/10/2021 23
Cont’d
Default values in parameters.
#include <iostream>
using namespace std;
int divide (int a, int b=2){
int r; r=a/b;
return (r);}
int main (){
cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;}

5/10/2021 24
Overloaded functions.
In C++ two different functions can have the same name if their parameter types or number are
different.
That means that you can give the same name to more than one function if they have either a
different number of parameters or different types in their parameters. For example:

5/10/2021 25
Overloaded functions.
#include <iostream>
using namespace std;
int operate (int a, int b){
return (a*b);}
float operate (float a, float b){
return (a/b);}
int main (){
int x=5,y=2;
float n=5.0,m=2.0;
cout << operate (x,y);
cout << "\n";
cout << operate (n,m);
cout << "\n";
return 0;
}

5/10/2021 26
inline functions.

The inline specifier indicates inline substitution is preferred to the usual function
call mechanism for a specific function.

This does not change the behavior of a function itself, but is used to suggest to the
compiler that the code generated by the function body is inserted at each point the
function is called, instead of being inserted only once and perform a regular call to
it, which generally involves some additional overhead in running time.

5/10/2021 27
inline functions.
The format for its declaration is:
inline type name ( arguments ... ) { instructions . .. }

and the call is just like the call to any other function. You do not have to include the
inline keyword when calling the function, only in its declaration.

Most compilers already optimize code to generate inline functions when it is more
convenient.

This specifier only indicates the compiler that inline is preferred for this function.

5/10/2021 28
If a function is inline, the compiler places a copy of the code of that function
at each point where the function is called at compile time.

5/10/2021 29
Exercise
#include <iostream>
using namespace std;
inline int Max(int x, int y){
return (x > y)? x : y;}
int main( ){
cout << " Max (20, 10): " << Max(20, 10) << endl;
cout << " Max (0, 200): " << Max(0, 200) << endl;
cout << " Max (100, 1010): " << Max(100, 1010) << endl;
return 0;
}

5/10/2021 30
Templates

A template is a blueprint or formula for creating a generic class or a function.

The library containers like iterators and algorithms are examples of generic
programming and have been developed using template concept.

5/10/2021 31
Function Template
Function templates are special functions that can operate with generic types.
This allows us to create a function template whose functionality can be adapted to more than one
type without repeating the entire code for each type.
In C++ this can be achieved using template parameters. A template parameter is a special kind of
parameter that can be used to pass a type as argument:
just like regular function parameters can be used to pass values to a function, template parameters
allow to pass also types to a function. These function templates can use these parameters as if they
were any other regular type.
The format for declaring function templates with type parameters is:
template <typename identifier> function_declaration;

5/10/2021 32
Function Template
For example, to create a template function that returns the greater one of two objects we
could use:

template <class myType>


myType GetMax (myType a, myType b) {
return (a>b?a:b);
}

5/10/2021 33
Function Template
Here we have created a template function with myType as its template parameter.

This template parameter represents a type that has not yet been specified, but that can be used in the
template function as if it were a
regular type.

 As you can see, the function template GetMax returns the greater of two parameters of this still undefined
type.

To use this function template we use the following format for the function call:
function_name <type> (parameters);

5/10/2021 34
Function Template
For example, to call GetMax to compare two integer values of type int we can write:
int x,y;
GetMax <int> (x,y);

When the compiler encounters this call to a template function, it uses the template
to automatically generate a function replacing each appearance of myType by the type
passed as the actual template parameter (int in this case) and then calls it. This
process is automatically performed by the compiler and is invisible to the programmer.

5/10/2021 35
Function Template

Here is the entire example:


#include <iostream>
using namespace std;
template <class T>
T GetMax (T a, T b) {
T result; result = (a>b)? a : b; return (result);{
int main () {
int i=5, j=6, k; long l=10, m=5, n; k=GetMax<int>(i,j); n=GetMax<long>(l,m);
cout << k << endl;
cout << n << endl;
return 0;}

5/10/2021 36
In this case, we have used T as the template parameter name instead of myType because it is shorter and in fact
is a very common template parameter name. But you can use any identifier you like.

In the example above we used the function template GetMax() twice. The first time with arguments of type int
and the second one with arguments of type long. The compiler has instantiated and then called each time the
appropriate version of the function.

As you can see, the type T is used within the GetMax() template function even to declare new objects of that
type:
T result;

5/10/2021 37
Therefore, result will be an object of the same type as the parameters a and b when the function
template is
instantiated with a specific type.

In this specific case where the generic type T is used as a parameter for GetMax the compiler can
find out
automatically which data type has to instantiate without having to explicitly specify it within angle
brackets (like we have done before specifying <int> and <long>). So we could have written instead:
int i,j;
GetMax (i,j);

Since both i and j are of type int, and the compiler can automatically find out that the template
parameter can only be int. This implicit method produces exactly the same result:

5/10/2021 38

You might also like