CSCI101 An Introduction to Programming using C - PowerPoint PPT Presentation

1 / 58
About This Presentation
Title:

CSCI101 An Introduction to Programming using C

Description:

also known as information hiding. also known as procedural abstraction ... If you have two or more function definitions for the same function name, that is ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 59
Provided by: AnneD71
Category:

less

Transcript and Presenter's Notes

Title: CSCI101 An Introduction to Programming using C


1
CSCI101 An Introduction to Programming using
C
2
Functions 1
3
Procedural Abstraction and Functions that Return
a Value
4
Contents of Functions 1
  • 0 Introduction to Functions
  • 1 Top-Down Design
  • 2 Predefined Functions
  • 3 Programmer-defined Functions
  • 4 Procedural Abstraction
  • 5 Local Variables
  • 6 Overloading Function Names

5
Introduction to Functions
  • Programs are normally made up of subparts.

6
Introduction to Functions
  • There may be a subpart to handle data input,
  • another subpart to handle the processing of
    the data
  • and another subpart to handle data output. . .

7
Introduction to Functions
  • These subparts or subprograms are given a name
    and are considered as a separate entity. They are
    known as functions.
  • Functions are executed simply by calling their
    name.

8
Introduction to Functions
  • Functions can receive data to be processed
    within the function, and . . .

9
Introduction to Functions
  • . . . functions can return data to the part of
    the program that called the function.

10
Introductory Example ProgramsRunning under
Dev-C
  • funcs00.cpp - example program with no functions
  • funcs01.cpp - using a function
  • funcs02.cpp - passing an int to a function
  • funcs03.cpp - passing an int and a double
  • funcs04.cpp - returning a value back from a
    function
  • funcs05.cpp - using the predefined function
    sqrt()
  • funcs06.cpp - using a global constant

11
Introductory Example ProgramsRunning under
Borland version 5.02
  • funcs00_Borland.cpp - example program with no
    functions
  • funcs01_Borland.cpp - using a function
  • funcs02_Borland.cpp - passing an int to a
    function
  • funcs03_Borland.cpp - passing an int and a
    double
  • funcs04_Borland.cpp - returning a value back
    from a function
  • funcs05_Borland.cpp - using the predefined
    function sqrt()
  • funcs06_Borland.cpp - using a global constant

12
A program with no functions
  • http//www.annedawson.com/funcs00.cpp

13
A program which uses a function
http//www.annedawson.com/funcs01.cpp
14
Passing an int to a function
http//www.annedawson.com/funcs02.cpp
15
Passing an int and a double to a function
http//www.annedawson.com/funcs03.cpp
16
Returning a value back from a function
  • http//www.annedawson.com/funcs04.cpp

17
Using the predefined function sqrt()
  • http//www.annedawson.com/funcs05.cpp

18
Using a global constant
  • http//www.annedawson.com/funcs06.cpp

19
Contents of Functions 1
  • 0 Introduction to Functions
  • 1 Top-Down Design
  • 2 Predefined Functions
  • 3 Programmer-defined Functions
  • 4 Procedural Abstraction
  • 5 Local Variables
  • 6 Overloading Function Names

20
Top-Down Design
  • Top-down design is breaking down a problem
    into smaller and smaller problems, until the
    smallest problem can be implemented in a function
    in C which fits onto a single screen of the
    code editor...

21
Top-Down Design
  • Program design known as structured program
    design or top-down program design requires that
    programming problems are broken down into small
    problems which are executed one at a time. . .

22
Top-Down Design
  • The goal of top-down design is to break a
    problem into individual tasks, or modules, that
    can easily be transcribed into pseudocode,
    flowcharts or a program.

23
Top-Down Design
24
(No Transcript)
25
Top-Down Design
  • Modules continue to be broken down into
    further modules, until at the lowest level, the
    modules are small enough to be coded into a
    single function which performs a single task.

26
Structured Programming
  • A structured program is one which is based on
    a modular design and uses only the three types of
    control structures sequences, decisions and
    loops

27
Advantages of Structured Programs
  • Easy to write
  • Easy to debug
  • Easy to understand
  • Easy to change

28
Contents of Functions 1
  • 0 Introduction to Functions
  • 1 Top-Down Design
  • 2 Predefined Functions
  • 3 Programmer-defined Functions
  • 4 Procedural Abstraction
  • 5 Local Variables
  • 6 Overloading Function Names

29
Predefined Functions
  • We have already seen the use of the predefined
    function sqrt()in program

http//www.annedawson.com/funcs05.cpp
30
Predefined Functions
  • Predefined functions are provided for you by
    the C language.

31
Predefined Functions
  • The definitions of predefined functions (the
    predefined function code - the C statements
    which make up the functions) are contained in a
    library header file.

32
Predefined Functions
  • The header file must be included in your
    program code with a include statement

33
sqrt() - square root function
  • Receives a double argument
  • Returns a double value
  • Example sqrt(4.0)
  • Value 2.0
  • Library ltmath.hgt or ltcmathgt
  • Example program Display 4.1 page 183

34
Contents of Functions 1
  • 0 Introduction to Functions
  • 1 Top-Down Design
  • 2 Predefined Functions
  • 3 Programmer-defined Functions
  • 4 Procedural Abstraction
  • 5 Local Variables
  • 6 Overloading Function Names

35
Programs which use Programmer-defined functions
  • funcs00.cpp - example program with no functions
  • funcs01.cpp - using a function
  • funcs02.cpp - passing an int to a function
  • funcs03.cpp - passing an int and a double
  • funcs04.cpp - returning a value back from a
    function
  • funcs05.cpp - using the predefined function
    sqrt()
  • funcs06.cpp - using a global constant

36
Contents of Functions 1
  • 0 Introduction to Functions
  • 1 Top-Down Design
  • 2 Predefined Functions
  • 3 Programmer-defined Functions
  • 4 Procedural Abstraction
  • 5 Local Variables
  • 6 Overloading Function Names

37
Procedural Abstraction
  • treating functions as black boxes
  • also known as information hiding
  • also known as procedural abstraction
  • it means that the function prototype and its
    comments should be all a programmer needs to know
    in order to use the function
  • All variables used by the function should be
    declared inside the function

38
Implementation of Procedural Abstraction
  • You can save your function definitions in a
    separate file, and compile the file separately,
    before linking it to the other files to generate
    the executable file.

39
Implementation of Procedural Abstraction
  • The file that contains the main function of the
    program also contains just the functions
    prototypes.

40
Implementation of Procedural Abstraction
  • This way, you can hide the details of the
    functions, maintaining procedural abstraction.

41
Contents of Functions 1
  • 0 Introduction to Functions
  • 1 Top-Down Design
  • 2 Predefined Functions
  • 3 Programmer-defined Functions
  • 4 Procedural Abstraction
  • 5 Local Variables
  • 6 Overloading Function Names

42
Local Variables
  • Variables that are declared inside a function
    definition are known as local variables.
  • A variable of the same name in the main
    function is a different variable!

43
Local Variables
  • A functions call-by-value formal parameters
    are variables which are local to the function
    definition. . .
  • in other words, they are local variables

44
Scope
  • The scope of a variable is that part of the
    program where it can be used.

45
Local and Global Scope
http//www.annedawson.com/funcs07.cpp
46
Local Scope
  • http//www.annedawson.com/local.cpp

47
Global Scope
  • http//www.annedawson.com/global.cpp

48
Global constants
  • const double PI 3.142
  • int main ()

Good!
49
Global Constant
  • http//www.annedawson.com/constant.cpp

50
Global variables
  • // DO NOT DO THIS
  • int number 10
  • int main ()

Bad!!!!!
51
Contents of Functions 1
  • 0 Introduction to Functions
  • 1 Top-Down Design
  • 2 Predefined Functions
  • 3 Programmer-defined Functions
  • 4 Procedural Abstraction
  • 5 Local Variables
  • 6 Overloading Function Names

52
Overloading Function Names
  • If you have two or more function definitions
    for the same function name, that is called
    overloading.

53
Overloading Function Names
  • When you overload a function name, the
    function definitions must have different numbers
    of formal parameters, or some formal parameters
    of different data types.

54
Calling an overloaded function
  • When there is a function call, the compiler
    uses the function definition whose number of
    formal parameters and types of formal parameters
    match the arguments in the function call.

55
Overloaded function program
  • Display 4.16 on page 230

56
Polymorphism
  • Polymorphism means many forms and is an
    important term in C. Function overloading is
    our first example of polymorphism.

57
End of CPP_Functions1.ppt
58
  • Last updated Friday 24th March 2006, 1347 PT,
    AHD
Write a Comment
User Comments (0)
About PowerShow.com