Week 6 Functions Part 2 BJ Furman 01
Week 6: Functions - Part 2 BJ Furman 01 OCT 2012
The Plan for Today n n n Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers ¨ ¨ ¨ n n n Program (global) scope File scope Function prototype Function scope Block scope How to make functions modify more than one value Function Example Function Practice
Learning Objectives n Explain the structure of a function n Explain the concept of identifier scope n Explain the method by which functions can modify more than one value
Functions - Example n Function prototype ¨ n n n Remember semicolon!! Function definition Function call Function return ¨ optional for functions of type void n ¨ ¨ ¨ or just, return; can have multiple return statements can use anywhere in the function body Note: only one value can be returned #include <stdio. h> /* function prototype */ double product(double x, double y); int main(){ double var 1 = 3. 0, var 2 = 5. 0; double ans; ans = product(var 1, var 2); printf("var 1 = %. 2 fn" "var 2 = %. 2 fn", var 1, var 2); printf("var 1*var 2 = %gn", ans); return 0; } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }
Functions - Definition Structure n Function 'header' ¨ Return data type n ¨ Name n ¨ ¨ ¨ statements; } Descriptive void if no data is passed into the function Statements ¨ { Arguments n n void if no value is returned type function_name (type arg 1, ) Variable declaration Operations Return value (if any) n Parentheses around the expression to return is optional double product(double x, double y) { double result; result = x * y; return result; }
Identifiers and Scope n Identifier ¨ The name of a variable, function, label, etc. int my_var 1; n pow_table(); n start: n n /* a variable */ /* a function */ /* a label */ Question: ¨ Does it make a difference where in a program an identifier is declared? YES! --> concept of ‘scope’
Scope of Identifiers n Scope of a declaration of an identifier ¨ n The region of the program that the declaration is active (i. e. , can access the variable, function, label, etc. ) Five types of scope: ¨ ¨ ¨ Program (global scope) File Function prototype Function Block (“between the { } scope”)
Scope of Identifiers - Program Scope n Program (global) scope ¨ ¨ ¨ if declared outside of all functions "Visible" to all functions from point of declaration Visible to functions in other source files Use only when necessary and then very carefully!! ex. from Ch n var_scope. c #include <stdio. h> int a = 10; double product(double x, double y); int main() { double var 1 = 3. 0, var 2 = 5. 0; double ans; ans = product(var 1, var 2); printf("var 1 = %. 2 fn" "var 2 = %. 2 fn", var 1, var 2); printf("var 1*var 2 = %gn", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }
Scope of Identifiers - File Scope n File scope ¨ Keyword static n ¨ Makes variable a ‘visible’ only within this source file Use file scope to avoid naming conflict if multiple source files are used #include <stdio. h> static int a = 10; double product(double x, double y); int main() { double var 1 = 3. 0, var 2 = 5. 0; double ans; ans = product(var 1, var 2); printf("var 1 = %. 2 fn" "var 2 = %. 2 fn", var 1, var 2); printf("var 1*var 2 = %gn", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }
Scope of Identifiers - Function Prototype Scope n Function prototype scope ¨ ¨ Identifiers x and y are not visible outside the prototype Thus, names in the prototype do not have to match names in the function definition n MUST match types, however! #include <stdio. h> double product(double x, double y); int main() { int a = 10; double var 1 = 3. 0, var 2 = 5. 0; double ans; ans = product(var 1, var 2); printf("var 1 = %. 2 fn" "var 2 = %. 2 fn", var 1, var 2); printf("var 1*var 2 = %gn", ans); } /* function definition */ double product(double A, double B) { double result; result = A * B; return result; }
Scope of Identifiers - Function Scope n Function scope ¨ Applies only to labels start: #include <stdio. h> int main() { int user_sel; * * * /* prompt user for entry */ /* get user entry */ switch( user_sel ) { case 1: printf("n message. . . "); /* call game function 1 here */ break; case 2: printf("n message. . . "); /* call game function 2 here */ break; default: printf("Error"); break; goto start; ¨ ¨ Active from the beginning to the end of a function Ex. Statement labels in a switch selection structure }
Scope of Identifiers - Block Scope n Block (local) scope ¨ ¨ A block is a series of statements enclosed in braces { } The identifier scope is active from the point of declaration to the end of the block ( } ) Nested blocks can both declare the same variable name and not interfere ex. from Ch n n var_scope_block. c scope_nested_blocks. c #include <stdio. h> double product(double x, double y); int main() { int a = 10; double var 1 = 3. 0, var 2 = 5. 0; double ans; ans = product(var 1, var 2); printf("var 1 = %. 2 fn" "var 2 = %. 2 fn", var 1, var 2); printf("var 1*var 2 = %gn", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }
Storage Duration n n How long the identifier exists in memory Static storage class ¨ Identifier exists when program execution begins n For variables: ¨ ¨ Storage allocated and variable is initialized once Retains their values throughout the execution of the program #include <stdio. h> void just_count(void); /* proto */ int main() { int i; for(i=0; i<10; i++) { just_count(); } return 0; } void just_count(void) { static int count_a; int count_b; count_a = count_a + 1; count_b = count_b + 1; printf("count_a== %dt", count_a); printf("count_b== %dn", count_b); } just_count. c
Storage Duration, cont. n For functions: ¨ n function name exists when execution begins For variables with global scope: ¨ ¨ ¨ i. e. , declared outside of all functions and uses static keyword "Visible" to all functions from point of declaration in this source file only Keeps data ‘private’ to this file only #include <stdio. h> static int a = 10; double product(double x, double y); int main() { double var 1 = 3. 0, var 2 = 5. 0; double ans; ans = product(var 1, var 2); printf("var 1 = %. 2 fn" "var 2 = %. 2 fn", var 1, var 2); printf("var 1*var 2 = %gn", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }
Scope of Identifiers - Practice 1 n What kind of scope do the variables have? ¨ ¨ i j m k #include <stdio. h> int i; static float m; int k=10; int main() { int j; for(j=0; j<5; ++j) { printf("j= %d", j); } { int k=7; printf("k= %d", k); } }
Review of Scope n Five types of scope: ¨ Program (global scope) ¨ File (just in the immediate source file) ¨ Function-prototype (just in the prototype) ¨ Function (applies only to labels) ¨ Block (“between the { } scope”) n Static keyword ¨ ¨ ¨ identifiers start to exist at execution variables retain their value throughout variables with global scope are private to the file they are declared in
Memory n n n Stores program instructions and data (variables, etc. ) 0 1 1 0 0 x 10 FF 0 1 0 1 0 0 0 x 1100 0 0 x. FFFF Bit 7 6 5 4 3 2 1 (Can represent using two hexadecimal digits) 0 Each location has an ‘address’ Each location stores the information as ‘bits’ ¨ Binary ____its n ¨ ¨ ¨ Zero or one Memory (8 -bit) Address 0 x 10 FE 8 bits is one byte Information is ‘coded’ (ex. ASCII) Memory is ‘written’ or ‘read’
ASCII Code
Extended ASCII Code
Variables and Memory n int var 1 = 0; Variables in general ¨ Variable declaration informs compiler of two things: Name of the variable 2. Data type of the variable n Actions caused: 1. ¨ ¨ ¨ Bytes allocated in memory Symbol table with name, address, and value Can think of a variable as having two "values" Value of what is stored at the memory location (rvalue) n Value of the memory location (its address) (lvalue) Memory (8 -bit) Address 0 x 10 FE 0 0 0 0 0 x 10 FF 0 0 0 0 0 x 1100 0 0 x 1101 0 0 0 0 7 6 5 4 3 2 1 0 Bit Symbol Table n Var name Var type Var address Var value var 1 int 10 FE 0
Assigning and Using Variables short int i, j; n Assigning a value to a variable (i = 3; ) ¨ n Value is copied to the address listed in the symbol table Using a variable in an expression (j = i; ) ¨ Accesses the value of what is stored at the memory location is accessed i = 3; j = i; Memory (8 -bit) Address 0 x 10 FE 0 0 0 0 0 x 10 FF 0 0 0 1 1 7 6 5 4 3 2 1 0 Bit Big-endian Symbol Table Name Type Address i short int 10 FE j short int 1100 Value
Variables that store addresses n We can define variables that store memory addresses ¨ Such a variable is called a “pointer”
Passing Arguments into Functions n How are the arguments passed into functions? ¨ 'Pass by value' function arguments are expressions ¨ In the function call: ¨ n n ¨ Expressions are evaluated and copies of their values are put into temporary memory locations The names of the corresponding parameters in the function definition are made to be the names of the copies The values of the expressions in the function call are not changed #include <stdio. h> double product(double x, double y); int main() { int a = 10; double var 1 = 3. 0, var 2 = 5. 0; double ans; ans = product(var 1, var 2); printf("var 1 = %. 2 fn" "var 2 = %. 2 fn", var 1, var 2); printf("var 1*var 2 = %gn", ans); } /* function definition */ double product(double A, double B) { double result; result = A * B; return result; }
Functions Affecting Values Indirectly n Instead of product() ¨ ¨ prod_sum() How can I get the function to give both product and sum? n n put * in front of variable name in prototype and function definition put & in front of variable names in function call #include <stdio. h> void prod_sum(double x, double y, double *ptr 1, double *ptr 2); int main() { double var 1 = 3. 0, var 2 = 5. 0; double prod, sum; prod_sum(var 1, var 2, &prod, &sum); printf("var 1= %gn" "var 2= %gn", var 1, var 2); printf("prod= %gn" "sum= %gn", prod, sum); } /* function definition */ void prod_sum(double A, double B, double *rslt_prod, double *rslt_sum) { *rslt_prod = A * B; *rslt_sum = A + B; }
Review Functions pass copies of their arguments into the function n To get multiple values returned n Use pointers in the argument list of the prototype and function definition ¨ Use & in front of variable names associated with pointer arguments in function call ¨
Returning Multiple Values - Practice 2 n Write a function that returns the square and cube of a number ¨ ¨ ¨ Prompts user for the number Prints number, square, and cube Prompt, squarecube calculation, and print must all be functions Steps 1. Pseudocode for program logic • The actions that your program will do • Everyone individually 2 minutes 2. Form into groups of three • Share pseudocode (3 minutes) • Divide up task to build functions 3. Write code
References
- Slides: 27