esc101-lec13

Upload: mukesh-kumar-dewra

Post on 05-Apr-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 esc101-lec13

    1/6

    1

    ESc101: Fundamentals of Com utin

    2011-12-Monsoon Semester

    Lecture #13, August 25, 2011

    Please switch off your mobile phones.

    Announcements

    Monday lab scheduled on 22nd August will instead be held on

    Saturday, 27th August from 2:00 PM to 5:00 PM.

    Wednesday lab scheduled on 31st August will instead be held

    on Saturday, 3rd September from 2:00 PM to 5:00 PM.

    Lec-13 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    1

    Quiz on lab days from 5th to 9th September in Lab at 2:00 PM.

  • 7/31/2019 esc101-lec13

    2/6

    2

    Recap

    Arrays

    Two dimensional arrays

    Lec-13 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    2

    Functions

    ,

    well-defined task.

    A function has some inputs, better known as arguments and

    an output, better known as result or return value A program may consist of any number of functions.

    Any function can call (or use) any other function.

    We have been writing only one function so far (called, main).

    We have used functions, such as, printf, scanf, sqrt, etc.

    Lec-13 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    3

  • 7/31/2019 esc101-lec13

    3/6

    3

    Why Use Functions

    .

    It is best to put such code in one place and re-use (call) it

    as many times from as many places as is needed.

    Will ensure that if there is an error or a change is needed in

    the code, then that change needs to be done only at one place.

    Hence, improves reliability of the program.

    Modular code

    Easy to understand

    It is easy to understand 5 programs of 20 lines each rather

    than one program of 100 lines.

    Lec-13 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    4

    Function - Syntax

    Function-name (type1 arg1, type2 arg2, , typen argn)

    Function-name is the name of the function.

    This name is needed to use or call this piece of program.

    We have to follow the same naming convention as for any variable.

    Function can have any number of arguments

    Each argument is a variable (memory area) whose value is set

    during the function call.

    , , , .

    Function carries out some activity and has a result, which is

    returned to the caller of this function.

    The type of this result has to be specified.

    Lec-13 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    5

  • 7/31/2019 esc101-lec13

    4/6

    4

    Function (contd.)

    Functions are called from other functions as:

    var a e = unc on_name argumen s

    Example:

    int sum (int a, int b) // Function definition

    {

    return (a + b); // Return the result

    }

    z = sum (x, y); // Calling the function

    Lec-13 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    6

    Functions (contd.)

    .

    In such cases, the return type can be void

    Any type of argument can be used

    Even arrays can be arguments

    Function body may contain any valid C code

    Lec-13 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    7

  • 7/31/2019 esc101-lec13

    5/6

    5

    Function to add two integers: Complete Program

    #include // Include standard I/O functions

    int main () // main function

    {

    n x, y, z;

    scanf(%d, &x); // Read an integer and store it in x

    scanf(%d, &y); // Read an integer and store it in y

    z = sum (x, y); // Call sum function with parameters x, y

    printf(%d\n, z); // Print z, which stores the return value of sum

    }

    int sum (int a, int b) // Function declaration two integer arguments

    eturn value s also an ntegerint c; // Need some memory area in this function

    c = a + b; // Compute the sum

    return c; // Return the sum

    }

    Lec-13 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    8

    Parameters and Arguments

    Function is called with parameters.

    .

    These expressions are evaluated before calling the function

    The values of these parameters are stored in memory areas

    called arguments.

    Parameters can be variables, but need not be variables.

    Even if parameters are variables, their values are not changed

    when value of arguments is changed inside the function.

    Arguments are local memory areas of the function.

    Lec-13 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    9

  • 7/31/2019 esc101-lec13

    6/6

    6

    Swap: A Function that will not work

    #include

    int main ()

    {

    int x = 10, y = 43;

    printf(x = %d, y = %d\n, x, y); // Print initial values of x and y

    swap (x, y); // Expectation is that x and y will be swapped

    printf(x = %d, y = %d\n, x, y); // If x and y are swapped, opposite values

    } // should be printed, but does not happen.

    void swap (int u, int v)

    {

    int temp;temp = u; u = v; v = temp; // Swap u and v.

    return; // Will not swap x and y.

    }

    Lec-13 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    10

    Parameter Passing

    Only values are passed to the arguments

    Changes to arguments in the function do not affect the callee

    We will, later, find a way to ensure that changes in the

    function arguments are reflected in the parameters at callee.

    Lec-13 Dheeraj Sanghi, CSE Dept., IIT Kanpur

    ESc101, 2011-12-Monsoon

    11