Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Similar documents
Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

UNIT- 3 Introduction to C++

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

C++ Programming: From Problem Analysis to Program Design, Third Edition

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

Lab Instructor : Jean Lai

BITG 1233: Introduction to C++

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

Programmer-Defined Functions

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Array Elements as Function Parameters

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Chapter 2 C++ Fundamentals

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

REVIEW. The C++ Programming Language. CS 151 Review #2

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Getting started with C++ (Part 2)

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Functions and Recursion

Cpt S 122 Data Structures. Introduction to C++ Part II

Add Subtract Multiply Divide

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

Introduction to C++ Introduction to C++ 1

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Computer Programming

2 nd Week Lecture Notes

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Computing and Statistical Data Analysis Lecture 3

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Creating a C++ Program

Lecture 3: C Programm

Introduction to C++ Systems Programming

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

The C++ Language. Arizona State University 1

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CSCE 110 PROGRAMMING FUNDAMENTALS

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

6.096 Introduction to C++ January (IAP) 2009

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Chapter 15 - C++ As A "Better C"

CS201 - Introduction to Programming Glossary By

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Program Organization and Comments

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Chapter 2. C++ Basics

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

PIC 10A Objects/Classes

Variables and Operators 2/20/01 Lecture #

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

C++ Programming Lecture 1 Software Engineering Group

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Programming Language. Functions. Eng. Anis Nazer First Semester

Scientific Computing

Objectives. In this chapter, you will:

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Short Notes of CS201

Programming. C++ Basics

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

CS242 COMPUTER PROGRAMMING

Review of the C Programming Language for Principles of Operating Systems

CSCE Practice Midterm. Data Types

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Input And Output of C++

Fundamentals of Programming CS-110. Lecture 2

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Chapter 2: Overview of C++

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Lecture 2: C Programm

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

A First Program - Greeting.cpp

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Section we will not cover section 2.11 feel free to read it on your own

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Transcription:

Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces

When a program runs it requires main memory (RAM) space for Program instructions (the program) Data required for the program There needs to be a system for efficiently allocating memory We will only consider how memory is allocated to program data (variables) code storage data storage

We will often refer to main memory By which we mean RAM or random-access memory RAM is both readable and writable RAM can be thought of as a (very long) sequence of bits In this simplified model we will number this sequence in bytes

RAM is a long sequence of bytes Starting with 0 Ending with the amount of main memory (-1) RAM is addressable and supports random access That is, we can go to byte 2,335,712 without having to visit all the preceding bytes

Consider a computer with 1GB* of RAM *1 GB = 1,073,741,824 bytes RAM can be considered as a sequence of bytes, addressed by their position 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1073741816 1073741817 1073741818 1073741819 1073741820 1073741821 1073741822 1073741823 Note this is a simplified and abstract model

Declaring a variable reserves space for the variable in main memory The amount of space is determined by the type The name and location of variables are recorded in the symbol table The symbol table is also stored in RAM The symbol table allows the program to find the address of variables We will pretty much ignore it from now on!

For simplicity's sake assume that each address is in bytes and that memory allocated to the program starts at byte 2048 int x, y; x = 223; x = 17; y = 3299; Creates entries in the symbol table for x and y variable address x 2048 y 2052 These lines change the values stored in x and y, but do not change the location or amount of main memory that has been allocated data 223 17 3299 address 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062...

There are two things that we might wish to find out about a variable Most of the time we just want to find what value is stored in a variable By the end of the previous example the variable x had the value 17 stored in it Sometimes we* might want to know where a variable is located its address The address of x was 2,048 *OK, we probably don t want to know this, but a program might need this information

Variables are stored in main memory in sequence We can find out the value of a variable And its address To retrieve the address write the variable name preceded by an ampersand (&) The value of a variable can be changed by assignment But its storage location and the amount of memory allocated to a variable cannot change

We can use cout to print the value of a variable, or its address int x = 12; cout << "x = " << x << endl;; cout << "x's address = " << &x << endl; Here is another example float y = 2.13; I wouldn't usually use x or y as the name of a variable since it doesn't convey any meaning, but in this example they are just arbitrary values cout << "y = " << x << endl; This just shows that we can access the address of a variable, printing the address like this is not generally useful cout << "y's address = " << &y << endl;

Library functions are often used in C++ For example, the cmath library contains mathematical functions Such as sqrt and pow When a function is called in a program the function code is executed And the return value of the function (if any) replaces the function call (or invocation)

// Prints the area of a circle #include <iostream> #include <cmath> #include <iomanip> using namespace std; // Define Pi const double = PI 3.14159265; // Main program next slide! int main(){ //...

int main(){ float radius; float area; // Get keyboard input cout << "Please enter the radius: "; cin >> radius; // Calculate area area = PI * pow(radius, 2); // Print output cout << fixed << setprecision(2); cout << "The circle's area is " << area << endl; return 0; Function call Sets decimal places to 2

the function executes, and it is replaced by the result that it returns area = PI * pow(radius, 2); function name arguments to the function

Function calls in statements have precedence over most other operations Such as arithmetic or comparisons A function is executed and its result returned before other operations are performed

Many functions need input Data passed to a function are referred to as arguments The number and type of arguments must match what is expected by the function Failing to give appropriate arguments results in a compilation error

area = PI * pow("fred", 2); no instance of overloaded function "pow" matches the argument list the error message will vary by compiler

When a function is called, program execution switches to the function Each line of code in the function is executed in turn until The last line is reached, or A return statement is processed Execution then returns to the line of code where the function was called from The function call is replaced by the return value

As well as using library functions, we can write our own functions This is very useful as it increases the modularity of programs Functions can be written and tested in isolation It is good programming style to write many small functions

Consider the calculation to find the greatest common divisor (gcd) of two integers Which was used in a previous example to simplify fractions This calculation might be useful in other places in a larger program Or in other programs

return type function body function name int gcd(int a, int b) { while(a!= b) { if(a > b){ a = a-b; else{ b = b-a; return a; parameter list function header return statement

Functions have two parts A header (or declaration) Return type of the function Function name Function parameter list A body (or definition) The executable statements or implementation of the function The value in the return statement (if any) must be the same type as the function's return type

Function's names are identifiers, so must follow identifier rules Only contain a to z, A to Z, _, 0-9 Must start with a to z, A to Z By convention, function names should Start with a lower case letter They can be distinguished from variables by their parameter lists (brackets) Have meaningful names

Not all functions need to return a value For example a function to print instructions Functions that return nothing should be given a return type of void A function that returns nothing (void) cannot be used in an assignment statement Void functions are often used for output

Every function must have a parameter list The parameter list consists of ()s which follow the function name Parameter lists contain function input A parameter list can be empty, but the brackets are still required when the function is defined Whenever a function is called (used) it also must be followed by brackets Again, even if they are empty

#include <iostream> void hi(){ std::cout("hello world"); int main (){ hi(); return 0; Noy very useful, but imagine it prints the instructions for a console application.

A function that is written in a.cpp file can be used in that file Just like a library function The function must be declared before it is used, either by Defining the function, or Writing a forward declaration, and defining it later Also referred to as a function prototype

#include <iostream> #include <cmath> using namespace std; const float PI = 3.14159; Error: C3861 'spherevolume': identifier not found The compiler processes the file one line at a time starting from the top, so when it reaches spherevolume it does not recognize the name int main() { float r = 3; cout << "radius = " << r << ", volume = " << spherevolume(r); return 0; double spherevolume(double radius) { return PI * pow(radius, 3) * 4.0/3;

#include <iostream> #include <cmath> using namespace std; it is usually preferable to use function prototypes const float PI = 3.14159; // Function Prototypes rather than defining all your functions above main double spherevolume(double radius); int main() { float r = 3; cout << "radius = " << r << ", volume = " << spherevolume(r); return 0; double spherevolume(double radius) { return PI * pow(radius, 3) * 4.0/3; fix by inserting a function prototype before main (or by moving the function definition) parameter argument

The compiler compiles a program one line at a time Starting at the top of the file If the compiler encounters an undefined identifier it will be unable to continue Functions can be declared before being used and defined later on in the file A function prototype consists of the function header (followed by a semi-colon)

Many functions require data to perform operations on (i.e. input) Such data can be given to functions in a number of ways The function can obtain the data itself By getting input from the user Data can be provided via global variables Which are visible throughout the file Values can be passed to parameters

A global variable is a variable that is declared outside any function Including the main function Variables declared inside a function are referred to as local variables Global variables can be used by any function In the same file (and possibly other files) Avoid global variables With a few exceptions, such as constants

They make programs harder to understand Functions that rely on global variables cannot be considered in isolation They make programs harder to modify It is relatively straightforward to change a function that is self-contained They make programs less portable If functions depend on global variables they cannot easily be used by other files

Parameter lists are the preferred method of data input to a function Parameters are special variables that can be passed values from calling code A function's parameters are given the value of variables passed to a function Variables passed to functions are called arguments

formal parameters void printintdivision(int dividend, int divisor) { int quotient = dividend / divisor; int remainder = dividend % divisor; cout << dividend << "/" << divisor << " = " << quotient << " remainder" << remainder); //... main function cin >> x; //... arguments cin >> y; //... printintdivision(x, y); //prints result of x/y

Parameters and arguments are different variables They refer to different main memory locations Parameters are given the values of arguments They may even have the same name but still refer to different variables It is also possible to pass a function the address of a variable Using pass by reference Or pointers

Many functions return values The function performs a calculation And sends the result back to calling code e.g. sqrt, pow Values are returned by a return statement The type of the value in a return statement must match the return type of the function

// Function to return a letter grade char lettergrade(int grade){ if (grade > 89) return 'A'; if (grade > 74) return 'B'; if (grade > 59) return 'C'; if (grade > 49) return 'D'; else //(grade < 50) return 'F'; As soon as any return statement is reached the function execution ends and the value is returned to the calling code

Whenever we want to perform an action or a calculation we should write a function Functions organize other programming constructs Functions often need input (arguments) and often return values The return type of a function specifies the type of information that it returns Note that a function with a return type of void returns nothing

A function that specifies a return type must contain a return statement Or an error results The compiler may not check that all paths within a function contain a return value Such as nested if statements, or If, else if,..., else statements Some compilers may give warnings about this, but others may not

Is it possible to return from a function that does not return a value (i.e. is void)? Yes! Just use return with no value This allows us to return from any function before reaching the end of the function For example, in a branch of an if statement

Functions should perform one task In particular, functions that calculate values should not be responsible for output Such values should be returned not printed Functions should be self contained Input should be passed via parameters Results should be returned using a return statement

Recall that two variables cannot have the same name More accurately, two variables can have the same name as long as they have different scope The scope of a variable is the block in which it is declared So variables with different scope may have different names

void f(){ int x = 99; cout << x << endl; int main() { int x = 1; if (x == 1){ int x = 2; cout << x << endl; cout << x; f(); cout << endl << endl; return 0; f x scope main x scope if x scope

The scope of any variable declared inside a function is local to that function This includes a function's parameters So a parameter of a function may not have the same name as a variable declared inside that function Unless the variable has a more limited scope Functions cannot be defined inside functions The scope of two different functions therefore does not overlap

The example shown previously had two variables called x in the main function One variable declared in if (if-x) and one declared in main (main-x) The scope of main-x encompassed the entire main function and overlapped with if-x In such cases only the variable with the least scope is visible

Let's expand our memory model to cover functions Which includes the main function It's not going to be very different from our original model Main memory is a long sequence of binary digits Variables are allocated in sequence A system table allows us to find variables But we are not going to go into much detail about it

This program fragment simplifies two fractions int num1 = 48; int den1 = 140; In this simple memory model of the call stack memory is allocated to variables in sequence in this example starting with byte 192 The call stack is the area of main memory used for function calls, also referred to as automatic memory next free byte name num1 den1 data 48 140 address 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

This program fragment simplifies two fractions int num1 = 48; int den1 = 140; int div = gcd(num1, den1); next free byte int gcd(int a, int b) { while (a!= b) { if (a > b) { a = a - b; else { b = b - a; return a; name num1 den1 a b data 48 140 48 140 address 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

This program fragment simplifies two fractions int num1 = 48; int den1 = 140; int div = gcd(num1, den1); next free byte int gcd(int a, int b) { while (a!= b) { if (a > b) { a = a - b; else { b = b - a; return a; name num1 den1 a b data 48 140 48 4 140 92 44 address 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

This program fragment simplifies two fractions int num1 = 48; int den1 = 140; int div = gcd(num1, den1); next free byte return from gcd int gcd(int a, int b) { while (a!= b) { if (a > b) { a = a - b; else { b = b - a; return a; name num1 den1 a b data 48 140 4 4 address 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

This program fragment simplifies two fractions int num1 = 48; int den1 = 140; int div = gcd(num1, den1); next free byte and assign memory to div int gcd(int a, int b) { while (a!= b) { if (a > b) { a = a - b; else { b = b - a; return a; name num1 den1 div data 48 140 4 address 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

This program fragment simplifies two fractions int num1 = 48; int den1 = 140; int div = gcd(num1, den1); num1 = num1/div; den1 = den1/div; next free byte int gcd(int a, int b) { while (a!= b) { if (a > b) { a = a - b; else { b = b - a; return a; name num1 den1 div data 48 12 140 35 4 address 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

This program fragment simplifies two fractions int num1 = 48; int den1 = 140; int div = gcd(num1, den1); num1 = num1/div; den1 = den1/div; int num2 = 11; int den2 = 288; next free byte int gcd(int a, int b) { while (a!= b) { if (a > b) { a = a - b; else { b = b - a; return a; name num1 den1 div num2 den2 data 12 35 4 11 288 address 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

This program fragment simplifies two fractions int num1 = 48; int den1 = 140; int div = gcd(num1, den1); num1 = num1/div; den1 = den1/div; int num2 = 11; int den2 = 288; div = gcd(num2, den2); next free byte int gcd(int a, int b) { while (a!= b) { if (a > b) { a = a - b; else { b = b - a; return a; name num1 den1 div num2 den2 a b data 12 35 4 11 288 11 288 address 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

The area of main memory used for function calls is called the call stack A stack is a simple data structure where items are inserted and removed at the same end Usually referred to as the top of the stack A stack is a LIFO (Last In First Out) structure The call stack behaves like a stack Since the last function to be called Is the first function to be removed (de-allocated)

The process for allocating main memory on the stack is simple Always allocate memory in sequence A few things have to be recorded The address of the next free byte The starting address for each function s memory The line number of each function call in the calling function

The process for allocating main memory on the stack is simple Always allocate memory in sequence A few things have to be recorded The address of the next free byte The starting address for each function s memory The line number of each function call in their calling functions So the program can continue where it left off

Fast The OS does not have to search for free space as variable is inserted at the next byte Variables are found at the byte address given in the symbol table Releases memory automatically When a function terminates its memory can be reused by resetting the next free byte Space efficient There is very little administrative overhead

Variable size cannot change The value of variables can change but more space cannot be allocated to an existing variable Since it would overwrite the next variable on the stack It might be useful to change the size of array variables That store multiple values Memory is only released when a function ends Variables declared at the start of a function call may not be needed for the lifetime of the function

We have used pass-by-value to give information to functions The data passed to a function is copied into the function s parameters This requires time to make a copy of the passed value Trivial if we are passing an integer Less trivial if we are passing a 100MB image

Let's say we want to write a function to swap the values in two variables Here is a first attempt void swap(int x, int y) { int temp = x; x = y; y = temp; Does this work?

// Calling code (in main) int a = 23; int b = 37; swap(a, b); void swap(int x, int y) { int temp = x; x = y; y = temp; 23 37 23 37 a b x y x and y are parameters of the swap function so have their own space in main memory

// Calling code (in main) int a = 23; int b = 37; swap(a, b); void swap(int x, int y) { int temp = x; x = y; y = temp; 23 37 23 37 23 37 23 a b x y temp

// Calling code (in main) int a = 23; int b = 37; swap(a, b); void swap(int x, int y) { int temp = x; x = y; y = temp; 23 note that neither a nor b's values have changed, so the swap function achieved nothing! a 37 b 37 x 23 y 23 temp swap has completed its execution so its memory is released

There is an alternative mechanism for passing variables to a function That allows the argument variables to be affected The function's parameters refer directly to the argument variables The function parameters are preceded by an ampersand (&) Immediately after the type name

// Calling code int a = 23; int b = 37; swap(a, b); void swap(int& x, int& y) { int temp = x; x = y; y = temp; 23 37 a b x y the parameters x and y refer to a and b, rather than storing their values

// Calling code int a = 23; int b = 37; swap(a, b); void swap(int& x, int& y) { int temp = x; x = y; y = temp; 23 37 23 37 23 a b x y temp

// Calling code int a = 23; int b = 37; swap(a, b); void swap(int& x, int& y) { int temp = x; x = y; y = temp; 37 23 23 a b x y temp because swap's parameters are passed by reference a and b have changed swap has completed its execution so its memory is released

The & is used to mean a number of different things, dependent on the context Bitwise and Which we probably won't talk about Pass by reference Return by reference Which we also probably won't talk about The address of operator Which we will talk about later

A function may have both pass by reference and pass by value parameters The pass by value parameters are not preceded by an ampersand Pass by reference parameters must be used when arguments are intended to be changed They are also commonly used when a large object is passed to a function

Function Overloading and Namespaces

Like variables, function names must be unique But we can imagine that the full name of a function includes the types of its parameters So the swap function is called swap-int-int Two functions may be given the same name if they have differently typed parameter lists Or different numbers of parameters This is referred to as function overloading

We can look up the sqrt function On http://www.cplusplus.com/ There are three different versions of sqrt double sqrt (double x); float sqrt (float x); long double sqrt (long double x) Allowing the square roots of these different types to be calculated without losing precision

For convenience our programs often start with #include <iostream> using namespace std; The first line includes the iostream library so that we can use cin and cout The second line allows us to use cin and cout without preceding them with std:: A namespace contains a list of function and constant names

Namespaces are a mechanism to handle multiple functions with the same name There are a number of standard libraries That contain functions with names that are unique to that library However is it quite possible for other libraries to contain functions with the same names Namespaces are a means of coping with this

The using keyword allows functions to be used without their namespace name e.g. using namespace std; Otherwise the function has to be used with its fully qualified name, that includes its namespace e.g. std::cout << " "; using declarations can be made inside individual functions So that they only apply within the body of that function