Functions - PowerPoint PPT Presentation

About This Presentation
Title:

Functions

Description:

Makes it easier to build large programs. Makes is easier to ... Creating your own Functions. Creating your own Functions #include stdio.h int square(int) ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 15
Provided by: evank
Category:

less

Transcript and Presenter's Notes

Title: Functions


1
Functions
2
Quick Review
  • What is a Function?
  • A module of code that performs a specific job.
  • Examples
  • Function that determines the maximum of two
    numbers.
  • Function that sorts a list of names.

3
Boss Analogy
  • One function can delegate certain jobs to
    specific functions.

Boss Function
Max Function
Sort Function
4
Important Concept 1
  • Divide and Conquer break large programs into a
    series of smaller functions.
  • Helps manage complexity.
  • Makes it easier to build large programs.
  • Makes is easier to debug programs.

5
Important Concept 2
  • Abstraction most of the time, you need to know
    what a function does, but not how it actually
    does it.
  • Also helps manage complexity.
  • You can use other peoples code, without
    understanding how it works.

6
Using Pre-Packaged Functions
  • Standard C includes lots of pre-packaged
    libraries
  • ctype.h character manipulation
  • math.h mathematical functions
  • stdio.h standard input/output
  • stdlib.h random numbers, memory handling
  • string.h string manipulation
  • time.h date/time functions
  • All of these are detailed in the Book.

7
Function Concepts
  • Function Prototype
  • Function Definition
  • Function Call

8
Function Prototype
  • Tells you what type of data the function is
    expecting, and what type of data the function
    returns.
  • Represents a communication protocol that enables
    two functions to talk.
  • If you want to use a pre-built function, you need
    to learn to read prototypes.

9
sqrt Function
  • double sqrt (double)
  • This function therefore accepts one double value,
    and returns one double value.

Return data type
Function argument data type
10
Using sqrt()
  • include ltstdio.hgt
  • include ltmath.hgt
  • main ()
  • double value
  • value sqrt(9.0)/function call/
  • printf ("f", value)

11
Creating your own Functions
12
Creating your own Functions
  • include ltstdio.hgt
  • int square(int)
  • main ()
  • printf("d ", square(5))
  • int square(int y)
  • return y y

Function Prototype must be declared at top of
program.
Function Call invokes the function
Function Definition contains the actual
function code.
Returns a value back up to main.
13
Understanding the Program
Main Function
Take this Integer!
Heres the answer. Its an integer!
Square function
14
Function Template
  • Use this template for adding functions to your
    program.
  • return-value-type function-name (parameter-list)
  • return-value-type function-name (parameter-list)
  • variable declarations
  • statements of work
  • return statement (optional outside of this
    class)

Function Prototype must be declared at top of
program.
Function Definition contains the actual
function code.
Write a Comment
User Comments (0)
About PowerShow.com