5_Functions

5_Functions - Functions in C With LC-3 as an example CMPE13...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPE13 Cyrus Bazeghi Functions in C With LC-3 as an example
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CMPE13 F UNCTIONS Smaller, simpler, subcomponents of programs Provide abstraction hide low-level details give high-level structure to program, easier to understand overall program flow enables separable, independent development C functions zero or multiple arguments passed in single result returned (optional) return value is always of a particular type In other languages are called procedures, subroutines, . .. 2
Background image of page 2
CMPE13 E XAMPLE OF HIGH - LEVEL STRUCTURE main() { SetupBoard(); /* place pieces on board */ DetermineSides(); /* choose black/white */ /* Play game */ do { WhitesTurn(); BlacksTurn(); } while (NoOutcomeYet()); } Structure of program is evident, even without knowing implementation. 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CMPE13 F UNCTIONS IN C Declaration (also called prototype ) int Factorial(int n); Function call , used in expression a = x + Factorial(f + g); type of return value name of function types of all arguments 1. evaluate arguments 2. execute function 3. use return value in expression 4
Background image of page 4
CMPE13 F UNCTION DEFINITION State type, name, types of arguments must match function declaration give name to each argument ( doesn't have to match declaration ) gives control back to calling function and returns value int Factorial(int n) { int i; int result = 1; for (i = 1; i <= n; i++) result *= i; return(result); } 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CMPE13 W HY DECLARATION ? Since function definition also includes
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 08/23/2010.

Page1 / 19

5_Functions - Functions in C With LC-3 as an example CMPE13...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online