Showing posts with label variable. Show all posts
Showing posts with label variable. Show all posts

Wednesday, March 22, 2017

C++ Part 42

Review Part V
Local Variables
Variables that are declared within the body of a function definition are
said to be local to that function or to have that function as their scope.
Variables that are declared within the main part of the program are said to
be local to the main part of the program or to have the main part of the
program as their scope. When we say that a variable is a local variable
without any mention of a function and without any mention of the main
part of the program, we mean that the variable is local to some function
definition. If a variable is local to a function, then you can have another
variable with the same name that is declared in the main part of the
program or in another function definition, and these will be two different
variables, even though they have the same name.

Overloading a Function Name
If you have two or more function definitions for the same function name,
that is called overloading. When you overload a function name, the
function definitions must have different numbers of formal parameters or
some formal parameters of different types. When there is a function call,
the compiler uses the function definition whose number of formal
parameters and types of formal parameters match the arguments in the
function call.

Call-by-Reference
To make a formal parameter a call-by-reference parameter, append the
ampersand sign & to its type name. The corresponding argument in a call
to the function should then be a variable, not a constant or other
expression.
When the function is called, the corresponding variable argument
(not its value) will be substituted for the formal parameter. Any
change made to the formal parameter in the function body will be made
to the argument variable when the function is called. The exact details of
the substitution mechanisms are given in the text of this chapter.
EXAMPLE
(OF CALL-BY-REFERENCE PARAMETERS IN A FUNCTION
DECLARATION):
void get_data(int& first_in, double& second_in);

Both void functions and functions that return a value can have return
statements. In the case of a function that returns a value, the return statement
specifies the value returned. In the case of a void function, the return
statement simply ends the function call.


C++ Part 38

Review Part I
Identifiers
Identifiers are used as names for variables and other items in a C++
program. An identifier must start with either a letter or the underscore
symbol, and the remaining characters must all be letters, digits, or the
underscore symbol.

Variable Declarations
All variables must be declared before they are used. The syntax for variable
declarations is as follows:
SYNTAX Type_Name Variable_Name_1, Variable_Name_2, . . .;
EXAMPLES:
int count, number_of_dragons, number_of_trolls;
double distance;

Syntax
The syntax for a programming language (or any other kind of language) is
the set of grammar rules for that language. For example, when we talk
about the syntax for a variable declaration (as in the box labeled “Variable
Declarations”), we are talking about the rules for writing down a well-formed
variable declaration. If you follow all the syntax rules for C++, then
the compiler will accept your program. Of course, this only guarantees that
what you write is legal. It guarantees that your program will do something,
but it does not guarantee that your program will do what you want it to do.

Assignment Statements
In an assignment statement, first the expression on the right-hand side of
the equal sign is evaluated, and then the variable on the left-hand side of
the equal sign is set equal to this value.
SYNTAX Variable = Expression;
EXAMPLES distance = rate * time;
count = count + 2;

Initializing Variables in Declarations
You can initialize a variable (that is, give it a value) at the time that you
declare the variable.
SYNTAX
Type_Name Variable_Name_1 = Expression_ for_Value_1,
Variable_Name_2 = Expresssion_ for_Value_2, . . .;
EXAMPLES
int count = 0, limit = 10, fudge_factor = 2;
double distance = 999.99;
Alternative Syntax for Initializing in Declarations
Type_Name Variable_Name_1 (Expression_ for_Value_1),
Variable_Name_2 (Expression_ for_Value_2), . . .;



C++ Part 35

Typical Program Organization

# include <iostream>  //Setup section used by compiler
storage class of a variable defines the lifetime and visibility of a variable.
Lifetime means the duration till which the variable remains active and visibility defines in which module of the program the variable is accessible.
There are five types of storage classes in C++.
They are:
  1. Automatic
  2. External
  3. Static
  4. Register // Next Post
  5. Mutable // Next Post
______________________________

1. Automatic Storage Class

Automatic storage class assigns a variable to its default storage type. auto keyword is used to declare automatic variables. However, if a variable is declared without any keyword inside a function, it is automatic by default.
This variable is visible only within the function it is declared and its lifetime is same as the lifetime of the function as well. Once the execution of function is finished, the variable is destroyed.

Syntax of Automatic Storage Class Declaration

datatype var_name1 [= value];
or
auto datatype var_name1 [= value];

Example of Automatic Storage Class

auto int x;
float y = 5.67;

2. External Storage Class

External storage class assigns variable a reference to a global variable declared outside the given program. extern keyword is used to declare external variables. They are visible throughout the program and its lifetime is same as the lifetime of the program where it is declared. This visible to all the functions present in the program.

Syntax of External Storage Class Declaration

extern datatype var_name1;
For example,
extern float var1;

Example of External Storage Class

Example 1: C++ program to create and use external storage.
File: sub.cpp
int test=100;  // assigning value to test

void multiply(int n)
{
    test=test*n;
}
File: main.cpp
#include<iostream>
#include "sub.cpp"  // includes the content of sub.cpp
using namespace std;

extern int test;  // declaring test

int main()
{
    cout<<test<<endl;
    multiply(5);
    cout<<test<<endl;
    return 0;
}
A variable test is declared as external in main.cpp. It is a global variable and it is assigned to 100 in sub.cpp. It can be accessed in both files. The function multiply() multiplies the value of test with the parameter passed to it while invoking it. The program performs the multiplication and changes the global variable test to 500.
Note: Run the main.cpp program
Output
100
500

3. Static Storage Class

Static storage class ensures a variable has the visibility mode of a local variable but lifetime of an external variable. It can be used only within the function where it is declared but destroyed only after the program execution has finished. When a function is called, the variable defined as static inside the function retains its previous value and operates on it. This is mostly used to save values in a recursive function.

Syntax of Static Storage Class Declaration

static datatype var_name1 [= value];
For example,
static int x = 101;
static float sum;

_________

source:  www.programtopia.net

Sunday, March 12, 2017

C++ Part 16

Review to C++ Basics

C++ is case sensitive. For example, count and COUNT are two different identifiers.
Use meaningful names for variables.
Variables must be declared before they are used. Other than following this rule, a
variable declaration may appear anyplace.

Be sure that variables are initialized before the program attempts to use their value.
This can be done when the variable is declared or with an 
assignment statement before
the variable is first used.

You can assign a value of an integer type, like int, to a variable of a floating-point
type, like 
double, but not vice versa.

Almost all number constants in a program should be given meaningful names that
can be used in place of the numbers. This can be done by using the modifier 
constin a variable declaration.

Use enough parentheses in arithmetic expressions to make the order of operations clear.
The object cout is used for console output.
A \n in a quoted string or an endl sent to console output starts a new line of output.
The object cerr is used for error messages. In a typical environment, cerr behaves
the same as 
cout.The object cin is used for console input.

In order to use cin, cout, or cerr, you should place the following directives near
the beginning of the file with your program:
#include <iostream>using namespace std;

There are two forms of comments in C++: Everything following // on the same line
is a comment
, and anything enclosed in/* and */ is a comment.

Do not over comment. 

__________________
Source: Absolute C++ by Walter Savitch , Kenrick Mock

Intro to C++ Part 9

A statement

is an instruction that is executed and typically
updates some variable (called a side effect).
An expression is something that is evaluated and does not update
a variable (typically)
int x,y;
y=5;
x = (y = y+2);

What is x going to be?

Evaluate 5+2 and store 7 in y
2 Above also resulted in evaluating the y=y+2 expression, to 7.
→ store 7 in x.
It is very bad Idia to do so
Assignment Statements
In an assignment statement, first the expression on the right-hand side of the equal sign is
evaluated and then the variable on the left-hand side of the equal sign is set equal to this value.
SYNTAX
Variable = Expression;
EXAMPLES
distance = rate * time;
count = count + 2;

Saturday, March 11, 2017

Intro to C++ Part 8

Variable

  • Every variable in a C++ program must be declared before it is used.  

Intro to C++ Part 5


  • Storage class: How/where in memory is the variable stored? 
  • Type: What is the set of values for the variable?
  • Name: What is the name of the variable? 
  • Scope: Where in the program is the variable visible? 
  • Value: Which value is currently stored in the variable?

Intro to C++ Part 4


  • Declaration of variables: How does the variable map onto memory? Updating variable: How do we update the variable, and what do we update it with? 
  • I/O: How do we input and output data into the system? 
  • Control: How do we control which statement gets executed next? 
  • Modularity and Object Orientation: How do we organize the program to enable proper software engineering practices? 
  • Comments: Used to describe code; ignored by compiler, but code unmaintainable without good comments! C/C++: on line beginning with // or surrounded by /*, */

Digital Design Part 3

4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...