FUNCTIONS IN C PROGRAMMING.pdf

2,028 views 17 slides Apr 11, 2023
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.


Slide Content

RITHIKA. R. S,
I M.Sc. Bioinformatics,
Sri Krishna Arts and Science College, Coimbatore

Introduction…
Afunctionisagroupofstatementsthattogetherperforma
task.
Itcanbeexecutedfromasmanydifferentpartsinaprogram
asrequired,itcanalsoreturnavaluetocallingprogram.
ACprogramconsistsofoneormorefunctions.
Function is a subprogram that helps to reduce the
complexity of a program

Example…
intadd (int x, int y)
{
int z;
z=x+y;
return z;
}

Why functions?
Writingfunctionsavoidsrewritingthesamecode
overandover.
Usingfunctionitbecomeseasiertowriteaprogram
andkeeptrackofwhattheyaredoing.

Types of function in C…
Libraryfunctions
Functionssuchasprintf(),scanf()andsqrt()arepresentinc
libraryandtheyarepredefinedfunctions.
Userdefinedfunctions
Ausercancreatetheirownfunctionsforperforminganyspecific
taskofprogram.Thisissaidtobeuserdefinedfunctions.

Function definition
Function definition is also known as function implementation
and includes the following elements:
Function name
Function type (return type)
List of parameters
Local variable declaration
Function statements and
A return statement

return_type function_name (parameter list )
{
local variable declaration;
executable statement1;
executable statement2;
…..
…..
return statement;
}
The general format of a function definition is:

Function definition elements…
Functionheader:Thisconsistsof3partsnamely;Functiontype,
Functionname,Parameterlist
Thefunction_typespecifiesthetypeofvaluethatthefunctionis
expectedtoreturntheprogramcallingthefunction(suchasint,
float,double).
Iffunctionisnotreturninganything,thenspecifythereturntype
asvoid

Thefunction_nameisanyvalidCidentifier.
Thenameshouldbeappropriatetothetaskperformedbythe
function,suchassum,add,mul.
Parameterlistdeclaresthevariablesthatwillreceivethedata.
Parametersarealsoknownasarguments.
Theparametersofthefunctionareenclosedintheparenthesis,
thevariablesareseparatedbycommas.
Theremustbenosemicolonafterclosingtheparenthesis

Function declaration…
Function declaration (function prototype) is declaring the
properties of a function.
Consists of 4 parts
Function type(return type)
Function name
Parameter list
Terminating semicolon(;)
The general format is:
function_type function_name(parameter list);

Forexample,mulfunctioncanbedeclaredas
intmul(intm,intn);/*functionprototype*/
Variousacceptableformsformulfunctionsare:
intmul(int,int);
mul(inta,intb);
mul(int,int);
Whennoparametersandnoreturnvalue,itsprototypeis
givenasvoiddisplay(void)

Prototype declaration…
FunctionPrototypeDeclarationisastatementinwhich
programmerdescribesthreeinformationaboutafunction:
Symbolnameofthefunction
Returntypeofthefunction
Argumentsthatwillbetakenasinput
Aprototypedeclarationcanbein2placesinprogram
(1)aboveallthefunctionsand
(2)insideafunctiondefinition.

Whenadeclarationisplacedaboveallthefunctions,itis
referredtoasglobalprototype.
Whenadeclarationisplaceinafunctiondefinition,itis
referredalocalprototype

The return statement…
Afunctionmaysendavaluebacktothecallingfunction
throughthereturnstatement.
Thecalledfunctioncanreturnonlyonevaluepercall.
Thereturnstatementcanbewritteninanyoneoftheforms
given
•return;or
•return(expression);

The first form of plain return does not return any value.An example is
if (error)
return;
The second form of return with expression returns a value. For example,
int mul (int x, int y)
{
int p;
p=x*y;
return(p);
}
This function returns the value of p, the product of x and y. the last 2
statements cane be combined as
return(x*y);

Afunctionmayhavemorethanonereturnstatements.
if(x<=0)
return(0);
else
return(1);
Allfunctionsreturninttypedatabydefault.Wecanmakeafunctionto
returnparticulartypeofdatabyusingtypespecifierinthefunctionheader.
Functionsthatdocomputationsusingdoublesorfloat,yetreturnsints.
int product (void)
{
return(2.5*3.0)
}
willreturnthevalue7,onlyintegerpart.