Unformatted text preview:

Slide 1Slide 2Slide 3Deciding on a FunctionSlide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Why use functions…?Slide 18Disadvantages of functions…?Slide 20Slide 21Slide 22Function callsFunction PrototypesParameters vs ArgumentsThe return statementControl Flow & Function CallsHow are arguments passed?Call by value vs Call by referenceAdvantages/disadvantages between the two?Slide 31Modularization of your projectsModularization of your projectsModularization of your projectsSlide 35Slide 36Slide 37Slide 38Slide 39Key takeawaysFunctionsDepartment of Computer ScienceGeorge Mason UniversityHAMZA MUGHAL HMUGHAL2@GMU,EDUPrologue: The Big PictureAct I: FunctionsFunction definitions, function calls, function prototypesParametersArgumentsParameter passingAct II: ScopeScope resolutionEpilogue: Take awaysHAMZA MUGHAL HMUGHAL2@GMU,EDUPrologue: The Big PictureHAMZA MUGHAL HMUGHAL2@GMU,EDUDeciding on a FunctionRobot get me a sodaWhat series of steps should our robot take?HAMZA MUGHAL HMUGHAL2@GMU,EDURobot get me a soda Robot walk to kitchen door Robot open kitchen door Robot walk to fridge Robot open fridge door Robot pick up soda Robot close fridge door Robot walk to me Robot give me the sodaNow that we’ve determined an algorithm for our robot, how do we expect it to actually do these things for us now?HAMZA MUGHAL HMUGHAL2@GMU,EDURobot get me a soda Robot walk to kitchen door Robot open kitchen door Robot walk to fridge Robot open fridge door Robot pick up soda Robot close fridge door Robot walk to me Robot give me the sodaHAMZA MUGHAL HMUGHAL2@GMU,EDURobot get me a soda walk(robot, kitchen door) Robot open kitchen door walk(robot, fridge door) Robot open fridge door Robot pick up soda Robot close fridge door walk(robot, me) Robot give me the sodaHAMZA MUGHAL HMUGHAL2@GMU,EDURobot get me a soda walk(robot, kitchen door) Robot open kitchen door walk(robot, fridge door) Robot open fridge door Robot pick up soda Robot close fridge door walk(robot, me) Robot give me the sodaHAMZA MUGHAL HMUGHAL2@GMU,EDURobot get me a soda… walk(robot, kitchen door) open(robot, kitchen door) walk(robot, fridge door) open(robot, fridge door) Robot pick up soda close(robot, fridge door) walk(robot, me) Robot give me the sodaHAMZA MUGHAL HMUGHAL2@GMU,EDURobot get me a soda… walk(robot, kitchen door) open(robot, kitchen door) walk(robot, fridge door) open(robot, fridge door) Robot pick up soda close(robot, fridge door) walk(robot, me) Robot give me the sodaHAMZA MUGHAL HMUGHAL2@GMU,EDURobot get me a soda… walk(robot, kitchen door) open(robot, kitchen door) walk(robot, fridge door) open(robot, fridge door) pick_up(robot, soda) close(robot, fridge door) walk(robot, me) give(robot, soda, me)Each one of these functions has a given name that performs a series of actions using values that may be passed inWhat are these “actions” and “values”?Great! Now lets put names into our algorithm into pseudocodeHAMZA MUGHAL HMUGHAL2@GMU,EDUwhile(not at place):walk(robot, place)if(door):open(robot, door) pick_up(robot, soda)if(door):close(robot, door)while(not at person):walk(robot, person)if(door):walk(robot, through door)close(robot, door)give(robot, soda, person)These functions are essentially building blocks to help us perform a series of actions (in this instance, making the robot grab us a soda)Okay robot… now go grab me a bookHAMZA MUGHAL HMUGHAL2@GMU,EDUwhile(not at place):walk(robot, place)if(door):open(robot, door) pick_up(robot, book)if(door):close(robot, door)while(not at person):walk(robot, person)if(door):walk(robot, through door)close(robot, door)give(robot, book, person)Okay robot… now go grab me my phone…Wait…HAMZA MUGHAL HMUGHAL2@GMU,EDUvoid fetch (robot, thing, place, person):while(not at place):walk(robot, place)if(door):open(robot, door) pick_up(robot, thing)if(door):close(robot, door)while(not at person):walk(robot, person)if(door):walk(robot, through door)close(robot, door)give(robot, thing, person)Now our robot can truly do our bidding… or can it?HAMZA MUGHAL HMUGHAL2@GMU,EDUAct 1: FunctionsHAMZA MUGHAL HMUGHAL2@GMU,EDUWe can define a function that:can accept incoming valuescan perform different “effects” such as printing or updating memorymay return a valueWe can call a function however many times we wish that:accepts any expected valuesmay return a valueHAMZA MUGHAL HMUGHAL2@GMU,EDUWhy use functions…?HAMZA MUGHAL HMUGHAL2@GMU,EDUFunctions allow us to:Simplify code by reducing redundancyWhat if we have to modify that code?Enable code reuseEasier to understandDecompose complex problems into simpler piecesImprove program readability & maintenancePerform same operations, but with different “starting values” (arguments)HAMZA MUGHAL HMUGHAL2@GMU,EDUDisadvantages of functions…?HAMZA MUGHAL HMUGHAL2@GMU,EDUFunction calls add memory and time overheadHAMZA MUGHAL HMUGHAL2@GMU,EDUFunctions have two major components:The function headerThe function bodyFunction header contains information such as:type, function name, and optional parameters (each parameter is denoted by an associated type)type function_name(type arg0, type arg1,…, type arg_n)if no type is provided for the argument, then defaults to the int typeHAMZA MUGHAL HMUGHAL2@GMU,EDUThe function bodyDenotes the actions that the function performsIndicated by the starting { and enclosing }User defined functions vs built-in functionsHAMZA MUGHAL HMUGHAL2@GMU,EDUFunction callsSyntax:func_name(argument_expressions);Compiler finds the definition of func_name (either defined before main() or after using function prototypes), then uses the values of arguments passed in to give values to the parameters which are evaluated left to right which then runs the body of the called functionHAMZA MUGHAL HMUGHAL2@GMU,EDUFunction Prototypestype function_name (type arg 1, ..., type arg n);Or type function_name (type, ..., type); Default Argument Promotions…HAMZA MUGHAL HMUGHAL2@GMU,EDUParameters vs ArgumentsThe function definition names parameters (which are variables, local to the function)Note that a function definition does not need to have any parametersThe function call supplies values (i.e. “arguments”) for those parametersEach function call supplies new “arguments”HAMZA MUGHAL HMUGHAL2@GMU,EDUThe return statement The return keyword statement lets us immediately leave a function call with a result Multiple return statements are allowed, but only one value is returnedHAMZA MUGHAL


View Full Document

MASON CS 262 - Functions

Download 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 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 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?