52bc4func& Arrays Ln 6

Embed Size (px)

Citation preview

  • 7/25/2019 52bc4func& Arrays Ln 6

    1/8

    Programming in C : UAA 105 : L .Notes 6 : M S Prasad ( AISST)

    Fn!tions " Arra#s

    Fn!tions1. It is always recommended to break a large program into smaller modules

    which can be understood well and produces legible output. In C languagethis is carried out by defning functions( similar to sub routines in old proglanguage).

    type function name( arg1 , arg 2 )

    type arg 1 ; type arg 2; !ariables

    """""" e#pressions $ statements

    return ( return !alue )%

    &ype defnes the 'unction type that is int , char etc. argument( arg) 1 $ 2are !ariables which are reuired to e#ecute this 'unction or defne it . &hereturn return !alue should be as o' the type defnition o' the 'unction .

    'unction in C languageis a block o' code that per'orms a specifc task. It has aname and it is reusable i.e. it can be e#ecuted 'rom as many di*erent parts in a C+rogramas reuired. It also optionally returns a !alue to the calling program. &he'unction which does not return a !alue is known as void 'unction such as main (

    +roperties o' -unction

    .!ery 'unction has a uniue name. &his name is used to call 'unction 'rom/main()0 'unction.

    'unction can be called 'rom within another 'unction.

    'unction is independent and it can per'orm its task without inter!ention 'rom orinter'ering with other parts o' the program. 'unction per'orms a specifc task. task is a distinct ob that your program

    must per'orm as a part o' its o!erall operation, such as adding two or more integer,sorting an array into numerical order, or calculating a cube root etc.

    'unction returns a !alue to the calling program. &his is optional and dependsupon the task'unction is going to accomplish. uppose you want to ust show 'ew lines through

    'unction then it is not necessary to return a !alue. 3ut i' you are calculating area o'

    http://hubpages.com/_eknow/hub/A-Brief-History-of-the-C-Languagehttp://hubpages.com/_eknow/hub/Your-First-C-Programhttp://hubpages.com/_eknow/hub/Your-First-C-Programhttp://hubpages.com/_eknow/hub/Your-First-C-Programhttp://hubpages.com/_eknow/hub/Your-First-C-Programhttp://hubpages.com/_eknow/hub/A-Brief-History-of-the-C-Language
  • 7/25/2019 52bc4func& Arrays Ln 6

    2/8

    rectangle and wanted to use result somewhere in program then you ha!e to sendback (return) !alue to the calling 'unction.

    C language is collection of various inbuilt functions. If you have written a program inC then it is evident that you have used Cs inbuilt functions. printf, scanf, clrscr etc.all are Cs inbuilt functions.

    Example of function.

    int sum (int #, int y)int result;result 4 # 5 y;return (result);%

    d!antages o' using 'unctions6

    It makes possible top down modular programming. In this style o' programming, thehigh le!el logic o' the o!erall problem is sol!ed frst while the details o' each lowerle!el 'unctions is addressed later.

    &he length o' the source program can be reduced by using 'unctions at appropriateplaces.It becomes uncomplicated to locate and separate a 'aulty 'unction 'or 'urther study.

    'unction may be used later by many other programs this means that a cprogrammer can use 'unction written by others, instead o' starting o!er 'romscratch.

    'unction can be used to keep away 'rom rewriting the same block o' codes whichwe are going use two or more locations in a program. &his is especially use'ul i' thecode in!ol!ed is long or complicated.

    &ypes o' 'unctions6

    'unction may belong to any one o' the 'ollowing categories6Functions with no arguments and no return values.In such 'unctions we can not pass data (!alues like int, char etc) to the called'unction. imilarly, 'unction with no return type does not pass back data to thecalling 'unction. It is one o' the simplest types o' 'unction in C. &his type o' 'unctionwhich does not return any !alue cannot be used in an e#pression it can be used only

    as independent statement.Functions with arguments and no return values

    &his type o' 'unction can accept data 'rom calling 'unction. In other words, you senddata to the called 'unction 'rom calling 'unction but you cannot send result databack to the calling 'unction. 7ather, it displays the result on the terminal. 3ut wecan control the output o' 'unction by pro!iding !arious !alues as arguments.Functions with arguments and return values.

  • 7/25/2019 52bc4func& Arrays Ln 6

    3/8

    &his type o' 'unction can send arguments (data) 'rom the calling 'unction to thecalled 'unction and wait 'or the result to be returned back 'rom the called 'unctionback to the calling 'unction. nd this type o' 'unction is mostly used in programmingworld because it can do two way communications; it can accept data as argumentsas well as can send back data as return !alue. &he data returned by the 'unctioncan be used later in our program 'or 'urther calculations.

    -unctions that return multiple !alues.

    o 'ar, we ha!e learned and seen that in a 'unction, return statement was able to

    return only single !alue. &hat is because; a return statement can return only one

    !alue. 3ut i' we want to send back more than one !alue then how we could do this8

    9e ha!e used arguments to send !alues to the called 'unction, in the same way we

    can also use arguments to send back in'ormation to the calling 'unction. &he

    arguments that are used to send back data are called Output Parameters.

    #ample 6

    -unctions with no arguments and return !alues.9e may need a 'unction which does not take any argument but only returns !aluesto the calling 'unction then this type o' 'unction is use'ul. &he best e#ample o' thistype o' 'unction is /getchar()0 library 'unction which is declared in the header fle/stdio.h0.

    #ample simple 'unction to add two integers.8

    12:

    ?@111121:11PO2P 4 $ o' scoresOPOP 5iI # siSe o'( int )# ma# test 5 # siSeo' (int)

    cores O>PO2P 4 1 5 > G 2 G : 5 2 G 2 4 1= siSe o' int in 1= bit system is2 bytes.%

    2.Character strings are treated as array o' strings by the compiler. .g / I amgoing U is taken as array o' character including end o' line .

    Example : indexing method of to access array elements also known asRelative addressing

    main ()

    int !alue O12P ; G defned a single dimension array o' 12 elements)

    'or ( inde# 4 ; inde# B 12 ; inde# 55 )

    !alues O inde#P 4 2 G O inde# 5 P ; G this is to put the !alues o' theelements in the array G

    'or ( inde# 4 ; inde# B 12 ; inde# 55 )

    print' ( / !alue at inde# 4 E 2d is E :d F n / , inde# , !alue inde#P ) ;

    %

  • 7/25/2019 52bc4func& Arrays Ln 6

    7/8

    Passing an array to a function

    Ainclude Bstdio.h

    Ainclude Bstring.h

    !oid my'unct (int , char O P);

    int main ( )

    char nameO2P 4 D a. b. cde'ghi /

    my'unct (2, name);

    %

    !oid my'unct (int len , char te#tO P)

    int k ;

    print' (DEdFnD, strlen(te#t)) ;

    'or (k 4 ; k B len ; k55) print' (DEcD, te#tOkP) ;

    %

    7esult would be 1 , ( includes blanks also)

    a. b. cde'ghi

    inding the si!e of array

    int array O 1P

    siSe o' ( array O :P) ; G returns the element siSe in bytes . since it is declared inthence 'or :2 bit computer it is bytes.

    siSe o' ( array) G returns to total siSe o' array i.e. 1 # bytes 4 G

    9hen we pass an array to a 'unction actually its siSe is lost.

    -or multi dimensional arrays , the siSe o' all , but the frst dimension must bespecifed. .g

    Char name O P O :P

    Array Processing using Pointers

    In general , e!ery array notation e#pression has an eui!alent pointer notatione.g

  • 7/25/2019 52bc4func& Arrays Ln 6

    8/8

    tree O 2 P is eui!alent to G ( tree 5 2).

    Tote 6 i' we use array name in any e#pression , actually the !alue o' thate#pression is the / array address0

    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""