CS 101 - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

CS 101

Description:

In math, we write a function as f(x) = x2. ... to write x inside the function declaration. It is not wrong to do so, but we may write double x or double y, ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 8
Provided by: unkn546
Category:
Tags:

less

Transcript and Presenter's Notes

Title: CS 101


1
CS 101
  • Lecture 5

2
Functions
  • In math, we write a function as f(x) x2 . If
    we want to write this same function in C, we
    use the following syntax
  • double f(double x)return xxThe first
    double says that f returns a double, which means
    that when the call the function, i.e., when the
    expression f(a) occurs, e.g., on the right side
    of an assignment statementdouble zf(a) where a
    is a double variable. We can also call f by
    writing coutnest calls of f as double zf(f(a)) The return
    value of f is the double value that f(a)
    represents. We cannot write f(a) 3, any more
    than we could write xy 3 The second double,
    in double x, is a declaration of the variable x.
    x is called a local variable of f, and also
    occurs inside the body of f which is return
    xx. If a is a double variable which is
    declared in main, when we call f by writing
    coutof f and the value of a is copied to the local
    variable x, then x is squared in xx, and finally
    f(a) holds a copy of xx. The variable x ceases
    to exist when control returns from f to main
    again.F is said to have one argument.Let us look
    at a program which calls f.

3
Using a function in a program
  • includedouble f(double)//functio
    n declaration or prototypemain()double
    a3.4cout11.56 to screendouble f(double x)return
    xx//function definitionNotice that it is not
    necessary to write x inside the function
    declaration. It is not wrong to do so, but we may
    write double x or double y, it makes no
    difference. We have made a point of
    distinguishing a from x, since a is called an
    actual parameter and x is called a formal or
    local parameter. A function must be declared
    before it is called. Here are some other ways
    that f can be declared defined and called.

4
Alternate possibilities for declaring, defining
and calling f.
  • includedouble f(double
    x)return xx/counts as declaration and
    definition/main()Double a3.4coutlAlsoincludemain()double
    a3.4double f(double)//declaration of f must
    come before f(a)coutf(double x)return xx//definition of ff
    cannot be defined inside main or any other
    function, but it can be declared.

5
Functions with no arguments
  • The main difference between math functions and
    C functions is that functions in C can DO
    things like printing out to the screen or to a
    file and math functions cant. Here is an example
    of a function f with no arguments and no
    return.void f()coutarguments and no return.\nYou may have
    noticed that main has no arguments either, since
    we write main(). The return type of main is int,
    though we dont have to write that for the g
    compiler. For other compilers, such as Borland or
    Visual C, we have to write int main(). One of
    the most common errors is calling f as f rather
    than the correct way which is f() Notice that we
    cant write coutanything, nor can we write int zf()

6
Another program with function calls
  • includeint sum(int x,int
    y)return xyint prod(int x,int y)return
    xymain()coutintegers.\nint a,bcinabcoutof product is

7
Quiz 4 and solution
  • Write a program which will use a function
    named sqs which will take three int arguments,
    return an int which is the sum of the squares of
    its three arguments. The values of the int
    variables which are to be passed to sqs are to be
    entered from the keyboard. Make sure you define
    sqs appropriately. includeint
    sqs(int x, int y, int z)return
    xxyyzzmain()coutintegers.\nint a,b,ccinabccoutsum of the squares of the integers you just
    entered is main to use sqs is to print out its return value
    to the screen.
Write a Comment
User Comments (0)
About PowerShow.com