L02_review - COMP152 Spring 2010 D.Y. Yeung C+ Review...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
C++ Review Nyhoff: 2,5,10 Deitel: 6,8,25 COMP152 Spring 2010 D.Y. Yeung
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Outline Functions Function calls Pass-by-value Pass-by-reference Passing arrays to functions Recursion Pointers and dynamic objects COMP152 2
Background image of page 2
Functions in a Program Every time a function is called, a “stack” is created. The return of the function removes the stack back to the caller main is the “mother” function C++ programs usually have the following form: // #include statements // function prototypes // main() function // user-defined functions 3 COMP152
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Functions & Memory Every function needs a place to store its local variables. Collectively, this storage is called the stack This storage (memory, aka RAM) is a series of storage locations and their numerical addresses Instead of using raw numerical addresses, we use variables to associate a name to an address (kept track by the compiler) All of the data/variables for a particular function call are located in a stack frame COMP152 4 Memory location void aFunc(int x, int y) { double d1, d2; int i; } x y d2 d1 i
Background image of page 4
Functions & Memory (cont) When a function is called, a new stack frame is set aside Parameters and returned values are passed by copy (i.e., they’re copied into and out of the stack frame) When a function finishes, its entire stack frame is reclaimed void aFunc(int x, int y) { double d1 = x + y; } int main(int argc, const char * argv[]) { int x = 7; aFunc(1, 2); aFunc(2, 3); return 0; } COMP152 5 x y d1 x 7 aFunc main
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Function Prototype The function prototype declares the interface, or input and output parameters of the function, leaving the implementation for the function definition . The function prototype or declaration has the following syntax: <type> <function name>(<type list>); Example: A function that prints the card (J) given the card number (11) as input: void printcard(int); (This is a void function - a function that does not return a value) 6 COMP152
Background image of page 6
return and exit A function may return values to its environment return exit return returns a value to the caller function return(-1), return(i+j), etc. Control is given to the caller to execute the exit statement Memory as occupied by the function is reclaimed In main() , calling return basically exits the program (returns to the caller which is the operating system) exit returns control and value directly to the operating system exit(-1), exit(i+j), etc. Usually in case of error or you want to exit the program early without going back to main() The whole program exits and the whole memory space is reclaimed by the OS The return code may be used to signal to the OS on the state/error encountered before exiting 7 COMP152
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Function Definition The function definition can be placed anywhere in the program after the function prototype.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/25/2010 for the course COMP COMP152 taught by Professor D.y.yeung during the Spring '10 term at HKUST.

Page1 / 72

L02_review - COMP152 Spring 2010 D.Y. Yeung C+ Review...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online