UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
Introduction ProgramModulesinC P M d l i C MathLibraryFunctions Functions FunctionDefinitions FunctionPrototypes FunctionCallStackand ActivationRecords Headers CallingFunctions:Call byValueandCallby Reference
RandomNumber Generation G i Example:AGameof Chance StorageClasses ScopeRules Recursion ExampleUsing Recursion:Fibonacci Series Recursionvs.Iteration
2
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
11/16/2009
q Divideandconquer
Constructaprogramfromsmallerpiecesor
components
Thesesmallerpiecesarecalledmodules
Eachpiecemoremanageablethantheoriginal
program
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
Functions
ModulesinC Programscombineuserdefinedfunctionswithlibraryfunctions
Cstandardlibraryhasawidevarietyoffunctions
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
11/16/2009
Fig.5.1 | Hierarchicalbossfunction/workerfunctionrelationship.
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
11/16/2009
Function
sqrt( x )
Description
square root of x
Example
sqrt( 900.0 ) is 30.0 sqrt( 9.0 ) is 3.0
exp( x )
exponential function e
log( x )
log10( x )
fabs( x )
absolute value of x
ceil( x )
rounds x to the smallest integer ceil( 9.2 ) is 10.0 not less than x ceil( -9.8 ) is -9.0
Fig.5.2 | Commonlyusedmathlibraryfunctions.(Part1of2.)
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
Function
floor( x )
Description
rounds x to the largest integer not greater than x x raised to power y (xy)
Example
floor( 9.2 ) is 9.0 floor( -9.8 ) is -10.0 pow( 2, 7 ) is 128.0 pow( 9, .5 ) is 3.0 (
pow( x, y )
fmod( x, y )
remainder of x/y as a floatingpoint number trigonometric sine of x (x in radians) trigonometric cosine of x (x in radians) trigonometric tangent of x (x in radians)
sin( x )
cos( x )
tan( x )
Fig.5.2 | Commonlyusedmathlibraryfunctions.(Part2of2.)
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
11/16/2009
Functions
Modularizeaprogram Allvariablesdefinedinsidefunctionsarelocalvariables Knownonlyinfunctiondefined Parameters Communicateinformationbetweenfunctions Localvariables Divideandconquer Manageableprogramde elopment Manageableprogramdevelopment Softwarereusability Useexistingfunctionsasbuildingblocksfornewprograms Abstraction hideinternaldetails(libraryfunctions) Avoidcoderepetition
Benefitsoffunctions
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
Function-name: any valid identifier Return-value-type: data type of the result (default int)
- void indicates that the function returns nothing
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
10
11/16/2009
Functiondefinitionformat(continued)
returnvaluetypefunctionname(parameterlist) t l t f ti ( t li t) { declarationsandstatements } Definitionsandstatements:functionbody(block) Variablescanbedefinedinsideblocks(canbenested) Functionscannotbedefinedinsideotherfunctions Returningcontrol If hi Ifnothingreturned d
return;
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Fig. 5.3: fig05_03.c Creating and using a programmer-defined function */ #include <stdio.h> int square( int y ); /* function prototype */ /* function main begins program execution */ int main( void ) { int x; /* counter */
fig05_03.c
/* loop 10 times and calculate and output square of x each time */ for ( x = 1; x <= 10; x++ ) { printf( "%d ", square( x ) ); /* function call */ } /* end for */ printf( "\n" );
21 } /* end main */ 22 23 /* square function definition returns square of parameter */ 24 int square( int y ) /* y is a copy of argument to function */ 25 { 26 return y * y; /* returns square of y as an int */ 27 28 } /* end function square */ 1 4 9 16 25 36 49 64 81 100
Function definition
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
12
11/16/2009
Functionprototype
Takesin3ints Returnsanint
Promotionrulesandconversions
Convertingtolowertypescanleadtoerrors
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Fig. 5.4: fig05_04.c Finding the maximum of three integers */ #include <stdio.h> int maximum( int x, int y, int z ); /* function prototype */ /* function main begins program execution */ int main( void ) { int number1; /* first integer */ int number2; /* second integer */ int number3; /* third integer */ printf( "Enter three integers: " ); scanf( "%d%d%d", &number1, &number2, &number3 ); /* number1, number2 and number3 are arguments to the maximum function call */
Function prototype
fig05_04.c
(1 of 2 )
Function call
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
14
11/16/2009
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/* Function maximum definition */ /* x, y and z are parameters */ int maximum( int x, int y, int z ) { int max = x; /* assume x is largest */
if ( y > max ) { /* if y is larger than max, assign y to max */ max = y; } /* end if */ if ( z > max ) { /* if z is larger than max, assign z to max */ max = z; } /* end if */ return max; /* max is largest value */
fig05_04.c
(2 of 2 )
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
15
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
16
11/16/2009
Introduction ProgramModulesinC MathLibraryFunctions Functions FunctionDefinitions FunctionPrototypes FunctionCallStackand ActivationRecords A i i R d Headers CallingFunctions:Call byValueandCallby Reference
RandomNumber Generation Example:AGameof Chance StorageClasses ScopeRules Recursion ExampleUsing Recursion:Fibonacci Series Recursionvs.Iteration
17
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
Discussion
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino,ICTSystemsEngineering.
18