Chapter 7 - Functions - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 7 - Functions

Description:

return (b); Returning Value From main. void type function do not return values ... rand( ) and srand ( ) work together. srand ( ) automatically 'seeds' function rand ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 31
Provided by: RalphFTo5
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7 - Functions


1
Chapter 7 - Functions
2
Functions
  • Code group that performs single task
  • Specification refers to what goes into and out of
    function
  • Design refers to functions task(s)
  • Two groups
  • Library
  • Programmer-defined

Lesson 7.1
3
Function Categories
  • Return no value
  • Return a single value
  • Use pass by reference

Lesson 7.1
4
Function Basics
  • Need three things to use function
  • Function declaration (prototype)
  • Indicates function exists and describes it
  • Must be done before use of function
  • Function call (invocation)
  • Causes control to transfer from function to
    function
  • Function definition
  • Header line followed by function body enclosed in
    braces

Lesson 7.1
5
Function Declaration
  • Indicates function name, return type and types of
    values passed to function
  • General form
  • ftype fname (atype, atype)
  • ftype is function return type
  • fname is function name
  • atype is argument type (may be different)
  • multiple argument separated by commas

Lesson 7.1
6
Function Call
  • Transfers program control to function
  • General form
  • fname (exp1, exp2)
  • fname is function name
  • exp1 and exp2 are arguments
  • Variables or constants
  • Called argument list

Lesson 7.1
7
Function Definition
  • Includes functions executable statements
  • General form (example with two arguments)
  • ftype fname (atype aname, atype aname)

Lesson 7.1
8
Argument Lists
  • Lists in function call and function header must
    coordinate
  • number
  • order
  • type
  • Example

kinetic_energy (mass, velocity) void
kinetic_energy (int m, double v)
Lesson 7.1
9
Function Arrangement
  • Not necessary for first function to be main
  • Function definition must be located outside body
    of any other function
  • Can write function declaration outside body of
    all functions or within function
  • if within, can only be called from function where
    declared
  • Function declaration MUST appear before call

Lesson 7.1
10
Function Storage
  • When function called, memory allocated for
  • Variables in argument list
  • Variables declared in function body
  • When completes execution, memory freed and
    variable values lost
  • Can prevent lost and maintain
  • Called multiple times, allocated and freed
    repeatedly

Lesson 7.1
11
Common Errors
  • Argument order not matching between function call
    and header
  • Mismatching data types
  • Pass more information than function needs to
    complete task

Lesson 7.1
12
Returning a Value
  • Two items needed
  • appropriate return type for function
  • return statement in function
  • Function declaration and definition list return
    data type
  • int fact(double) int fact(double arg)
  • return statement form
  • return expression

Can put expression in ( ).
Lesson 7.2
13
Return Statement
if (expression) return (a)
else return (b)
  • Considered to be jump statement
  • Can appear anywhere in function body
  • More than one return statement can appear in
    function

Lesson 7.2
14
Returning Value From main
  • void type function do not return values
  • control transfers back to calling function
    automatically
  • int main ( )
  • return (0)
  • Returns value 0 to operating system indicating
    normal termination

Lesson 7.2
15
Recap Pass By Value
  • Default
  • Function called
  • Memory set aside and value copied into new memory
    location
  • Calculations done inside function using value
  • When function finished executing, memory location
    destroyed

Lesson 7.2
16
Pass By Reference
  • Use argument list to allow function to directly
    use calling function's values
  • Reference symbol required in function
    declaration and header
  • Indicate arguments that will have values modified
  • Create aliases for original variable names

Lesson 7.3
17
Pass By Reference
  • Example

void func_name (int, double, double,
int) func_name (x, y , z, a) void func_name
( int b, double c, double d, int e)
Lesson 7.3
18
Scope
  • Refers to region in which declaration is active
  • Three kinds of scope
  • Block
  • Variable valid with block of code enclosed in
    braces
  • Function
  • Variable valid only within function where defined
  • File
  • Variable valid throughout all modules in file
  • Determined by identifier's declaration location

Lesson 7.4
19
Scope
n
j
Lesson 7.4
20
Storage Classes
  • Allows manual modification to scope and storage
    rules
  • Stated in the declaration
  • Four specifiers
  • register store in register
  • auto memory freed after function executes
  • static memory persists after function executes
  • extern global variables among files

Lesson 7.5
21
static
  • Variable maintains storage space and value after
    function finishes executing
  • Memory reserved and initialized only once
  • First time function called
  • General form static type variable
  • Can be initialized in declaration
  • Done once, does not initialize on other calls

Lesson 7.5
22
Comparing global and static Variables
  • Similarity
  • Permanent storage created for both
  • Difference
  • Scope
  • Static variables accessed only from function in
    which declared
  • Global variables accessed from any function
  • With extern from any file

Lesson 7.5
23
extern
  • Multiple files help manage large programs
  • Each compiled separately then linked to create
    executable file
  • Share variable
  • Variable declared as global without specifier in
    one file
  • Other files extern type variable

Lesson 7.5
24
Default Arguments
  • Argument assigned particular value when argument
    omitted in function call
  • Values specified in function declaration
  • Must have ordinary argument listed prior to
    default arguments

type function_name ( ordinary arguments,
default arguments)
Lesson 7.6
25
Using Default Arguments
  • Calling function
  • Must be called with at least the number of
    ordinary arguments

void commute (double, double 15.0, int 8)
commute ( 40.0 )
Cannot specify first and third anduse default
for second must haveall defaults last!
Call uses 40.0 for first argument, then
defaultvalues 15.0 for second argument and 8
for third
Lesson 7.6
26
Function Overloading
  • Defining two or more functions with same name
  • Each function takes different number of arguments
  • C knows which function is being called by
    counting number of arguments used
  • Should not use default arguments since ambiguity
    could result

Lesson 7.7
27
Generating Random Numbers
  • Need three functions
  • rand ( )
  • Returns pseudorandom integer in range 0 to 32767
  • srand ( )
  • Operates with rand( ) using global variable
    invisible to programmer
  • time ( )
  • Returns number of seconds from midnight
  • Need mod () operator to restrict range

Lesson 7.8
28
Generating a Random Number
  • rand( ) and srand ( ) work together
  • srand ( ) automatically "seeds" function rand ( )
  • Functions linked through global variable
  • time ( ) used to get true random effect

srand (time (0)) rand ( )
Call to createrandom number
Lesson 7.8
29
Random Number in Specific Range
  • Use mod operator
  • Example n rand ( )
    roll (n 6) 1

Simulate roll of die so result should be integer
from 1 to 6
Lesson 7.8
30
Summary
Learned how to
  • Define and call functions
  • Determine the scope of a variable
  • Pass values by reference
  • Overload functions
  • Create random numbers

Chapter 7
Write a Comment
User Comments (0)
About PowerShow.com