c-10
DESCRIPTION
TRANSCRIPT
Lecture 10Lecture 10Version 1.0Version 1.0
More on FunctionsMore on Functions
Scope RulesScope Rules
Math Library FunctionsMath Library Functions
2Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on More Knowledge on FunctionsFunctions
Actual Arguments
Formal Arguments
3Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on More Knowledge on FunctionsFunctions
Instead of using different variable Instead of using different variable names names xx, , y y and and zz, we could have , we could have used the same variable names used the same variable names aa, , b b and and cc
compiler would still treat them as compiler would still treat them as different variables since they are in different variables since they are in different functions different functions
4Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on More Knowledge on FunctionsFunctions
There is no restriction on the number of There is no restriction on the number of return return statements that may be present in a statements that may be present in a function function
return return statement need not always be statement need not always be present at the end of the called function present at the end of the called function
5Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on More Knowledge on FunctionsFunctions
All the following are valid All the following are valid return return statements statements In the last statement a garbage value is In the last statement a garbage value is
returned to the calling function since we are returned to the calling function since we are not returning any specific value not returning any specific value
Note that in this case the parentheses after Note that in this case the parentheses after return return are dropped are dropped
6Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on More Knowledge on FunctionsFunctions
A function can return only one value at a A function can return only one value at a time time
the following statements are invalid the following statements are invalid
7Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on More Knowledge on FunctionsFunctions
If the value of a formal argument is If the value of a formal argument is changed in the called function, the changed in the called function, the corresponding change does not take place corresponding change does not take place in the calling function in the calling function
8Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Knowledge on More Knowledge on FunctionsFunctions
9Rushdi Shams, Dept of CSE, KUET, Bangladesh
Calling ConventionsCalling Conventions
Calling convention indicates the order in Calling convention indicates the order in which arguments are passed to a function which arguments are passed to a function when a function call is encountered when a function call is encountered
There are two possibilities here: There are two possibilities here:
1.1. Arguments might be passed from left to Arguments might be passed from left to right. right.
2.2. Arguments might be passed from right to Arguments might be passed from right to left. left.
C language follows the second order C language follows the second order
10Rushdi Shams, Dept of CSE, KUET, Bangladesh
Calling ConventionsCalling Conventions
In this call it doesn’t matter whether the In this call it doesn’t matter whether the arguments are passed from left to right or from arguments are passed from left to right or from right to left right to left
However, in some function call the order of However, in some function call the order of passing arguments becomes an important passing arguments becomes an important consideration consideration
11Rushdi Shams, Dept of CSE, KUET, Bangladesh
Calling ConventionsCalling Conventions
12Rushdi Shams, Dept of CSE, KUET, Bangladesh
Scope RulesScope Rules
The scope of an identifier is the The scope of an identifier is the portion of the program in which an portion of the program in which an identifier is referenced identifier is referenced
When we declare a local variable in When we declare a local variable in a block, it can be referenced only in a block, it can be referenced only in that block or in blocks nested within that block or in blocks nested within that block that block
13Rushdi Shams, Dept of CSE, KUET, Bangladesh
14Rushdi Shams, Dept of CSE, KUET, Bangladesh
Math Library FunctionsMath Library Functions
Math library functions allow the Math library functions allow the programmer to perform certain common programmer to perform certain common mathematical calculations mathematical calculations
A programmer desiring to calculate and A programmer desiring to calculate and print the square root of 900.0 can write-print the square root of 900.0 can write-
15Rushdi Shams, Dept of CSE, KUET, Bangladesh
Math Library FunctionsMath Library Functions
When the statement is executed, sqrt() is When the statement is executed, sqrt() is called to calculate the square root of the called to calculate the square root of the number contained in the parenthesis (900.0) number contained in the parenthesis (900.0)
The number 900.0 is the argument of sqrt() The number 900.0 is the argument of sqrt() function function
The statement will print 30.00 The statement will print 30.00 sqrt() function takes an argument of type sqrt() function takes an argument of type
double and returns a result of type double double and returns a result of type double All math library functions return the data All math library functions return the data
type double type double
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 If c1=13.0, d=3.0 and f=4.0, then the line would print 5.00 would print 5.00
17Rushdi Shams, Dept of CSE, KUET, Bangladesh
Other Useful Math Other Useful Math FunctionsFunctions