Slide 1Functions Support DecompositionProgram Modules in CMath Library FunctionsFunctionsFunction DefinitionsFunction PrototypesSlide 8ExamplesAcknowledgementsDale RobertsCSCI 230 Functions DeclarationsDepartment of Computer and Information Science,School of Science, IUPUIDale Roberts, LecturerDale Roberts, [email protected]@cs.iupui.eduDale RobertsFunctions Support DecompositionFunctions Support DecompositionFunctionsFunctionsCategory 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. These Construct a program from smaller pieces or components. These smaller pieces are called modulessmaller 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 something returned If something returned returnreturn expressionexpression;;Dale RobertsFunction PrototypesFunction PrototypesFunction prototype Function prototype Function nameFunction nameParameters Parameters –– what the function takes in what the function takes inReturn type Return type –– data type function returns (default data type function returns (default intint))Used to validate functionsUsed to validate functionsPrototype only needed if function definition comes Prototype only needed if function definition comes
View Full Document