Functions and Function Prototypes - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Functions and Function Prototypes

Description:

time and date manipulation functions iostream C standard input/output processing functions ... functions are grouped together and organized into libraries. ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 22
Provided by: Too94
Category:

less

Transcript and Presenter's Notes

Title: Functions and Function Prototypes


1
Functions and Function Prototypes
  • Lesson 11

2
Agenda
  • Able to use C built-in functions
  • The cmath library
  • The cstdlib library
  • The cstring library
  • Able to design and implement customised functions
  • Able to create function prototypes

3
Functions
  • Functions allow you to group commonly used code
    into a compact unit that can be used repeatedly.
  • You have already encountered one function,
    main(), which is a special function called at the
    beginning of the program after all static and
    global variables have been initialized.
  • Functions are the basics of modular programming
    and allow you to manage your program better.

4
Functions
  • Before creating the function, use sometime to
    think about the following issues
  • Name of the function
  • Description of what the function does
  • Description of parameter(s) to the function
  • Description of the return value of the function

5
C Standard Libraries
  • C comes with large number of built-in functions
    organised into sets of libraries.
  • You can use them without knowing how they are
    written as long as you understand what they do
    and how to communicate with them.
  • For example, there are useful mathematical
    functions in the math library. To use these
    functions, you have to
  • Include the header file ltcmathgt
  • double sqrt(double x)
  • Example
  • cout ltlt sqrt(900.0)
  • sqrt (square root) function The preceding
    statement would print 30
  • All functions in math library return a double

5
6
C Standard Libraries
6
7
Using C Standard Library
  • Related C standard functions are grouped
    together and organized into libraries.
  • To use them, you have to include appropriate
    header files.
  • How do you know what functions are available,
    what they do, how you can use them?
  • References http//www.cplusplus.com/

7
8
Using C Standard Library
  • Many functions do come from (old) C libraries. In
    fact, most of functions are from old C libraries.
  • On the references, you may see include ltmath.hgt.
  • This is C style. Although its still supported,
    you should follow the new C standard way
    specifying the standard library header files,
    which is include ltcmathgt.
  • All C headers are suffixed with c in C.
    ltstdlib.hgt becomes ltcstdlibgt, ltctype.hgt becomes
    ltcctypegt, and so on.

8
9
Using C Standard Library
  • Browse on any references of the standard library
    and make yourself familiar with kinds of
    functions available.
  • As users of the functions, you do not have to
    know how they are internally written.
  • You need to know
  • what name is the function called,
  • what a function does (description),
  • what parameters it requires, and
  • what it returns.
  • This is called Information Hiding or Abstraction.

9
10
Functions
  • Declaring a Function
  • ReturnType FunctionName(param1,param2,,paramN)
  • //Do Something
  • return ReturnTypeVal//Return will exit the
    function
  • Calling a Function
  • value FunctionName(param1,param2,,paramN)
  • Note The argument and the parameter do not need
    to have the same name!

11
Function Creation Example 1
  • In our program, we want to calculate the cube of
    a number often (nnn).
  • To prevent typing mistake, you want to create a
    function that will calculate the cube for you.

12
Function Creation Example 1
  • Thinking Process
  • Name cubeOf
  • Description The function will take an input and
    return the power 3 of that number.
  • Parameters integer num
  • Returns a large number (Cube returns huge
    number!)

13
Function Creation Example 1
  • Writing the Function
  • long cubeOf(int num)
  • return (num num num)

14
Function Creation Example 1
  • Using cubeOf function
  • include ltiostreamgt
  • using namespace std
  • long cubeOf(int no)
  • return (nonono)
  • int main()
  • long output cubeOf(3) // Using cubeOf
  • cout ltlt output
  • return 0

15
Function Creation Example 2
  • In our program, we have to print the following
    menu many times
  • 1 - Buy Item
  • 2 - Sell Item
  • 3 - View Item
  • 4 Exit Item

16
Function Creation Example 2
  • Thinking Process
  • Name printMenu
  • Description Print the menu
  • Parameters none
  • Returns none

17
Function Creation Example 2
  • Writing the Function
  • void printMenu()
  • cout ltlt "1 Buy Item" ltlt endl
  • cout ltlt "2 Sell Item" ltlt endl
  • cout ltlt "3 View Item" ltlt endl
  • cout ltlt "4 Exit Item" ltlt endl
  • // Notice no return!

18
Function Creation Example 2
  • Using the Function
  • include ltiostreamgt
  • using namespace std
  • void printMenu()
  • cout ltlt "1 Buy Item" ltlt endl
  • cout ltlt "2 Sell Item" ltlt endl
  • cout ltlt "3 View Item" ltlt endl
  • cout ltlt "4 Exit Item" ltlt endl
  • int main()
  • printMenu()
  • return 0

19
Function Prototypes
  • In order to use your function, tell C/C the
    function exists before using it.
  • In our previous example notice the function comes
    before where you call it
  • include ltiostreamgt
  • using namespace std
  • long cubeOf(int num)
  • return (num num num)
  • int main()
  • long output cubeOf(3)
  • cout ltlt output
  • return 0

20
Function Prototypes
  • However it would not be working in the following
    case, when the function is declared in the order
    after you use the function
  • include ltiostreamgt
  • using namespace std
  • int main(void)
  • long output cubeOf(3) // ERROR!
  • cout ltlt output
  • return 0
  • long cubeOf(int num)
  • return (num num num)

21
Function Prototypes
  • To solve that, you can create function prototypes
    to tell your C/C program that a function exists
    and is declared later in your program
  • include ltiostreamgt
  • using namespace std
  • long cubeOf(int no) // Function Prototype
  • int main(void)
  • long output cubeOf(3) // Now Working
  • stdcout ltlt output
  • return 0
  • long cubeOf(int no)
  • return (nonono)
Write a Comment
User Comments (0)
About PowerShow.com