1. Lecture 10Lecture 10
Version 1.0Version 1.0
More on FunctionsMore on Functions
Scope RulesScope Rules
Math Library FunctionsMath Library Functions
2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
Actual Arguments
Formal Arguments
3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
Instead of using different variable namesInstead of using different variable names xx,, yy
andand zz, we could have used the same variable, we could have used the same variable
namesnames aa,, bb andand cc
compiler would still treat them as differentcompiler would still treat them as different
variables since they are in different functionsvariables since they are in different functions
4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
There is no restriction on the number ofThere is no restriction on the number of returnreturn
statements that may be present in a functionstatements that may be present in a function
returnreturn statement need not always be present at the endstatement need not always be present at the end
of the called functionof the called function
5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
All the following are validAll the following are valid returnreturn statementsstatements
In the last statement a garbage value is returned to theIn the last statement a garbage value is returned to the
calling function since we are not returning any specificcalling function since we are not returning any specific
valuevalue
Note that in this case the parentheses afterNote that in this case the parentheses after returnreturn areare
droppeddropped
6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
A function can return only one value at a timeA function can return only one value at a time
the following statements are invalidthe following statements are invalid
7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
If the value of a formal argument is changed in theIf the value of a formal argument is changed in the
called function, the corresponding change does not takecalled function, the corresponding change does not take
place in the calling functionplace in the calling function
8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on FunctionsMore Knowledge on Functions
9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh
Calling ConventionsCalling Conventions
Calling convention indicates the order inCalling convention indicates the order in
which arguments are passed to a functionwhich arguments are passed to a function
when a function call is encounteredwhen a function call is encountered
There are two possibilities here:There are two possibilities here:
1.1. Arguments might be passed from left to right.Arguments might be passed from left to right.
2.2. Arguments might be passed from right to left.Arguments might be passed from right to left.
C language follows the second orderC language follows the second order
10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh
Calling ConventionsCalling Conventions
In this call it doesn’t matter whether the argumentsIn this call it doesn’t matter whether the arguments
are passed from left to right or from right to leftare passed from left to right or from right to left
However, in some function call the order of passingHowever, in some function call the order of passing
arguments becomes an important considerationarguments becomes an important consideration
12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh
Scope RulesScope Rules
The scope of an identifier is the portion of theThe scope of an identifier is the portion of the
program in which an identifier is referencedprogram in which an identifier is referenced
When we declare a local variable in a block, itWhen we declare a local variable in a block, it
can be referenced only in that block or in blockscan be referenced only in that block or in blocks
nested within that blocknested within that block
14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh
Math Library FunctionsMath Library Functions
Math library functions allow the programmer toMath library functions allow the programmer to
perform certain common mathematical calculationsperform certain common mathematical calculations
A programmer desiring to calculate and print the squareA programmer desiring to calculate and print the square
root of 900.0 can write-root of 900.0 can write-
15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh
Math Library FunctionsMath Library Functions
When the statement is executed, sqrt() is called toWhen the statement is executed, sqrt() is called to
calculate the square root of the number contained incalculate the square root of the number contained in
the parenthesis (900.0)the parenthesis (900.0)
The number 900.0 is the argument of sqrt() functionThe number 900.0 is the argument of sqrt() function
The statement will print 30.00The statement will print 30.00
sqrt() function takes an argument of type double andsqrt() function takes an argument of type double and
returns a result of type doublereturns a result of type double
All math library functions return the data type doubleAll math library functions return the data type double
16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh
Math Library FunctionsMath Library Functions
If c1=13.0, d=3.0 and f=4.0, then the line would printIf c1=13.0, d=3.0 and f=4.0, then the line would print
5.005.00
17. 17Rushdi Shams, Dept of CSE, KUET, Bangladesh
Other Useful Math FunctionsOther Useful Math Functions