Lab Session-VI CSIT-120 Spring 2001 - PowerPoint PPT Presentation

About This Presentation
Title:

Lab Session-VI CSIT-120 Spring 2001

Description:

Lab session-VI deals with functions ... A semicolon is a must to terminate any C statement (except loop starters) 22. The Events ... – PowerPoint PPT presentation

Number of Views:10
Avg rating:3.0/5.0
Slides: 26
Provided by: sunyfr
Category:
Tags: csit | lab | session | spring | starters

less

Transcript and Presenter's Notes

Title: Lab Session-VI CSIT-120 Spring 2001


1
Lab Session-VI CSIT-120 Spring 2001
  • Let us look at C syntax rules in brief
  • Exercise VI-A (Demo Required)
  • Lab Assignment4 Due May 1st, 2001
  • (No Lab on April 24th, .To Earthquake Zone)
  • Lab session-VI deals with functions
  • We will learn how to write independent functions
    that can be called from main()
  • Students are given examples of functions

2
C Syntax Rules in Brief
  • include ltiostream.hgt
  • ..other include files go here
  • void main(void)
  • Declarations of variables and constants
  • Assignment and control statements

3
C Syntax Rules in Brief
  • Make it a habit to put declarations in the
    beginning. For example,
  • int employeeID
  • const int current-year2000
  • float deduction-amount
  • Once a data item has been declared, do not
    mention its data type in assignment statement
  • int employeeID23 is WRONG
  • employeeID23 is RIGHT

4
C Syntax Rules in Brief
  • If you write a control statement, its conditions
    must be in parenthesis with no semicolon after
    the parenthesis
  • For example, displaying employeeID if employeeID
    is 24
  • if (employeeID 24)
  • coutltltID is ltltemployeeIDltltendl
  • Please note double-equal in comparing, no
    semicolon after comparison statement and joining
    several displayable messages with ltlt

5
C Syntax Rules in Brief
  • Different comparison statements
  • if (employeeID ! 24) coutltltendl
  • (if employeeID is not equal to 24, do a blank
    cout)
  • if (employeeID gt 24)
  • if (employeeID lt24)
  • if (employeeID gt 24)
  • if (employeeID lt 24)
  • We could also use an else clause to capture the
    false results of comparison

6
C Syntax Rules in Brief
  • For example, if employeeID is 24, display ID is
    24 else do a blank line display
  • if (employeeID 24)
  • coutltltID is ltltemployeeIDltltendl
  • else coutltltendl
  • Use braces to show blocks of code even if only a
    single statement is there in if part or else part
    (for clarity purposes)

7
C Syntax Rules in Brief
  • In assigning values, there can be only one
    destination shown on left of the single equal
    sign
  • deductions pay- (paytax-percentage)
  • Please note the use of parenthesis to emphasize
    and make the expression very clear

8
Lab Exercise VI-A
  • Develop a program that takes a number from the
    user and then enters a loop to keep displaying
    the result of successively multiplying the number
    by 2. The loop is terminated when the number
    exceeds half of INT_MAX (Do not forget to include
    the header file limits.h)
  • Example 2,4,8,16,32,64,..

9
Lab Assignment4 Due 5/1
  • A classroom has the maximum capacity of 37
    people, write a program that accepts the total
    number of students wishing to register for a
    course. This program checks for any violations of
    fire safety code. It asks the user to input the
    total number of students. If this number is more
    than the maximum allowed, it prints a warning and
    shows how many people will not get the course
    otherwise it shows the additional number of
    people who can be admitted to the course. Bonus 2
    marks for making it a loop and asking again and
    again until the number of students is exactly
    equal to the capacity

10
Lab VI Continued
  • We look at some C code segments
  • Introducing functions
  • We solve one programming problem

11
Basic Concepts
  • What will be the output? Dont run this code,
    just predict
  • void main()
  • int num1,num2
  • num112345 num26789
  • coutltltnum1ltltnum2ltltendl

12
Basic Concepts
  • What is wrong with this code segment?
  • void main()
  • int num1, myloop
  • while (mylooplt24)
  • coutltltOnce in myloop, show no mercyltltendl

13
Basic Concepts
  • What is wrong with this code segment?
  • Void main()
  • int mybirthdate
  • mybirthdate 2
  • while (mybirthdatelt24)
  • coutltltEnter my birth date (date only)ltltendl
  • cingtgt mybirthdate
  • coutltltyou got it\n

14
Basic Concepts
  • What is the error in this code segment?
  • int cans
  • for(int cans1 canslt100 cans)
  • coutltltEach one has a refund of 5 cents\n
  • coutltltcount until 100 cans are done
  • cans--

15
Basic Concepts
  • Fix this code segment
  • int k
  • coutltltNumber please cingtgtk
  • if (k5) coutltltYou typed 5
  • else coutltltNothing else please

16
Functions
  • Functions are subroutines, procedures,
    modules or program units.
  • Functions perform a given task, usually one
    function is dedicated to one task
  • One advantage of using functions is to avoid
    repeated coding for the same job

17
Functions Reduce Code Size
  • For example, consider a program that prints text
    in a box, something like below

  • The Journey Through Central Spine

  • We can achieve this printout with a program
    containing 9 cout statements OR we can develop a
    function to print aesterik lines

18
Program with 9 cout Lines
  • coutltltltlt
    endl
  • coutltltltlt
    endl
  • coutltltltlt
    endl
  • coutltlt
    ltltendl
  • coutltlt The Journey Through Central Spine
    ltltendl
  • coutltlt
    ltltendl
  • coutltltltlt
    endl
  • coutltltltlt
    endl
  • coutltltltlt
    endl.

19
Same Program with a Function
  • Print_three_star_lines()
  • coutltlt
    ltltendl
  • coutltlt The Journey Through Central Spine
    ltltendl
  • coutltlt
    ltltendl
  • Print_three_star_lines()
  • Now the program is more compact and readable.
    Actual function definition will be developed
    after the main() functions code
  • DEMONSTRATION OF PROGRAM

20
Function Specific Actions
  • If you plan to work with functions, make sure
    that you name your functions according to C
    naming conventions
  • (letters, underscores, digits, no digits in the
    beginning)
  • As demonstrated, function data type is specified
    before its name. If the function does not return
    anything, void is used
  • Also note the DECLARATION before the actual
    function code, ending with a

21
Function Call
  • Notice the function name is simply repeated when
    it is called from the main routine
  • print_three_star_lines()
  • The empty parenthesis indicate the fact that no
    data is passed to this function. A semicolon is a
    must to terminate any C statement (except loop
    starters)

22
The Events
  • When a function is called from the main routine,
    the following events take place
  • Control (of the running program) is transferred
    to the function as well as any data that may be
    needed
  • Function performs its task and at the end,
    transfers the control back to the calling routine

23
Why does the Function name appear so many times?
  • Count the number of times the function name is
    repeated in our program
  • The name appears each time for a different
    purpose
  • Firstly we have to declare the function. Function
    declaration is called its prototype
  • void print_three_star_lines()

24
Why does the Function name appear so many times?
  • Next, we have to define the function,i.e. its
    actual source code
  • void print_three_star_lines() .
  • Finally, we have to make the function call
  • print_three_star_lines()
  • What do you know about main function when it
    reads void main().?

25
Lab Programming Exercise
  • Write a program that accepts a digit entered by a
    user and then displays it in words.
  • For example, user enters 8, program displays,
    You Entered Eight
  • User enters 0, program displays You Entered
    Zero
  • and so on..
Write a Comment
User Comments (0)
About PowerShow.com