Functions - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Functions

Description:

Build structured programs that are divided into functions. ... How to Build Programs with Functions ... To make your own functions work, you must tell the ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 31
Provided by: southtexa
Category:
Tags: build | functions | how | to

less

Transcript and Presenter's Notes

Title: Functions


1
Chapter 3
  • Functions

2
Objectives
  • Build structured programs that are divided into
    functions.
  • Describe the flow of execution in a program with
    multiple functions.
  • Describe what is meant by the phrase "scope of
    variable."

3
Objectives
  • Pass data to functions.
  • Get values from functions using return.
  • Describe and use library functions.
  • Use common math functions.
  • Use character manipulation functions.

4
How to Build Programs with Functions
  • Thus far, you have only written programs with the
    main() function.
  • Most programs use functions that perform specific
    tasks.
  • Typically the main function will "call" other
    functions as the program needs their
    functionality.

5
Guidelines Using Functions
  • Organization
  • Autonomy
  • Encapsulation
  • Reusability

6
Program Design
  • Top-down design begins with the functions at the
    top of the VTOC and works toward the functions at
    the bottom of the VTOC.
  • Bottom-up design involves beginning with the
    bottom of the VTOC and working your way up.

7
The Syntax of Functions
  • You have seen the main function many times so
    far
  • int main()
  • // body of program
  • return 0

8
The Syntax of Functions
  • When the program reaches the return 0 statement,
    the value zero is returned to the operating
    system, which shows the program ended normally.
  • The value returned is a standard integer because
    we specified an int data type when the main
    function was declared.

9
The Syntax of Functions
  • To prevent a value from being returned, the void
    keyword is used in place of a data type.
  • void main()
  • // body of program

10
Calling Functions
  • An example of a user created function might look
    like the following
  • void print_title()
  • cout
  • cout

11
Calling Functions
  • The main function would then "call" the user
    defined function.
  • int main()
  • print_title() // call to print_title
  •   // insert the program here
  •   return 0

12
Function Prototypes
  • To make your own functions work, you must tell
    the compiler at the top of your program that your
    function exists.
  • You do this by creating a prototype.
  • Basically, a prototype defines the function for
    the compiler.

13
Prototype Example
  • The top part of a program might look like the
    following
  • // 1stfunct.cpp
  • include
  • void print_title()
  • // prototype for print_title
  • // function

Notice the semicolon.
14
Functions and Program Flow
  • When a function is called, the computer executes
    the statements in the function beginning with the
    first statement.
  • When the end of the function is reached, program
    execution resumes with the statement that follows
    the call to the function.

15
Scope of Variables
  • The availability of a variable is known as its
    scope.
  • A local variable is declared within a function
    and is accessible only within that function.
  • A global variable is declared before the main
    function.
  • Global variables are accessible by any function.

16
Data and Functions
  • The parentheses after a function's name can be
    used to pass data to a function and in some cases
    to return data from a function.
  • When a function is called, the data in the
    parentheses (called the argument) is passed into
    the receiving function.
  • There are two ways to pass data to functions
    passing by value and passing by reference.

17
Passing by Value
  • When you pass a variable to a function by value,
    a copy of the value in the variable is given to
    the function for it to use.
  • If the variable is changed within the function,
    the original copy of the variable in the calling
    function remains the same.

18
Passing by Reference
  • Functions that pass variables by reference will
    pass any changes you make to the variables back
    to the calling function.
  • To pass a variable by reference, simply precede
    the variable name with an ampersand () in the
    function definition.

19
Returning Values Using Return
  • Functions other than the main function can return
    their values to the calling function.
  • The value to be returned is specified using the
    return statement.

20
return example
  • This is the function that will return the value
  • double celsius_to_fahrenheit(double celsius)
  • double fahr // local variable
  • fahr celsius (9.0/5.0) 32.0
  • return(fahr)

21
return statement
  • include
  •  int main()
  • double fahrenheit
  • double celsius 22.5
  •  fahrenheit celsius_to_fahrenheit(celsius)
  •  
  • cout F\n"
  • return 0

22
Using Library Functions
  • C compilers include pre-written, ready-to-use
    functions to make programming easier.
  • The functions that come with your compiler are
    called library functions.
  • The prototypes for library functions are provided
    to your program using the include compiler
    directive.

23
Example of Library Function
  • A common library function is pow() which is used
    to raise a number to a power.
  • z pow(x, y)
  • // z equals x raised to
  • // the y power
  • In order to use the pow function, you must
    include the math.h header file using the compiler
    directive include .

24
Header Files
  • A header file is a text file that provides the
    prototypes of a group of library functions.
  • The linker uses the information in the header
    file to properly link your program with the
    function you want to use.

25
Popular Math Functions
  • abs Returns the absolute value of an integer
  • ceil Rounds up to a whole number
  • floor Rounds down to a whole number
  • Pow10 Calculates 10 to the power of x
  • sqrt Calculates the positive square root of x

26
Functions for Working with Characters
  • isupper Determines if a character is uppercase
  • islower Determines if a character is lowercase
  • isalpha Determines if a character is a letter
  • isdigit Determines if a character is a digit
  • toupper Converts a character to uppercase
  • tolower Converts a character to lowercase

27
Summary
  • Designing a program that consists of functions
    results in code that is better organized,
    reusable, and easier to debug.
  • The syntax of functions you create is very
    similar to that of the main function.
  • You must create a prototype for your functions to
    let the compiler know your function exists.
  • Prototypes are placed at the top of the program.

28
Summary
  • A local variable is created within a function and
    is accessible only from within that function.
  • A global variable is declared outside of all
    functions and is accessible from any function.
  • Getting data to and from functions is called
    passing data.
  • Data can be passed to functions by value or by
    reference.
  • When possible, you should pass by value.

29
Summary
  • Data passed by reference brings back changes made
    to it within a function.
  • A value can be passed to the calling function
    using return.
  • A void function does not return a value.
  • In a function prototype, you are only required to
    provide the data types of the parameters.

30
Summary
  • Library functions are functions that come with
    the compiler.
  • A header file provides the prototypes for library
    functions.
  • C includes common math functions and functions
    for working with characters.
Write a Comment
User Comments (0)
About PowerShow.com