storage classes 4

Upload: ravitejabavandla

Post on 09-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Storage Classes 4

    1/60

    Computer Science: A Structured Programming Approach Using C 1

    Functions

    All c programs have at least one functionthat is the main.

    When the program grows into larger, mayhave many disadvantages.

    1. Difficult to write a larger programs

    2. Difficult to identify the logical errors and

    correct.3. Difficult to read and understand.

    4. Larger programs prone for more errors.

  • 8/8/2019 Storage Classes 4

    2/60

    Computer Science: A Structured Programming Approach Using C 2

    A program is divided into a main module and its related

    modules.

    Each module is in turn divided into sub modules until the

    resulting modules are smaller; that is, until they are not

    further division.

    The process of subdividing a problem into manageable partscalled top-down design.

  • 8/8/2019 Storage Classes 4

    3/60

    Computer Science: A Structured Programming Approach Using C 3

    FIGURE 4-2 Structure Chart

  • 8/8/2019 Storage Classes 4

    4/60

  • 8/8/2019 Storage Classes 4

    5/60

    Computer Science: A Structured Programming Approach Using C 5

    A large program divided into series ofindividual related programs called

    modules. These modules are calledfunctions.

    Functions are classified into two categories

    1. Library functions ( built-in functions).

    2. User-defined functions

  • 8/8/2019 Storage Classes 4

    6/60

    Computer Science: A Structured Programming Approach Using C 6

    Library (built in) functions

    C is a library it is a collection of various types of functionswhich perform some standard and predefined tasks.

    This are part of the c compiler that have been written forgeneral purpose are called library functions.

    exampleprintf()scanf()sqrt()

    Advantage1.No need to write the code because the functions are

    already available so it makes the programmers job mucheasier.

    2. these functions are used anywhere in the program.

  • 8/8/2019 Storage Classes 4

    7/60

    Computer Science: A Structured Programming Approach Using C 7

    User defined functions

    The functions are written by theprogrammer to achieve some work

  • 8/8/2019 Storage Classes 4

    8/60

    Computer Science: A Structured Programming Approach Using C 8

    Advantages of function

    1. Readability of the program can beincreased. Problem can be easilyunderstandable.

    2. Reuse- the function written in oneprogram can also used in other program.

    (reduce the size).

    1. Makes the program easy for testing,coding and debugging.

    2. Saves time as well as space.

  • 8/8/2019 Storage Classes 4

    9/60

    Computer Science: A Structured Programming Approach Using C 9

    FIGURE 4-3 Structure Chart for a C Program

  • 8/8/2019 Storage Classes 4

    10/60

    Computer Science: A Structured Programming Approach Using C 10

    4-3 User-Defined Functions

    LikeLike everyevery otherother variablevariable inin C,C, functionsfunctions mustmust bebe bothboth

    declareddeclared andand defineddefined.. TheThe functionfunction declarationdeclaration givesgives

    thethe returnreturn typetype andand functionfunction namename needsneeds toto bebe defineddefined

    laterlater.. TheThe functionfunction definitiondefinition containscontains thethe codecode forfor aafunctionfunction..

    Function Definition

    Function Declaration

    The Function Call

    Topics discussed in this section:Topics discussed in this section:

  • 8/8/2019 Storage Classes 4

    11/60

    Computer Science: A Structured Programming Approach Using C 11

    Definition

    A function is a self- contained block of codethat performs a particular task.

    It takes some data from the main programand may or may not returns a value.

  • 8/8/2019 Storage Classes 4

    12/60

    Computer Science: A Structured Programming Approach Using C 12

    A function name is used three times: for declaration, in acall, and for definition.

    NoteNote

  • 8/8/2019 Storage Classes 4

    13/60

    Computer Science: A Structured Programming Approach Using C 13

    Function declaration

    Funtion prototype(syntax)

    Return_type function_name (argument list);

  • 8/8/2019 Storage Classes 4

    14/60

    Computer Science: A Structured Programming Approach Using C 14

    Definition

    Syntax for the function definitionReturntype function_name(argument list/parameters list)argument list declaration{local variable declarations:Statements;return(expression);}//function.

    All the parts are not essential.

    Argument list and declaration optional.Return statement is also optional.

  • 8/8/2019 Storage Classes 4

    15/60

    Computer Science: A Structured Programming Approach Using C 15

    main(){-----------

    ----------

    func1();

    ---------

    --------

    funct2();--------

    ---------

    funct3();---------

    -------

    }//main

    func1(){----------

    -----------

    }func1func2(){--------

    --------

    }//func2fucn3(){----------

    ---------

    }//func3

  • 8/8/2019 Storage Classes 4

    16/60

  • 8/8/2019 Storage Classes 4

    17/60

    Computer Science: A Structured Programming Approach Using C 17

    main()\\calling function

    {

    func1();

    printf(we are in main\n);}//main

    func1()\\ called funciton

    {printf(we are in func1\n);

    }//func1

  • 8/8/2019 Storage Classes 4

    18/60

    Computer Science: A Structured Programming Approach Using C 18

    Points to remember

    1. C program must have at least one function

    2. If program have one function it must be main

    3. If program have more than function, in which one mustbe main because program execution starts from thatonly.

    4. We can use unlimited no of functions in one program.

    5. Each function is called in the sequence we havespecified in the main function.

    6. After each function task is over the control passed to thecalling function(main()).

    7

  • 8/8/2019 Storage Classes 4

    19/60

    Computer Science: A Structured Programming Approach Using C 19

    main(){printf(\n I am calling other functions);func1();

    func2();func3();Printf(all functions are called\n);}//mainfunc1(){printf(in func1\n);}//func1

    func2(){printf(in func2\n);}//func2func3(){printf(in func1\n);}//func3

    Output:

    I am calling other functionin func1in func2in func3all functions are called.

  • 8/8/2019 Storage Classes 4

    20/60

    Computer Science: A Structured Programming Approach Using C 20

    7 One function can also call the other function which has alreadybeen called.main()

    {printf(I am in main\n); outputfunc1(); I am in mainprintf(I back in main\n); in func1}//mainfunc1() In func2{ In func3printf(in func1\n); I back in func2func2(); I back in func1printf(I back in func1\n); I back in main}//func1func2(){printf(In func2\n);func3();printf(I back in func2\n);}//func2func3(){printf(in fac3\n);}//func3

  • 8/8/2019 Storage Classes 4

    21/60

    Computer Science: A Structured Programming Approach Using C 21

    FIGURE 4-5 Declaring, Calling, and Defining Functions

  • 8/8/2019 Storage Classes 4

    22/60

  • 8/8/2019 Storage Classes 4

    23/60

    Computer Science: A Structured Programming Approach Using C 23

    PROGRAM 4-2 voidFunction with a Parameter

  • 8/8/2019 Storage Classes 4

    24/60

    Computer Science: A Structured Programming Approach Using C 24

    PROGRAM 4-2 voidFunction with a Parameter

  • 8/8/2019 Storage Classes 4

    25/60

    Computer Science: A Structured Programming Approach Using C 25

    PROGRAM 4-2 voidFunction with a Parameter

  • 8/8/2019 Storage Classes 4

    26/60

    Computer Science: A Structured Programming Approach Using C 26

    FIGURE 4-7 Non-void Function without Parameters

  • 8/8/2019 Storage Classes 4

    27/60

    Computer Science: A Structured Programming Approach Using C 27

    FIGURE 4-8 Calling a Function That Returns a Value

  • 8/8/2019 Storage Classes 4

    28/60

    Computer Science: A Structured Programming Approach Using C 28

    PROGRAM 4-3 Read a Number and Square It

  • 8/8/2019 Storage Classes 4

    29/60

    Computer Science: A Structured Programming Approach Using C 29

    PROGRAM 4-3 Read a Number and Square It

  • 8/8/2019 Storage Classes 4

    30/60

    Computer Science: A Structured Programming Approach Using C 30

    PROGRAM 4-3 Read a Number and Square It

  • 8/8/2019 Storage Classes 4

    31/60

    Computer Science: A Structured Programming Approach Using C 31

    PROGRAM 4-3 Read a Number and Square It

  • 8/8/2019 Storage Classes 4

    32/60

    Computer Science: A Structured Programming Approach Using C 32

    FIGURE 4-9 Function Definition

  • 8/8/2019 Storage Classes 4

    33/60

    Computer Science: A Structured Programming Approach Using C 33

    Categories of functions

    Based on the functions return type and itsarguments the functions are categorized into 4different types.

    1. Functions with no arguments and no returnvalues

    2.Functions with no arguments and return values

    3. Functions with arguments and no return values4. Functions with arguments and return values

  • 8/8/2019 Storage Classes 4

    34/60

  • 8/8/2019 Storage Classes 4

    35/60

  • 8/8/2019 Storage Classes 4

    36/60

    Computer Science: A Structured Programming Approach Using C 36

    2. FUNCTIONS WITH NO ARGUMENTS AND RETURN VALUES

    There are two ways that a function terminatesexecution and returns to the caller.

    1. When the last statement in the function has

    executed and conceptually the functions ending} is encountered.

    2. Whenever it faces return statement.

    there is no data transfer from the calling functionto the called function. But, there is data transferfrom called function to the calling function.

  • 8/8/2019 Storage Classes 4

    37/60

    Computer Science: A Structured Programming Approach Using C 37

    example// C program to find sum of two numbers using functions with no arguments and return values

    #include int sum ();

    void main () { int c; clrscr (); c=sum (); /*calling function */ printf (\n The sum = %d, c); getch (); } int sum () { int x, y;

    printf (\n Enter the values of x and y: ); scanf (%d%d, &x, &y); return (x+y); }

  • 8/8/2019 Storage Classes 4

    38/60

    Computer Science: A Structured Programming Approach Using C 38

    3. FUNCTIONS WITH ARGUMENTS AND NO RETURN VALUES

    In this category there is data transfer from the calling function to thecalled function using parameters. But, there is no data transfer fromcalled function to the calling function.

    Function arguments The arguments that are supplied in to two categories 1. actual arguments/parameters 2. formal arguments/parameters

    Actual arguments/parameters Actual parameters are the expressions in the calling functions.

    These are the parameters present in the calling statement (functioncall).

    formal arguments/parameters formal parameters are the variables that are declared in the header

    of the function definition. These list defines and declares that willcontain the data received by the function. These are the valueparameters, copies of the values being passed are stored in thecalled functions memory area.

    Note:Actual and Formal parameters must match exactly in type,order, and number. Their names however, do not need to match.

  • 8/8/2019 Storage Classes 4

    39/60

    Computer Science: A Structured Programming Approach Using C 39

    ->the values of actual arguments are assigned to the formal arguments.func1(x1, x2, x3,xn);

    | | | |

    func1(y1, y2, y3,yn)

    ->In case, the actual arguments are more than the formal arguments , extraactual arguments are discarded.

    -> on other hand, the formal arguments are more, the extra formal argumentsare initialized to some garbage values.

    -> if any mismatch found in their data type may also takes some value. No

    error is reported.

  • 8/8/2019 Storage Classes 4

    40/60

    Computer Science: A Structured Programming Approach Using C 40

    example//C program to find sum of two numbers using functions with arguments and no return values

    #include void sum (int ,int ); void main () { int a, b; clrscr (); printf (\n Enter the values of a and b: );

    scanf (%d%d, &a, &b); sum (a, b); /*calling function */ getch (); } void sum (int x, int y) { int z; z=x+y; printf (\n The Sum =%d, z); }

  • 8/8/2019 Storage Classes 4

    41/60

    Computer Science: A Structured Programming Approach Using C 41

    4. FUNCTIONS WITH ARGUMENTS AND RETURN

    VALUES

    In this category there is data transferbetween the calling function and calledfunction.

  • 8/8/2019 Storage Classes 4

    42/60

    Computer Science: A Structured Programming Approach Using C 42

    example

    //C program to find sum of two numbers using functions with arguments and return values

    #include

    int sum (int ,int ); void main () { int a, b; clrscr (); printf (\n Enter the values of a and b: ); scanf (%d%d, &a, &b); c=sum (a, b); /*calling function */ printf (\n The Sum =%d, c); getch (); }

    int sum (int x, int y) { int z; return x+y; }

  • 8/8/2019 Storage Classes 4

    43/60

    Computer Science: A Structured Programming Approach Using C 43

    Note: generally we are using functions with argumentsand return values (category 4) in our applications. Whybecause the job of a function is to perform a well-

    defined task, it carries inputs and sends result afterexecuted. In real world the programming teams codesthe modules (functions) according to the input

    (arguments) given by the team coordinators.

  • 8/8/2019 Storage Classes 4

    44/60

  • 8/8/2019 Storage Classes 4

    45/60

  • 8/8/2019 Storage Classes 4

    46/60

  • 8/8/2019 Storage Classes 4

    47/60

  • 8/8/2019 Storage Classes 4

    48/60

  • 8/8/2019 Storage Classes 4

    49/60

    Computer Science: A Structured Programming Approach Using C 49

    Storage classes in c

    The variables are stored in computer.

    - Basically variables are stored in computer in two locations.

    - 1. memory 2. registers.

    - Location of the variable to store is determined by the storage

    classes.- It also tells us the following

    1. where the variable would be stored.

    2. what is initial value of the variable, if variable is not initializedwhile declaring.

    3. what is the scope of the variable; i.e in which functions thevalue of the variable would be available.

    4. what is the life of a variable; i.e how long variable would beexist.

  • 8/8/2019 Storage Classes 4

    50/60

  • 8/8/2019 Storage Classes 4

    51/60

    Computer Science: A Structured Programming Approach Using C 51

    Auto (default and most common)

    -Memory for automatic variables is allocated when a block or function isentered.

    - They are defined and are local to the block.

    - When the block is exited, the system releases the memory that was

    allocated to the auto variables, and their values are lost.

    - For Example, a variable in the body of a loop is created and destroyed ineach iteration.

    declaration- auto type variable_name;

    int a;

    By default a is treated as auto.

  • 8/8/2019 Storage Classes 4

    52/60

    Computer Science: A Structured Programming Approach Using C 52

    Features of auto variable when they declared

    Storage Memory

    Default initial value garbage value

    Scope local to the block

    in which variabledefined

    Life remains within theblock in whichvariable defined.

  • 8/8/2019 Storage Classes 4

    53/60

    Computer Science: A Structured Programming Approach Using C 53

    Example- auto

    main(){auto int x=10;{auto int x=20;

    {auto int x=30;printf(%d,x);// 30

    }printf(%d,x);//20

    }Printf(%d,x);//10}//main

  • 8/8/2019 Storage Classes 4

    54/60

    Computer Science: A Structured Programming Approach Using C 54

    Static storage classes

    syntax-

    static type identifier;

    A variable with a static extent is created andinitialized to its value when the function

    executed for first time and destroyed when it is

    exit from the function. This is true no matter

    how many times the declaration is encountered

    during the execution.

  • 8/8/2019 Storage Classes 4

    55/60

    Computer Science: A Structured Programming Approach Using C 55

    Features of static variable

    - Storage -------- Memory

    - Default initial value ----- zero

    - Scope ------------ Local to the block inwhich the variable is defined.

    - Life --------- value of the variable persistsbetween different function calls.

    - Once memory is allocated to the staticvariable is persists its value until he end ofthe program

  • 8/8/2019 Storage Classes 4

    56/60

  • 8/8/2019 Storage Classes 4

    57/60

  • 8/8/2019 Storage Classes 4

    58/60

  • 8/8/2019 Storage Classes 4

    59/60

    Computer Science: A Structured Programming Approach Using C 59

    exampleint i;main()

    {

    printf(\n%d=,i);

    demo1();

    demo1();

    demo2();

    demo2();

    }//main

    demo1()

    {i++;

    printf(\n%d,i);

    }//demo1()

    demo2()

    {

    i--;printf(\n%d,i);

    }//demo2()

    Output

    0

    12

    1

    0

  • 8/8/2019 Storage Classes 4

    60/60