Functions - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Functions

Description:

main() can then call other functions such as printf() functions take ... r for carriage return b for backspace for backslash ' for quotes f for form feed ... – PowerPoint PPT presentation

Number of Views:10
Avg rating:3.0/5.0
Slides: 10
Provided by: infcU
Category:

less

Transcript and Presenter's Notes

Title: Functions


1
Functions
  • ONLY functions in C
  • C programs are a collection of functions. All C
    programs have at least one function - main()
  • main() can then call other functions such as
    printf()
  • functions take arguments
  • e.g... printf(hello) - hello is the argument
  • You can write your own functions with/without
    arguments
  • A function without return value is called a
    procedure in Pascal, or a subroutine in Fortran

2
Reserved Words
3
Variables and Data types
  • Local variables
  • those for use within a function only
  • declared at the beginning of a function
  • Global variables
  • those for use in all functions
  • declared in main block (outside the other
    function bodies) and available for all functions
  • 4 base data types
  • char
  • int (can be short, long, signed, unsigned)
  • float
  • double (is long float can be long double)

4
Input/Output
  • input - scanf()
  • output - printf()
  • Need for format specifiers
  • indicate the data type
  • String - s
  • character c
  • int d (ld for long int), (or i, li)
  • float f (lf for double), (or exponential form
    e, E(2.3e02, 2.3E02), general form g,
    G(automatically select f or E)
  • e.g.. printf(Student grade is f,sgrade)
  • where sgrade is of type float
  • e.g.. scanf(d,noofstudents)
  • where noofstudents is of type int
  • where means address of

5
More about printf()
  • takes a string as an argument
  • special notation for hard to get characters
  • \n for new line
  • \t for tab
  • \r for carriage return
  • \b for backspace
  • \\ for backslash
  • \ for quotes
  • \f for form feed
  • integer printing -145
  • I -gt-145 6d -gt bb-145 -6d -gt-145bb
  • float printing 157.8926
  • f -gt157.892600 6.2 -gt157.89e -gt1.57826e02
    .1E-gt1.5E02

6
More about scanf()
  • Complements printf()
  • Same format specifiers
  • Need to use the address of operator,
  • Without the address of operator you may crash
    the system
  • scanf(d,noofstudents)
  • scanf(d, year)
  • printf(Enter the distance and velocity\n)
  • scanf(lf lf, distance, velocity)
  • Enter the distance and velocity
  • 10 15.5

7
Operators
  • Assignment x y z
  • Relational lt, lt, gt, gt, , !, e.g. if (x y)
  • Arithmetic - ,-,/,,
  • Types must be correct for expressions
  • Expressions can be mixed in some cases
  • e.g.. int and float
  • Generally, the expression if mixed is converted
    to the type of highest accuracy
  • You can enforce the type with the cast operator
  • e.g. Celsius (int) (Fahrht - 32) 5 / 9)
  • This forces the answer to be an integer
  • Logical !, ,

8
Expressions
  • More or less as in Pascal except
  • Use instead of for assignment
  • The is part of the statement
  • Increment operator
  • j i 1 can be written j i
  • j takes the value of i then add 1
  • OR j i
  • j takes the value of i incremented by 1
  • Decrement operator
  • j i - 1 can be written j i--
  • OR j --i

9
Expressions
  • and -- can be used in expressions
  • If used in a prefix position, the identifier is
    modified, and then the new value is used in
    evaluating the rest of the expression
  • if used in a postfix position, the old value is
    used to evaluate the rest of the expression, and
    then the identifier is modified
  • Example
  • Let x2, y4, before each statement executed.
    Find the results!
  • zxy x y z
  • zxy x y z
  • xy x y
  • yx x y
Write a Comment
User Comments (0)
About PowerShow.com