cs31 lecture 17

gbar stdendl stdcout 51 functions x a function

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: is a variable whose value never Definition: changes after declaration+assignment changes x In a nutshell: a constant variable holds the same value for In the lifetime of the variable the x In C++: use the const keyword before the type x Example: const int toto = 42; const double sqrtpi = sqrt(M_PI)
; const 49 Global variables x They are standard variables, but declared outside of any They function (reminder: main()
 is a function)
 function x Usage: share some program state between functions, Usage: units, and even libraries units, x Can be read/written from anywhere after the declaration x Note: should be avoided as much as possible (technically, Note: can always be avoided)
 can 50 Global variable example int g_foo; int g_bar; int main()
 { g_foo = 42; g_foo g_bar = g_foo + 2; g_bar std::cout << g_bar << std::endl; std::cout } 51 Functions x A function packages a set of instructions together, as a function callable object callable x It may or may not take data as input x It may or may not return data x It may or may not do something x It can be called from anywhere in the program (provided It proper declaration)
 proper 52 Functions in C++ x Two parts: Two 1.. prototype declaration (optional, must come first if it exists)
 1 2. Implementation x Implementation: the actual set of instructions to be Implementation: performed when calling the function performed x Declaration: a description of the funct...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online