160 likes | 285 Views
Style. Helpful variable and function names isSomething for the functions/variables indicates a boolean value - generally TRUE/FALSE Use indentation!!! #define ’s Declare them, then use them 36 should not be a magic number. #define it! Bad style from HW 3b
E N D
Style • Helpful variable and function names • isSomething for the functions/variables indicates a boolean value - generally TRUE/FALSE • Use indentation!!! • #define’s • Declare them, then use them • 36 should not be a magic number. #define it! • Bad style from HW 3b • For this homework, we gave you the function headers, but this won’t always be the case. Learn the skill of clear function names! • What does int num2 mean?
Functions & Memory • Every function needs memory/storage for each of its variables. Collectively, the memory reserved for allfunctions is called the stack. • This storage, located in main memory (“RAM”), is a series of numbered addresses and the associated storage space. • Variables are a way of accessing data at these locations. By attaching a symbolic name to each location, it eliminates the need to remember each individual address. • All of the data/variables for a particular function are located in a stack frame. void aFunc(int x, int y) { double a, b; char c; /* etc */ } a b c x Memory location y
Functions & Memory (cont) • When a function is called, a new portion of the stack is set aside for that particular function call’s data/variables. • Parameters and return values are passed by copy • Once the function finished execution, that portion of the stack is reclaimed and the values there are no longer available void aFunc(int x, int y) { double a, b; char c; /* etc */ } int main(void) { char c = ‘h’; int x = 7; aFunc(1, 2); aFunc(2, 3); return 0; } a b c aFunc’svariables x y c ‘h’ main’svariables x
The Turtle's Quest Terry the turtle lives in one-dimensional world (everything along a single line). Terry needs some money for Turtle Wax to polish his shell. There's a big pot of gold available if Terry can just move over there! Solution: terryPosition = moveTerry(GOLD_POSITION); GOLD_POSITION terryPosition
Journey to Another Dimension y What if Terry lived in a two-dimensional world? We need x and y coordinates to specify Terry's new position, but moveTerry can only return one value! If only there were a way to return more than one value from a function… GOLD_Y ??? terryY x terryX GOLD_X
Output Parameters to the Rescue! • If you want to return more than one value from a function, use a special kind of parameter called an output parameter*. • When you pass a variable into a function as an output parameter, that function is allowed to change the value of the original variable! • There is some special syntax for all of this, which can be confusing at first. Focus on the concept, and the syntax will make sense eventually. * Also known as pointer parameters. Pointers are an important but confusing topic, and for now all we need to know is how to use them for output parameters.
The Secret to Pointers Pointers are just variables which store addresses of other variables! void aFunc(int x, int *y) { /* Etc */ } int main(void) { int n; /* Insert amazing code here */ return 0; } x(4104) y(4100) Variable name n(4096) Address Storage space
Pointers - An Overview • Use pointers if you want to: • Modify a function’s argument(s) (This is a way of achieving multiple “return” values!) • Do other cool stuff (take CSE143) • Accessing the data that the pointer refers to is called “dereferencing” • (Very) informally, you’re “following a pointer” • In reality, dereferencing means accessing the data located at the address which the pointer holds x(4104) y(4100) n(4096)
Declaring Pointers <type> * ptrName; “ptrName is a variable which contains the address of a variable of type <type>” For example: char *cPtr; int *nPtr1, *nPtr2; void aFunc( int aParam, int *ptrParam); Using Pointers Dereferencing a pointer: *ptrName “Go to the address contained in the variable ptrName” Getting the address of a variable: &aVar “Get the address of the variable aVar” For example: aFunc(myInt, &anotherInt); anInt = *myPtr * 4; *dinner = eggplant + sauce; Pointer Syntax
The code int *p; int q; *p = 5; Lecture “p’s type is int pointer. q’s type is int.” “Assign 5 to where p points.” Pointers: Putting it all together Section p contains the address of anint. q contains anint. Go to the address that p contains, and place a 5 there. 5 p p (8200) 8196 q (8196) 5
The code void doubleIt(int x, int *p) { *p = 2 * x; } int main(void) { int a = 16; doubleIt(9, &a); return 0; } Lecture Pointers: Putting it all together (cont) Section main p (8200) 8192 16 a doubleIt x (8196) 9 16 main a (8192) myFunc 9 x p
The code void doubleIt(int x, int *p) { *p = 2 * x; } int main(void) { int a = 16; int *b = &a; myFunc(9, b); return 0; } Lecture Pointers: Putting it all together (cont) Section main p (8200) 8192 16 a doubleIt x (8196) 9 b a (8192) 16 myFunc main 8192 b (8188) 9 x p
Pointer pointers (cont) Remember the control-flow example a few weeks ago?Here’s why uninitialized pointers are bad! int *i; if(someCondition) { … i = malloc(aNum); } else if(anotherCondition) { … i = malloc(aNum); } else if(aThirdCondition) { … i = malloc(aNum); } *i = someVariable; Think of malloc() as afunction which initializes pointers
Back to Terry… y moveTerry(int *x, int *y, int newX, int newY) { *x = newX; *y = newY; } How to call it: moveTerry(&terryX, &terryY, GOLD_X, GOLD_Y); GOLD_Y $$$! terryY x That's one shiny turtle! terryX GOLD_X
Arrays • Arrays are merely contiguous memory locations with one name (compare this to a regular variable, which has one location for every name). • When declaring an array, its size must be known at compile-time. myArray[5] myArray[4] myArray[3] myArray[2] myArray[1] myArray[0]
Array Syntax • Arrays are zero-indexed, which means that you can only access elements from 0 to arraySize-1. • This makes starting loop counters at 0 very convenient! int myArray[ARRAY_SIZE]; int index; for(index = 0; index < ARRAY_SIZE; index++) { printf(“%d\n”, myArray[index]); } • For-loops are particularly useful for array accesses