60 likes | 79 Views
Functions, Scope, and The Free Store. Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type , type, …); // Strong type checking The return_type :
E N D
Functions, Scope, and The Free Store • Functions • Functions must be declared by a function prototype before they are invoked, • return_type Function_name(type , type, …); // Strong type checking • The return_type: • A function that does not return a value should declare a return_type of void. • A return statement is not necessary in a function with a void return type (however, • it can still be used to terminate the function), void Fun(){ if(cond) return; ….} • The default return_type is int (e.g., in function main(), return 0; is found) • Neither an array or a function can be specified as a return_type, • int[10] fun(); // Error. • The Argument_list: • Functions are allocated storage on a structure called the program’s run-time stack, • this stack is automatically popped when the function terminates. • Arguments are passed by-value (the entire object is copied), or by-reference (only • the address of the argument is copied) • Arrays are passed by reference using a pointer to the zeroth element of the array • Arguments can have default values, and functions can be overloaded
Functions, Scope, and The Free Store • Default Values for the Argument_list • A function may specify default values for all or only a subset of its arguments, • int fun(int i= 0);// a function prototype with a default argument initializer • int fun(int i = 0){//fun body….} // Error, a default value must be specified • only once in a file, by convention it should be in the declaration • The right most argument must have a default value before any argument to its left, • fun(int i= 0, double x); // Error, default order is incorrect • fun1(double x, int i= 0, char* str); // Error, the right most is not initialized • fun1(double x, int i=0; char* str= “No Free Lunch”); // OK • the invocation fun1(0.05, , “Hello”); // gives syntax error • Function Overloading • Several functions can be given the same name provided that each have a unique • Argument_list, e.g. the following functions can exist in the same file, • int max(int ,int); // returns the max of two integers • int max(const int * , int); // the first argument is a pointer • int max(const List &); // returns the max of a List type object • Avoid Ambiguous overloading: max(const int, int); // ambiguous with max(int,int) • print(unsigned int); print (int); // suppose we have the invocation print(100);
Functions, Scope, and The Free Store Identifier Scoping and Storage Classes The scope of identifiers has three types, 1. File Scope: identifier is declared in the file and not within a class or within a function 2. Local scope: identifiers are declared within a function 3. Class scope: identifiers are declared within a class For example, enum Boolean {false,true}; // Boolean has a file scope class X{ // X has a file scope Boolean fun(); // fun has a class scope } const int Max = 100; // Max has a file scope X x_obj; // x_obj has a file scope main(){ Boolean Quit; // Quit has a local scope int fun();// fun has local scope} Boolean X::fun(){…} int fun(){…..}// now fun has the file scope // the above is not function overloading
Functions, Scope, and The Free Store • File Scope • The outer most scope which encloses both local scope and class scope • The scope extends from the point of declaration in the file to the end of the file • Storage classes static and extern are used to specify whether the same identifier • can be declared and used in other files, e.g., • in file x.cpp: extern const int Max = 100; // Max can be used in other files • static int Max_size=1000; // Max_size can not be used in other • // files, i.e., it does not have external linkage • in file y.cpp: extern const int Max; // this does not allocate storage for Max • // Max has already been defined in x.cpp • extern int Max_size; // Error Max_size has static file scope • Constants by default are static(i.e., no external linkage), while variables by default • are extern. • Class names can be used in other files but there are conditions or rules for that, • it is better to put the class declarations in a header file and include this file in different • files as shown in the lab. assignment examples • typedef, enum, and template names can not have external linkage
Functions, Scope, and The Free Store Local Scope consists of function scope and block scope function scope: extern const int Max; // global constant max defined and initialized in another file funny(){ int I = 100; // local variable const int Max = :: Max * I; // the scoping operator is used to //access the global Max to initialize the local Max } Block Scope: while(cond){ int I = 0; I = 500; // this I is local to the while block if (cond) {int I = 0; // this is a different I local to the if block} } Static local variables {static int I=0; // I retains its most recent values and stays alive until the // program terminates I++; } // I will contain the number of times this block is executed
Functions, Scope, and The Free Store The following code will not produce a syntax error but will produce a dangling pointer, a pointer that points to no object, int * sqr(int x) { int y; y = x*x; return &y; }// the data object y does not exist after the termination of this function main(){ int * p = sqr(100); // p is a dangling pointer // and is initialized to point to an object which has been destroyed } // the above will different if y is declared as a static storage class using static int y; // y will still exist even after sqr() terminates