14593 function

Upload: kirandeep-singh-gandham

Post on 02-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 14593 Function

    1/36

    Functions

  • 8/11/2019 14593 Function

    2/36

    Definition

    A function is a group of statements that

    performs some kind of task.

  • 8/11/2019 14593 Function

    3/36

    3 essential things about Function

    function prototypecan be thought of as

    specifying its interface

    Function callingwhen a function is being

    called by some other function

    function definition specifies what a function

    does.

  • 8/11/2019 14593 Function

    4/36

    Advantages of Functions

    Less code duplication

    easier to read

    Easier to update programs Easy to find out errors--each function can be

    verified separately

    Reusable codethe same functions can be used in different programs

  • 8/11/2019 14593 Function

    5/36

    Syntax of Function

    Return_type function_ name (arguments)

    {

    Body of function Return value;

    }

  • 8/11/2019 14593 Function

    6/36

    Program of additionWITHOUTusing

    Functions

    #include #include

    Void main()

    {

    Int a , b, c;

    Scanf(%d%d, &a,&b);

    c=a+b;

    Printf(value of c= %d,c);

    }

  • 8/11/2019 14593 Function

    7/36

    Scenario 1

    Main function will call another function to do

    its own task.

    For example--- main function will call add

    functionto do the addition and display the

    result

  • 8/11/2019 14593 Function

    8/36

    Program of additionusing Functions

    #include

    #include

    Void add(); // function declaration Void main()

    {

    Add(); // calling of add function

    }

    Void add() // function definition

    {

    Int a , b, c; Scanf(%d%d, &a,&b);

    c=a+b;

    Printf(value of c= %d,c);

    }

  • 8/11/2019 14593 Function

    9/36

    Doing the additionMORE THAN ONE

    TIMEin the same program

    without_function.doc

    http://localhost/var/www/apps/conversion/tmp/scratch_6/without_function.dochttp://localhost/var/www/apps/conversion/tmp/scratch_6/without_function.dochttp://localhost/var/www/apps/conversion/tmp/scratch_6/without_function.dochttp://localhost/var/www/apps/conversion/tmp/scratch_6/without_function.dochttp://localhost/var/www/apps/conversion/tmp/scratch_6/without_function.doc
  • 8/11/2019 14593 Function

    10/36

    Using Function

    using_function.doc

    http://localhost/var/www/apps/conversion/tmp/scratch_6/using_function.dochttp://localhost/var/www/apps/conversion/tmp/scratch_6/using_function.dochttp://localhost/var/www/apps/conversion/tmp/scratch_6/using_function.dochttp://localhost/var/www/apps/conversion/tmp/scratch_6/using_function.doc
  • 8/11/2019 14593 Function

    11/36

    Calling Function & Called function

    In previous example,

    Main function is the Calling function----who ismaking a call to add function

    Add function is said to be called functionwho is responsible for actually performing the

    addition of two numbers

  • 8/11/2019 14593 Function

    12/36

    Main function --------------is the master

    Add function -------------is the slave , who isperforming some task.

  • 8/11/2019 14593 Function

    13/36

    Scenario 2

    Main function will call another function to doits own task + main function wants to printresult of its own.

    For example--- main function will call addfunctionto do the addition,now after doing

    addition the result is sent back to mainfunction , then main function will displaythe result.

    U S

  • 8/11/2019 14593 Function

    14/36

    Use o returnStatement

    #include

    #include

    intadd(); // function declaration Void main()

    {

    Int basket;

    basket=Add(); // calling of add function

    Printf(value of addition = %d, basket);

    }

    intadd() // function definition

    { Int a , b, c;

    Scanf(%d%d, &a,&b);

    c=a+b;

    return c;

    }

  • 8/11/2019 14593 Function

    15/36

    Another example

    Find out the greater number among two

    numbers using Functions , if a>b then

    calculate the Quotient , otherwise set answer

    =0.

    grtr_without using return.docx

    using return statement

    using_return.doc

    http://localhost/var/www/apps/conversion/tmp/scratch_6/grtr_without%20using%20return.docxhttp://localhost/var/www/apps/conversion/tmp/scratch_6/using_return.dochttp://localhost/var/www/apps/conversion/tmp/scratch_6/using_return.dochttp://localhost/var/www/apps/conversion/tmp/scratch_6/grtr_without%20using%20return.docxhttp://localhost/var/www/apps/conversion/tmp/scratch_6/grtr_without%20using%20return.docxhttp://localhost/var/www/apps/conversion/tmp/scratch_6/grtr_without%20using%20return.docx
  • 8/11/2019 14593 Function

    16/36

    Disadvantage of return statement

    We can only ONE value from one function toanother function

  • 8/11/2019 14593 Function

    17/36

    Program to check whether a given number is

    EVEN or ODD using Function

    chk_even_odd.docx

    http://localhost/var/www/apps/conversion/tmp/scratch_6/chk_even_odd.docxhttp://localhost/var/www/apps/conversion/tmp/scratch_6/chk_even_odd.docxhttp://localhost/var/www/apps/conversion/tmp/scratch_6/chk_even_odd.docxhttp://localhost/var/www/apps/conversion/tmp/scratch_6/chk_even_odd.docx
  • 8/11/2019 14593 Function

    18/36

  • 8/11/2019 14593 Function

    19/36

    Scenario 3

    Earlier responsibility of main function is just

    to call another function.

    Another function is responsible for-----------

    Taking the input of two numbers

    doing the addition of numbers.

    BUT NOW , main function wants to takeinput of two numbers of its own ,wants to

    pass these numbers to another function.

  • 8/11/2019 14593 Function

    20/36

    How to pass values from one

    function to another

    Either using

    ------------Call by VALUE

    Or using

    ------------Call by REFERENCE / Call by ADDRESS

  • 8/11/2019 14593 Function

    21/36

    What is parameter/ argument

    Passing values from one function to another

    function ---------------is said to be PARAMETER

    or ARGUMENT.

    TWO TYPES of parameter/ argument-----

    ACTUAL PARAMETER----used at the time of

    function calling

    FORMAL PARAMETER-----used at the time of

    function definition

  • 8/11/2019 14593 Function

    22/36

    What is call by VALUE & call by

    REFERENCE

    In call by value , we pass the actual values

    at the time of function calling.

    In call by reference, we pass addressinstead of values, at the time of function

    calling.

  • 8/11/2019 14593 Function

    23/36

    Local variable & Global Variable

    Local variables------------

    ----------------are limited to one block.

    ----------------its value does not exist in any

    other function.

    -------------by default value is garbage

    Global Variable-----------

    --------------- are used in whole program

    ---------------by default value is zero

    ---------------its value persists for entire program

  • 8/11/2019 14593 Function

    24/36

    Addition of two numbers using call

    by VALUE

    call_value.doc

    http://localhost/var/www/apps/conversion/tmp/scratch_6/call_value.dochttp://localhost/var/www/apps/conversion/tmp/scratch_6/call_value.doc
  • 8/11/2019 14593 Function

    25/36

    Area of circle using Call by VALUE

    and using RETURN

    call_value_return.doc

    Program of factorial using FUNCTIONS

    http://localhost/var/www/apps/conversion/tmp/scratch_6/call_value_return.dochttp://localhost/var/www/apps/conversion/tmp/scratch_6/call_value_return.doc
  • 8/11/2019 14593 Function

    26/36

    Program of factorial using FUNCTIONS # include

    #include

    int fact(int a);

    Void main()

    {

    Int z, basket;

    scanf(%d, &z);

    basket= Fact(z);

    Printf(factorial is=%d , basket);

    }

    Int fact(int a)

    {

    Int i, ans=1;

    For (i= 1; i

  • 8/11/2019 14593 Function

    27/36

    Scenario 4

    Instead of passing values as arguments,

    Pass the ADDRESSES of variables , at the time

    of function calling.

  • 8/11/2019 14593 Function

    28/36

    POINTER

    It is a special type of variable,

    which contains the address of any other

    variable , instead of value.

    int * p;

    Int q; P= &q;

  • 8/11/2019 14593 Function

    29/36

    Addition of two numbers using call

    by REFERENCE

    call_ref.doc

    http://localhost/var/www/apps/conversion/tmp/scratch_6/call_ref.dochttp://localhost/var/www/apps/conversion/tmp/scratch_6/call_ref.doc
  • 8/11/2019 14593 Function

    30/36

    Area of circle using Call by REFERENCE

    and using RETURN

    call_ref_return.doc

    S NO call by value call by reference

    http://localhost/var/www/apps/conversion/tmp/scratch_6/call_ref_return.dochttp://localhost/var/www/apps/conversion/tmp/scratch_6/call_ref_return.doc
  • 8/11/2019 14593 Function

    31/36

    S. NO. call by value call by reference

    1. Values are passed at the

    time of function

    Address is passed at the

    time of function

    2. No use of pointer use of pointer

    3. Simple , easy to

    understand

    Complicated, difficult to

    learn

    4. Also knows as downward

    communication

    Also knows as upward

    communication

    5. NO need to worry about

    the memory addresses

    User must know about

    the concepts of the

    memory addresses

    6. ( IMPORTANT) Changes made in

    function will not

    reflected back in main

    function

    Changes made in

    function will reflected

    back in main function

    Swapping of two numbers using CALL BY VALUE

  • 8/11/2019 14593 Function

    32/36

    Swapping of two numbers using CALL BY VALUE

    #include

    #include

    Void swap(int a, int b);

    Void main()

    {

    Int a, b;

    Scanf(%d %d, &a , &b);

    Swap(a,b);

    Printf(value of a= %d \n value of b= %d, a, b);

    }

    Void swap(int a, int b)

    {

    Int temp;

    Temp=a;

    a=b;

    b=temp;

    Printf(value of a= %d \n value of b= %d , a, b);

    }

    Swapping of two numbers using CALL BY REFERENCE

  • 8/11/2019 14593 Function

    33/36

    Swapping of two numbers using CALL BY REFERENCE

    #include

    #include

    Void swap(int *a, int *b); Void main()

    {

    Int a, b;

    Scanf(%d %d, &a , &b);

    Swap(&a,&b); Printf(value of a= %d \n value of b= %d, a, b);

    }

    Void swap(int *a, int *b)

    {

    Int temp;

    Temp=*a;

    *a=*b;

    *b=temp;

    Printf(value of a= %d \n value of b= %d , *a, *b);

    }

  • 8/11/2019 14593 Function

    34/36

    Recursion

    A function can call itself either directly or

    indirectly.

    We can use recursion , where we want to

    execute the same set of instructions,

    repeatedly.

    Factorial of a number using RECURSION

  • 8/11/2019 14593 Function

    35/36

    Factorial of a number using RECURSION #include

    #include

    int fact(int a);

    void main()

    {

    int z,ans;

    scanf("%d", &z);

    ans=fact(z);

    printf("factorial is = %d", ans);

    }

    int fact(int b)

    {

    int ans;

    if(b==0)

    return 1;

    else

    return b*fact(b-1);

    }

  • 8/11/2019 14593 Function

    36/36