Week 6 Functions Part 2 BJ Furman 26SEP2009 The Plan for Today Review of functions Scope of variables Program global scope File scope Block scope Function scope Functions that return more than one value Function Example Function Practice Learning Objectives Explain the structure of a function Explain the concept of variable scope Explain the method by which functions can return more than one value Functions Definition Structure Function header Return data type void if no value is returned Name type function name type arg1 type arg1 statements Descriptive Arguments void if no data is passed into the function Statements Variable declaration Operations Return value if any Parentheses around the expression to return is optional double product double x double y double result result x y return result Functions Example Function prototype Remember semicolon Function definition Function call Function return optional for functions of type void or just return can have multiple return statements can use anywhere in the function body include stdio h function prototype double product double x double y int main double var1 3 0 var2 5 0 double ans ans product var1 var2 printf var1 2f n var2 2f n var1 var2 printf var1 var2 g n ans function definition double product double x double y double result result x y return result Scope of Variables in Functions 0 Scope of a variable declaration The region of the program that the declaration is active Four types of scope Program global File Block Function Scope of Variables in Functions 1 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 infrequently and carefully ex from Ch include stdio h int a 10 double product double x double y int main double var1 3 0 var2 5 0 double ans ans product var1 var2 printf var1 2f n var2 2f n var1 var2 printf var1 var2 g n ans function definition double product double x double y double result result x y return result Scope of Variables in Functions 2 Scope of a variable declaration cont File scope if declared outside of all functions and uses static keyword Visible to all functions from point of declaration in this source file only include stdio h static int a 10 double product double x double y int main double var1 3 0 var2 5 0 double ans ans product var1 var2 printf var1 2f n var2 2f n var1 var2 printf var1 var2 g n ans function definition double product double x double y double result result x y return result Scope of Variables in Functions 3 Scope of a variable declaration cont Block local scope Block is a series of statements enclosed in braces if declared in a block scope is limited to the block blocks can use the same variable name and not interfere with variables having the same name ex from Ch include stdio h double product double x double y int main int a 10 double var1 3 0 var2 5 0 double ans ans product var1 var2 printf var1 2f n var2 2f n var1 var2 printf var1 var2 g n ans function definition double product double x double y double result result x y return result Scope of Variables in Functions 4 Scope of a variable declaration cont Function scope include stdio h int main int user sel Applies only to goto label names Active from beginning to end of a function Ex Statement labels in a switch selection structure prompt user for entry get user entry switch user sel case 1 printf n message call game function1 here break case 2 printf n message call game function2 here break default printf Error break Scope of Variables in Functions 5 Scope of a variable declaration cont Function prototype Parameters with names are not considered to be declared outside the prototype Thus names in the prototype do not have to match names in the function definition match types however include stdio h double product double x double y int main int a 10 double var1 3 0 var2 5 0 double ans ans product var1 var2 printf var1 2f n var2 2f n var1 var2 printf var1 var2 g n ans function definition double product double A double B double result result A B return result Function Scope Practice 1 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 Passing Arguments into Functions How are the arguments passed into functions Pass by value function arguments are expressions In the function call 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 var1 3 0 var2 5 0 double ans ans product var1 var2 printf var1 2f n var2 2f n var1 var2 printf var1 var2 g n ans function definition double product double A double B double result result A B return result Functions Returning Multiple Values Instead of product prod sum How can I get both product and sum returned 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 prod double sum int main double var1 3 0 var2 5 0 double prod sum prod sum var1 var2 prod sum printf var1 g n var2 g n var1 var2 printf prod g n sum g n prod sum function definition void prod sum double A double B double prod double sum prod A B sum A B Returning Multiple Values Practice 2 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 Review References
View Full Document