c Unit 7 Function Last Print

Embed Size (px)

Citation preview

  • 8/18/2019 c Unit 7 Function Last Print

    1/8

    Programming Language Functions Unit 7

    Functions

    A function is a self- contained program segment that carries out some specific, well-defined tasks. Functions break large computing tasks into smaller ones, and enable peopleto build on what others have done instead of starting over from scratch. Appropriatefunctions hide details of operation from parts of the program that don't need to know about

    them, thus clarifying the whole, and easing the pain of making changes.

    C has been designed to make functions efficient and easy to use; C programs generallyconsist of many small functions rather than a few big ones. A program may reside in one ormore source files. ource files may be compiled separately and loaded together, along withpreviously compiled functions from libraries.

    The advantages of functions are as follows:! Can be developed, tested, debugged, and complied independently by different

    member of a programming team.

    ! "here is no chance of code duplication.! #asily maintainable.! #ach function is reusable! $arge member of programmer can be involved! %rogram can be developed in short period of time! Can be developed in different places

    Components of function:! Function prototype . Function definition .Function calling! Function parameters . Function return

    /*program to multiply any two numbers by using function * &include (stdio.h)include (conio.h)

    int product*int, int+; &&Function prototypevoid main*+

    int a, b, c;printf* #nter any two numbers +;scanf* /d/d ,0a,0b+; c1product*a,b+; &&function callingprintf* "he product of two number is2/d ,c+;getch*+;

    3int product*int 4, int y+ &&function definition

    int p; p145y;return*p+;

    3

    Function prototypeint product(int, int);

    Function prototype provides the following information to the compiler2! "he name of the function i.e. product! "he type of the value return eg.int , float, void etc! "he number and the type of the arguments that must be supplied in a function call

    Function definitionint product*int 4, int y+

    int p;

    Ashok Pandey Page 1 of 8

  • 8/18/2019 c Unit 7 Function Last Print

    2/8

    Programming Language Functions Unit 7p145y;return*p+;

    3"he first line of the function definition is known as function declaratory and is followed bythe function body. 6oth declarator and function body makeup the function definition. 7f thefunction is defined before the main function then its prototypes declaration is optional.

    Function call

    c1product*a, b+;A function call is specified by the function name followed by the arguments enclosed inparentheses and terminated by a semicolon. "he return type is not mentioned in thefunction call.

    Function parameters"he parameters specified in the function call are known as actual parameters and thosespecified in the function declarator are known as formal parameters.

    #g a, b in c1product*a,b+; are known as actual parameters and4, y in int product*int 4, int y+ are known as formal parameters.

    Function returnFunction can be grouped into two type2! Function that do not have a return type ie. 8oid function.! Function that do have a return value ie return*p+.

    Types of function:! Function returning value and passing arguments! Function returning no value but passing arguments! Function returning value and passing no arguments! Function returning no value and passing no arguments

    Function returning value and passing arguments:include (stdio.h)include (conio.h)

    int product*int, int+; &&Function prototypevoid main*+

    int a, b, c;printf* #nter any two numbers +; scnaf* /d/d ,0a,0b+;c1product*a,b+; &&function callingprintf* "he product of two number is2/d ,c+;getch*+;

    3int product*int 4, int y+ &&function definition

    int p;p145y;return*p+;

    3

    Function returning no value and passing arguments:include (stdio.h)include (conio.h)

    void product*int, int+; &&Function prototypevoid main*+

    int a, b;printf* #nter any two numbers +; scnaf* /d/d ,0a,0b+;product*a,b+; &&function callinggetch*+;

    Ashok Pandey Page 2 of 8

  • 8/18/2019 c Unit 7 Function Last Print

    3/8

    Programming Language Functions Unit 73void product*int 4, int y+ &&function definition

    int p;p145y;printf* "he product of two number is2/d ,p+;

    3

    Function returning value and passing no arguments:

    include (stdio.h)include (conio.h)int product*+; &&Function prototype

    void main*+ int c;

    c1product*+; &&function callingprintf* "he product of two number is2/d ,c+; getch*+;

    3int product*+ &&function definition

    int a, b, p;

    printf* #nter any two numbers +; scnaf* /d/d ,0a,0b+;p1a5b;return*p+;

    3Function returning no value and passing no arguments:

    include (stdio.h)include (conio.h)

    void product*+; &&Function prototypevoid main*+

    product*+; &&function callinggetch*+;

    3void product*+ &&function definition

    int a, b, p;printf* #nter any two numbers +;scanf* /d/d ,0a,0b+;p1a5b;printf* the product of two number is2/d , p+;

    3

    Preprocessor directives:

    "he preprocessor directives are e4ecuted at the beginning of the compilation process. "heyare not program statements, neither it is a part of function body, and do not end with asemicolon, as program statements. 7t begins with the pound sign * +.%reprocessordirectives are instruction to the compiler itself."he preprocessor directive include tells thecompiler to insert another file into the same source file.

    Macros:

    9acros are the single identifiers that are e:uivalent to e4pression, complete statement orgroup of statements. "he work of macros is similar to function, however they treateddifferently during the compilation process. "he preprocessor directive define can be usedto define macros.

    Ashok Pandey Page 3 of 8

  • 8/18/2019 c Unit 7 Function Last Print

    4/8

    Programming Language Functions Unit 7/* a simple macro * &

    include (stdio.h)include (conio.h)define %7 .

  • 8/18/2019 c Unit 7 Function Last Print

    5/8

    Programming Language Functions Unit 7long int fact*int num+

    if*num11 +return

  • 8/18/2019 c Unit 7 Function Last Print

    6/8

    Programming Language Functions Unit 7include (stdio.h)include (conio.h)

    void "KD*int, char, char, char+; &&Function prototypevoid main*+

    int n;printf* #nter number of disks +;scanf* /d ,0n+; "KD*n,LKL,L L,L7L+;getch*+;

    3void "KD*int n, char A, char 6, char C+

    if*n) +

    "KD*n-;iNN+

    Ashok Pandey Page 6 of 8

  • 8/18/2019 c Unit 7 Function Last Print

    7/8

    Programming Language Functions Unit 7staticdemo*+;

    3void staticdemo*+

    static int p1>;pNN;printf* p1/dMt ,p+;

    3,utput:

    O P Q <

    egister storage class:"he storage class register tells the compiler that the associated variables should be stored inhigh speed memory register. "he use of the storage class register is an attempt to improvee4ecution speed. "he numbers of variables which can be declared register are limited. "hefre:uently used variables are declared as register variable."he register variables can be declared

    as2 register int i;register float b;

    "xternal storage class:"he variables that are active and alive throughout the entire program are known as e4ternalvariable. Rhen a variable is declared outside a function storage is permanently assigned toit, and its storage class is e4tern.

    "xample: program to illustrate the use of externalvariable /*file12c */

    include(stdio.h)int g;

    void fun

  • 8/18/2019 c Unit 7 Function Last Print

    8/8

    Programming Language Functions Unit 7,utput:

    #nter value of g2 <File