DOC PREVIEW
IUPUI CSCI 23000 - Functions

This preview shows page 1-2-3-19-20-39-40-41 out of 41 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1IntroductionProgram Modules in CMath Library FunctionsFunctionsFunction DefinitionsSlide 7Function PrototypesExamplesMore Examples:Header FilesCalling Functions: Call by Value and Call by ReferenceSlide 13Taking it slowSimple function in CRepeatedly calling functionsCalling functions from with other control structuresFunctions with input parametersFunctions can return a single resultTop-Down ProgrammingRandom Number GenerationSlide 22Slide 23Example: A Game of ChanceSlide 25Slide 26Storage ClassesSlide 28Slide 29Slide 30Scope Rulesextern Storage Class SpecifierRecursionRecursion (cont.)Recursion vs. IterationExample Using Recursion: FactorialExample Using Recursion: The Fibonacci SeriesSlide 38Recursion vs. IterationSlide 40AcknowledgementsDale RobertsCSCI 230FunctionsDepartment of Computer and Information Science,School of Science, IUPUIDale Roberts, LecturerDale Roberts, [email protected]@cs.iupui.eduDale RobertsIntroductionIntroductionFunctionsFunctionsCategory Programmers Duration Size of CodeTrivialTrivial 1 11-2 weeks1-2 weeks< 500 lines (student homework)< 500 lines (student homework)SmallSmall 1 - 3 1 - 3Few WeeksFew Weeks500 -2000 lines (term projects)500 -2000 lines (term projects)MediumMedium 2 - 5 2 - 5Few MonthsFew Months2000 -10000 (research project)2000 -10000 (research project)LargeLarge 5-25 5-251 - 3 years1 - 3 years10,000-100,000(current applications)10,000-100,000(current applications)Very LargeVery Large 25-100 25-1003 - 5 years3 - 5 years100,000 - 1M (real-time operations)100,000 - 1M (real-time operations)Extremely Large > 100Extremely Large > 100> 5 years> 5 years>1M (advanced military work)>1M (advanced military work)Divide and conquer Divide and conquer Large programs cannot be monolithicLarge programs cannot be monolithicConstruct a program from smaller pieces or components. Construct a program from smaller pieces or components. These smaller pieces are called modulesThese smaller pieces are called modulesEach piece is more manageable than the original programEach piece is more manageable than the original programDale RobertsProgram Modules in CProgram Modules in CFunctionsFunctionsModules in CModules in CPrograms combine user-defined functions with library functionsPrograms combine user-defined functions with library functionsC standard library has a wide variety of functionsC standard library has a wide variety of functionsMath function, I/O, string function, such as Math function, I/O, string function, such as printf()printf(), , scanf()scanf()Function callsFunction callsInvoking functionsInvoking functionsProvide function name and arguments (data)Provide function name and arguments (data)Function performs operations or manipulationsFunction performs operations or manipulationsFunction returns resultsFunction returns resultsWrite function once and call it many timesWrite function once and call it many timesFunction call analogy:Function call analogy:Boss asks worker to complete taskBoss asks worker to complete taskWorker gets information, does task, returns resultWorker gets information, does task, returns resultInformation hiding: boss does not know detailsInformation hiding: boss does not know detailsAlso called EncapsulationAlso called EncapsulationDale RobertsMath Library FunctionsMath Library FunctionsMath library functions Math library functions perform common mathematical calculationsperform common mathematical calculations #include <math.h>#include <math.h>gcc compiler requiresgcc compiler requires –lm –lm parameterparameterFormat for calling functionsFormat for calling functions FunctionName(FunctionName( argumentargument ););If multiple arguments, use comma-separated listIf multiple arguments, use comma-separated list printf( "%.2f", sqrt( 900.0 ) ); printf( "%.2f", sqrt( 900.0 ) ); Calls function Calls function sqrtsqrt, which returns the square root of its argument, which returns the square root of its argumentAll math functions return data type All math functions return data type doubledoubleArguments may be constants, variables, or expressionsArguments may be constants, variables, or expressionsDale RobertsFunctionsFunctionsFunctionsFunctionsModularize a programModularize a programAll variables declared inside functions are local variablesAll variables declared inside functions are local variablesKnown only in function definedKnown only in function definedParametersParametersCommunicate information between functionsCommunicate information between functionsLocal variablesLocal variablesBenefits of functionsBenefits of functionsDivide and conquerDivide and conquerManageable program developmentManageable program developmentSoftware reusabilitySoftware reusabilityUse existing functions as building blocks for new programsUse existing functions as building blocks for new programsAbstraction - hide internal details (library functions)Abstraction - hide internal details (library functions)Avoid code repetitionAvoid code repetitionDale RobertsFunction DefinitionsFunction DefinitionsFunction definition formatFunction definition formatreturn-value-type function-name( parameter-list )return-value-type function-name( parameter-list ){{ declarations and statements declarations and statements} } Function-name: any valid identifierFunction-name: any valid identifierReturn-value-type: data type of the result (default Return-value-type: data type of the result (default intint))voidvoid –– indicates that the function returns nothing indicates that the function returns nothingAn unspecified return-value-type is always assumed by the compiler to be An unspecified return-value-type is always assumed by the compiler to be intintParameter-list: comma separated list, declares parametersParameter-list: comma separated list, declares parametersA type must be listed explicitly for each parameter, unless the parameter is of type A type must be listed explicitly for each parameter, unless the parameter is of type intintDeclarations and statements: function body (block)Declarations and statements: function body (block)Variables can be declared inside blocks (can be nested)Variables can be declared inside blocks (can be nested)Functions can not be defined inside other functionsFunctions can not be defined inside other functionsReturning controlReturning controlIf nothing returned If nothing returned return;return; or, until reaches right braceor, until reaches right braceIf


View Full Document

IUPUI CSCI 23000 - 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?