m04_un08_p01

Upload: zie-bakar

Post on 03-Jun-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 M04_UN08_P01

    1/22

    Unit 4.8 - Function

    Presentation1

    Programming Concepts and Constructs

    (C Language)

  • 8/12/2019 M04_UN08_P01

    2/22

    Revision

    1. Two-dimensional arrays are initialised

    __________________.

    a. Row-wise.b. Column-wise.

    2. In x[i][j], i refers to ______________ and jrefers to __________________.

  • 8/12/2019 M04_UN08_P01

    3/22

    Objectives

    At the end of this presentation, you will be able to:

    List the advantages of functions

    List the function components

    List the types of functions

    Accept data from the user and display data on

    the screen using standard input/output functions

  • 8/12/2019 M04_UN08_P01

    4/22

    Functions

    Is a block of statements that performs a

    specific task.

  • 8/12/2019 M04_UN08_P01

    5/22

    Advantages of Using Function

    Reusability

    Function Portability

    Modularity

    Easy to Debug

    Easy to Modify and Extend

  • 8/12/2019 M04_UN08_P01

    6/22

    Function Components

    Function Header - Specifies the function

    name along with parameters.

    Function Body- Contains the actual code ofthe function.

    Return Statement- Returns a value to the

    main program.

  • 8/12/2019 M04_UN08_P01

    7/22

    Types of Function

    The two types of functions in C language are:

    Built-in functions

    User-defined functions

  • 8/12/2019 M04_UN08_P01

    8/22

    Built-in Functions

    Are pre-defined functions that are part of theC language.

    Built-in functions are also called as libraryfunctions.

    Example

    printf()

    scanf()

  • 8/12/2019 M04_UN08_P01

    9/22

    Built-in Functions

    The code for these functions are already

    defined by the C language.

    To use a particular function, you must includethe respective header file.

  • 8/12/2019 M04_UN08_P01

    10/22

    User-Defined Functions

    Are the functions written by the users for

    performing a specific task.

    Example

    main()

  • 8/12/2019 M04_UN08_P01

    11/22

    Header File - stdio.h

    This is the standard input-output header file.

    The functions in this header file are used for

    accepting the input data from the user anddisplaying the processed data on the screen.

  • 8/12/2019 M04_UN08_P01

    12/22

    Functions in stdio.h

    Function : getchar()

    Description : Accepts a single character from

    the user.

    Syntax : getchar();

  • 8/12/2019 M04_UN08_P01

    13/22

    Functions in stdio.h

    Function : putchar()

    Description : Displays a single character on

    the screen.

    Syntax : putchar();

  • 8/12/2019 M04_UN08_P01

    14/22

    Hands-On!

    ProgramD8_1.Cwill accept and display acharacter on the screen.

  • 8/12/2019 M04_UN08_P01

    15/22

  • 8/12/2019 M04_UN08_P01

    16/22

    Functions in stdio.h

    Function :puts()

    Description :Displays the string on the

    screen.

    Syntax : puts();

  • 8/12/2019 M04_UN08_P01

    17/22

  • 8/12/2019 M04_UN08_P01

    18/22

    Summary

    In this presentation, you learnt the following:

    A function is a block of statements that performs

    a specific task.

    Function components:

    Function Header Function Body

    Return Statement

  • 8/12/2019 M04_UN08_P01

    19/22

    Summary

    Types of functions available in C are: Built-in functions

    User-defined functions

    Built-in functions are pre-defined functionsthat are part of the C language.

    Standardinput-output functions can be usedto accept input from the user and display thesame.

  • 8/12/2019 M04_UN08_P01

    20/22

    Assignment

    1. What are the two types of functionsavailable in C language?

    2. Which of the following functions is moreappropriate for reading multi-word strings?

    a) getchar()

    b) putchar()

    c) gets()

    d) puts()

  • 8/12/2019 M04_UN08_P01

    21/22

    Lab Exercise

    1. Write a program to accept characters from

    the user. The program must accept the

    characters till the user presses the Enterkey.

  • 8/12/2019 M04_UN08_P01

    22/22

    Lab Exercise

    2. Write a program to count the number of

    blank spaces in the input string.