Functions - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Functions

Description:

These functions are called as user- defined functions or library functions ... Get this running, then expand one of second level function, get this running... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 17
Provided by: amandaso
Category:

less

Transcript and Presenter's Notes

Title: Functions


1
Functions
2
What is a function?
F
c
Function
t
o
n
u
n
i
Input(s)
Output
3
Purpose of function
  • Make the programmer's life easier
  • Functions create new conceptual units
    programmer thinks the problem in terms of
    functions
  • Write code once, and call it from many places
    - this insures consistency, and reduces the cost
    of writing and debugging.
  • Allows code to be parameterized by passing
    values to it
  • Functions can be collected in libraries and
    reused in other programs

4
Built-in Functions
  • Some of the common functions are programmed
    and stored in C library
  • These functions can be called in any program
  • These functions are called as built-in
    functions or library functions
  • E.g. 1 sin(x) - returns sine of x, x in
    radians
  • E.g. 2 clrscr() clears the screen
  • Built-in mathematical functions

5
User-defined functions
  • C allows programmers to define their own
    functions relevant to their program
  • These functions are called as user- defined
    functions or library functions
  • To use a user-defined we must do the following
  • Provide a function prototype
  • Provide a function definition
  • Call the function
  • Example

6
Prototypes
  • Every function (except main) should be
    declared near the beginning of a program
  • Contains return type (or void), name, and
    parameter names (optional) and types and end
    with a semicolon ()
  • Tells C compiler in advance about some
    characteristics of a function
  • return type
  • number and the types of parameters

7
Prototypes ensure
  • The program correctly handles the function
    return value
  • Checks that the program uses the correct
    number of parameters
  • Checks that the program uses the correct type
    of parameters
  • If not, it converts the parameters to the
    correct type, if possible

8
Function definition
  • Comprises of two parts
  • function heading served as interface with
    the rest of the program
  • function body consists of instructions to be
    executed inside the function
  • General form
  • return_type functionName( parameter_list )
  • local_variable_declarations function-impleme
    ntation
  • return value //value is of type function_type

9
Function definition (Cont.)
  • If the function returns a value then the type
    of that value must be specified in return_type
  • This could be
  • integer/floating point number
  • structure
  • pointer
  • object (discuss later)
  • void function function that does not return
    a value

10
Function definition (Cont.)
  • Parameter list specifies the types and
    number of formal parameters passed to the
    function
  • Local Variables - All variables that are
    declared in a function are local to the function
  • They can not be referenced from outside the
    function (local scope).
  • Created when the function is called and are
    destroyed when the function terminates
  • Parameters are like local variables (local
    scope, function lifetime)

11
Function definition (Cont.)
  • Function_implementation - consists of C
    executable statements that implement the effect
    of the function
  • Return statement - stops execution and returns
    to the calling function
  • Value of the return statement should match
    with the function_type.
  • Optional for void functions

12
Calling the function
  • Invoke simply by
  • using its name including the parentheses
  • within parentheses parameters to be passed (if
    any) separated by comas (,)
  • ends with semicolon ()
  • E.g.
  • printLine () //invokes a function without
    passing arguments
  • printLineOfCharacters (, 25) //invokes a
    void function with parameters
  • dist distance(x1, y1) //invokes a function
    with a return value

13
Naming
  • Name should be clear to the reader
  • Choose a name that has a clear meaning
  • E.g. "f" is not a good name
  • Void functions are often more readable if a
    verb name is used that describes what it does
  • E.g. printAverage
  • Boolean functions should usually start with
    "is" or another word that suggests a yes/no
    answer
  • E.g. isOdd

14
Naming (Cont.)
  • Name should be clear to the reader
  • Start the name with lowercase and capitalize
    the first letter of each additional word in the
    name
  • E.g. printLineOfCharacters
  • Value-returning functions are often named
    after the value that they return
  • E.g. square

15
Top-down programming
  • Write a short main function that calls
    additional functions to accomplish its task
  • These functions too kept short, and if
    necessary it can call more functions
  • Start with the main function, and write only
    functions which display only a small message
  • Get this running, then expand one of second
    level function, get this running

16
One page
  • Good idea to keep functions shorter than one
    page or one screen
  • then it is easy understand
  • If one page is not enough space, then write
    additional functions to help perform the task
    (top-down programming).
Write a Comment
User Comments (0)
About PowerShow.com