cs31 lecture 17

gbar stdendl stdcout 51 functions x a function

Info icon This 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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern