University of Malta CSA2090: Lecture 4 © Chris Staff 1 of 20 CSA2090: Systems Programming Introduction to C Dr. Christopher Staff Department of Computer Science & AI University of Malta Lecture 4: Functions & Pointers
University of Malta CSA2090: Lecture 4 © Chris Staff 2 of 20 Aims and Objectives The definition of function allows a C program to be modularised C permits memory locations to be accessed directly as well as through variable names
University of Malta CSA2090: Lecture 4 © Chris Staff 3 of 20 Functions C has no procedures or methods, only functions Functions can take 0, 1, or more parameters or arguments Functions return exactly one value
University of Malta CSA2090: Lecture 4 © Chris Staff 4 of 20 Form of a function ( ) { }
University of Malta CSA2090: Lecture 4 © Chris Staff 5 of 20 Example function int mean(int a, int b) { int tmp; tmp = (a + b) / 2; return tmp; }
University of Malta CSA2090: Lecture 4 © Chris Staff 6 of 20 Function behaviour Functions end when –a closing } is reached –a return statement is reached returns control to the calling function –an exit statement is reached terminates the program –a crash occurs
University of Malta CSA2090: Lecture 4 © Chris Staff 7 of 20 C function arguments All parameters are ‘passed by value’ int main() { int a = 1277, x; … x = foo(a); }
University of Malta CSA2090: Lecture 4 © Chris Staff 8 of 20 C function arguments When foo is entered a copy of a is created int main() { int a = 1277, x; … x = foo(a); } int foo(int a) {… }
University of Malta CSA2090: Lecture 4 © Chris Staff 9 of 20 C function arguments To change the value of a in main() int main() { int a = 1277; … a = foo(a); } int foo(int a) {a = a*2; return a;}
University of Malta CSA2090: Lecture 4 © Chris Staff 10 of 20 C function arguments To change the value of a in main() int main() { int a = 1277; … a = foo(a); } int foo(int a) {a = a*2; return a;}
University of Malta CSA2090: Lecture 4 © Chris Staff 11 of 20 C function arguments To change the value of a in main() int main() { int a = 1277; … a = foo(a); } int foo(int a) {a = a*2; return a;} 1 2
University of Malta CSA2090: Lecture 4 © Chris Staff 12 of 20 Call by reference Being able to return one value means that you can only change the value of one variable To change the values of other variables, we must tell the called function where they exist in memory
University of Malta CSA2090: Lecture 4 © Chris Staff 13 of 20 Pointers C supports reference to a memory address using the & operator &a will return the address of variable a in memory &a is 100 Addresses can be printed using %u
University of Malta CSA2090: Lecture 4 © Chris Staff 14 of 20 Pointers To retrieve or access the value stored at a memory address, use the * operator a is 1277 You can use & with any variable, but * dereferences the value
University of Malta CSA2090: Lecture 4 © Chris Staff 15 of 20 Declaring pointers int *p; // p is pointer to int p = &a; // p contains address of a printf(“%d\n”, *p);
University of Malta CSA2090: Lecture 4 © Chris Staff 16 of 20 Declaring pointers int *p; // p is pointer to int p = &a; // p contains address of a printf(“%d\n”, *p); &p = ? *a = ?
University of Malta CSA2090: Lecture 4 © Chris Staff 17 of 20 Declaring pointers int *p; // p is pointer to int p = &a; // p contains address of a printf(“%d\n”, *p); &p = 200 *a = garbage!! a = 10 *p = ?
University of Malta CSA2090: Lecture 4 © Chris Staff 18 of 20 Declaring pointers int *p; // p is pointer to int p = &a; // p contains address of a printf(“%d\n”, *p); &p = 200 *a = garbage!! p = 10 *p = ?
University of Malta CSA2090: Lecture 4 © Chris Staff 19 of 20 Nice try! int main() { int *p, bar=10; p = foo(bar); } int *foo(int bar){ int x = bar * 2; return &x; }
University of Malta CSA2090: Lecture 4 © Chris Staff 20 of 20 Next lecture Pointers, structures and strings