Unformatted text preview:

CS262 Lecture 03Chapter 4 FunctionsZoran DuricDepartment of Computer Sciencearray• very similar to java but there are some important differences• int tmp[3];• int tmp[3]={0,0,0};• const int tmp[]={0,0,0};• tmp is a pointer to the first element of the array• cannot use variable to define the size of the array– ex: you cannot say: int x=10; double tmp[x];– this won’t compile, but you can say:– #define x 10, then double tmp[x];2char array (string)• c has no string (class)• a string is simply a “char array” with the last element being null (‘\0’)• ex:– char msg[]=”hello\n”;– char msg[7]=”hello\n”; //size 7 or larger3function• call-by-value– the arguments are local variables whose values are copied from the callers– each function call allocates all these local variables which are placed on the top of the call stack– ex: long ans=fib(n); //in ex4.c• variable n in main function and variable n in fib function are different variables even though they have the same value.– ex: void swap(int a, int b); //won;t work– void swap(int * a, int * b); //need to use pointers4function• Since array variables are pointers so:– char A[]=”GMU”, B[]=”UMD”;– swap(A,B); //call by value– void swap(int X[], int Y[]){...} • X will have address A• Y will have address B• java is also “call-by-value” and “references” (i.e. pointers) are passed when arguments are objects– so, java does have pointers (references), but you cannot manipulate them5scopes• scopes – life span (global, local)– visibility (static, extern)• Life span– variables outside all functions are global variables (has life span of the program)– variables inside a function is local to a function call (does not span different calls) unless “static” is used• int foo(){ static int x=0; printf(“x=%d”,x++); }• call foo multiple times will output different values6scopes• see – static.c7scopes• Visibility (for global variables)– similar to private, protected, public in java/c++– static means “only visible to the file contains that variable” – extern means “visible to the entire program”• this is default for all global variables–8scopes• see – longest-line-2.c – longest-line-3 (dir)9typedef and call-back functions• see call-back.c10typedef and call-back functions• see call-back.c11variadic functions• a function that take arbitrary number of arguments• ex:in c, it can have this prototype:– int foo(char * format, int size, ...);– there must be one fixed parameter– there is “...” to indicate the rest of variables• macros in stdarg.h are used to retrieve the rest of arguments• there is also variadic marco for the same purpose12variadic functions• see


View Full Document

MASON CS 262 - Chapter 4 Functions

Download Chapter 4 Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 4 Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 4 Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?