Академический Документы
Профессиональный Документы
Культура Документы
Function
Part ‐ 7
Forms of return statement a return statement is used to return the result of outcome of the
function to the function which called it, so this return statement is expressed in several ways and it will
return types of values, the most common type is a 'return' and within parenthesis expression ,this
expression value is return to the calling function ,the value which is obtained as a result evaluating the
expression is return to the calling function , if this expression parenthesis is omitted , the return
statement doesn't return any value simply transfers control to the main function or the function which
called it , what is a calling function and called function has been seen in the last session understanding
the meaning for both ,here there is an example for return (a+b+c)
a is the variable b is variable c is variable
so the that is obtained result of adding this three variables is returned to the function which
called this , this is the most common type of returns statements , a function may use one or more return
statements not necessary that if we declaring the function and defining it , it have only one return
statement there may be exceptional cases where because e of some conditional expression because of
some evaluating expression , we may have to return more than one value depending upon the situation
, so that is most common use of the decision statements so here is an example where if the values a
and b evaluated if a is greater we have to return a or we have to return b, there same for the switch
case constructor also if the case 1 is true we have to return 1 if 2 is true we has to return the value 2 , so
such exceptional conditions may occur where in a single function we may have more than one return
statement , with the ultimate value is return is only one in number .
the third type of return statement is return within (&p) we have seen this & operator its is used to hold
or signify or identify the address that is held by a variable so this returns the actual address of the
variable P to the calling function , variable name is given there only as a example . the next type return
within parenthesis point that variable P , now we have still seen the pointers , pointer is nothing but it
holds the value of an address or the value of the variable that is stored at the particular memory
location , so here if I give pointer P means P is an address P represents the address the pointer variable
is used to extract value which is stored in the address so here this returns statement will return the
value which is stored at that particular address which is pointed to by P.
the last type is the return within parenthesis have Sqrt() nothing but a square root function inside that
we have an argument ,so here if you see , sqrt (r) is a function itself , so this return statement is
returning a function so how is this be evaluated , her the control from the return statement first goes to
the functions sqrt(r) , in sqrt (r) certain statements would be written we all know sqrt(r) will return the
square root of the number , so if I give square root of 81 , it will return the number 9 , so this number
will be return the value so this expression will be equal to return 9 so the number 9 will return to the
function which called this function so this is the how the function also can be passes an argument in a
return statement so these are the most commonly used types of return statements and their values
another possibility with the return statement is there , we can call a function in addition to tat we can
do type conversion also ,we all know what is type conversion , when we exchange the datatype of
particular value or variable ,so here we have return within parenthesis we have float ,within that we
having a functions square and passing the value so how will this evaluated is first the function square of
2.8 will be executed so square of the function 2.8 will be called again by itself , so will get the square of
the number , the number should be explicitly return the float data type that is it should not take just
the integer value and return to the function. We want answer in the float data type with the exact
decimal and fraction. so that is the reason we are explicit converting that square of 2.8 into a float and
here returning the value. So also is the permissible in the c language. See all these are all that is
permitted in c language. Depending upon our application and our need. We can modified these
statements and used only those kind of expressions which are required for us which is not nesscesary
that we have to use that we have to compulsorily in our programs. it is all available it is up to us to use
them up to our benefits in the program, then we have something called void functions its not necessary
that every functions should return some value. Sometimes we may print the value of the result of the
function in the function itself. In such cases the function need not have the return statement so there
we explicitly mentioned that those functions are called void that is that they won't retunes any value.
They return simply null. So such functions are called void functions .so these are the different types of
return statements available in C. Next we are moving over to argument passing method. Argument
passing in the sense we know that their two types of arguments actual arguments and formal
arguments. The variables that follows a function call or function definition are called the arguments. So
the variable in which the function called actual arguments. The variables in the function definition are
called formal arguments. So there are two ways in which we can pass these arguments one is by using
call by value, and other is using call by reference. Reference is just another name for address. So we will
see in detail how these functions are defined or how these methods are defined. Call by value, here the
values of the actual arguments are passed to the function. So, this is the normal way of passing an
argument. You have a function is sum and you pass into the integer a, integer b as the arguments. Any
change which is made to the formal to the parameters does not affect the actual parameters. So here if
you are calling in the functions sum of a, b and you are defining the functions sum of c,d. the case value
will be copied to c. And these values will be copied to d. whatever changes you want to make, you make
inside the functions and you print he value their itself. No change that is made to the formal parameters
will be seen in the actual parameters. You will understand this better when we seeing the example
programs. what you have to remember here is in call by value only a copy of the original value is send to
the function which is called no change that is made in the function has any affect on the actual
arguments where is in call by reference we don't pass the value but here we pass the address or the
reference of the actual parameters to the call function. so, here any change which is made to the formal
parameters is also affected in the actual arguments because here we are manipulating the address of
the variable and not its value so automatically when the address is affected any value which is stored in
the particular address will be automatically changed so here the modifications or the changes that are
made are permanent. You will see the affects in both the called function and the calling function. Where
is in call by value only a copy of the variable is send to the called function. You can make any changes
you want over there it will not be seen in the original value. So we will see how these arguments work.
So this is the example for the call by value. In this program what we are doing is we are actually
swapping the values of the two variables, say I am entering two values five and seven for x and y. I want
the values to change to seven and five for the same variables. so here one method and passing the call
by value and the another method by call by reference so according to the definition when I use the call
by value that x and y values such be changed only in the called function which effect should not seen in
the main function. Where in the call by reference the effect should be permanent. It should be visible in
called function as well as the call by function. so we will see how this is executed in c. so here I am
declaring the variable and I am declaring the function also the name of my function is change and I am
having two integer variables and I am printing enter x and y values and I am scanning the values three
and four for x and y. so now x holds three and y holds four. Next I am calling this change function that is
the function call. So what happens when the compiler sees this statement immediately the control will
go to this change of x, y. so the control will goes to this particular function and it will check whether the
two integer value is send over here. Their are two integer values declare over here. So the x will be
copied to a and the y will be copied to b. So, now x is having the value three and y is having the value
four. a will become three and y will became four. So, here I am having this integer k which is a local
variable. I want to interchange the value and this is the logic for interchanging the value a is send to k, b
is send to a and again the value of I am coping into b. so on this way if you see the value a is changed to
four and the value b is changed to three. So the values have been interchanged over here. And here if
you see I am printing values in the change function itself. I am not exactly returning any function. So in
change when I am printing the value the value of x is four and the value of y is three. So here if you see
the value of b and a have been in swapped. That is that is been interchanged. so these values I am
printing here in the change function itself because the ultimate aim of my program is to indicate that in
the change function the values are swapped but in the main function the values remain the same so that
the printf statement I am giving in the change function itself so that the output will appear like this x=4
then y=3. Because that's why I am printing it here x=%d y=%d and then I am printing the values a and b. I
am using a and b here because here the variables a and b are only declared .so what happens after the
change function is printed control has to get back to the main function so there is no return statement
here to explicitly return the control so only when this closing brace is counted the compiler understands
that ok now the function is over I have to get back to the function which called it. so in this case as soon
as the closing brace is encounter come to this function so, here if you see I am again having a printf
statement and here I am printing in x=%d and y=%d x and y. so, here the output will appear like this x=3
and y=4.though the values have been slapped in the function change and though the function is called
over here what ever the changes who are made in the function change is visible only to that function. So
that is the reason where the values are printed their itself. There is no modification or effect on the
actual arguments. So that is the difference between these two x=3 and y=4. And here I am closing the
main function statement with return 0 statement. It is not the must we can have that statement or close
that statement. There is no difference. And this is how call by value program is executed. You simply
have to enter into the values and call the function is usual. And few of the values go to the calling
function. What ever changes made over there remains over there. It does not have any effect on these
mean actual arguments. So next we move over to the call by reference program. so the same program I
am going to execute but with call by reference that is I am going to pass the address of the variable now
here if you will see the whatever the change made in the calling function will have the effects on the
called function also to here again I am decelerating again the main function integer x, y and the here if
you see the change function it is int *, int* that means I am passing the pointer variable now the star
and the # indicates pointer. So in change this is the first difference between call by value and call by
reference. In the function declaration itself where we passed only int and int where us here we are
passing pointer variables as the arguments. Now what is the pointer? It is something that holds the
value.
Of an address so, from that itself it is clear that here passing the address of the variable to access its
value we have to use the pointer variable. So this is the first difference between the call by value and
the call by reference. then we are entering the two values x and y. here scanning the values and giving
the same value three and four and I am executing this function call change of x and y. and again if you
are see if their is a difference &x and &y name passing. Here ampersand is nothing but the address
operator mean that here I am passing the address of x and the address of yes here if you the variables
three and four have been return like this. And this is the address at which the first variable x is held and
this is the address at which the variable y is held. So, when the change function is executed we will that
these values are swapped. So, how these values are swapped what happen in the change function. We
will seeing about the examples of call by reference and how the values are swapped and what the
difference between call by value is and call by reference. so, in the call by reference the first difference
was in the declaration of the change function where we are passing the pointer variables and while
calling making the function called and we are passing the address of the two variables rather than the
simply the value. That is the second difference. So, what are we actually passing over here. Here if you
seeing the change function that integer pointer variable an integer pointer variable b. so this is
equivalent to x and y which is passed over there. So, there we are passing the address x and address y.
and here we are passing the pointer variable a and pointer variable b. so, the address that is passed over
there to excess its value need these two pointer variables. so this pointer a swill take the value which is
stored at the address x and pointer b will take the value which is stored at the address y. so that is why
we are using this asterisk symbol over here and it also called as pointer symbol. So, here I have to
declare again a pointer variable I cannot simply use a variable because my values are all in pointer
variables. So pointer a gives the same operation is done again the only thing is the introduction of the
pointer variable.
So the value of 'an' I am assigning to k. The value of b to a and then the value of a to b. so that the values
is slapped over here. So, I am printing the value again in change x = the value held at he address a. y =
the value held at the address b so here you can see that the value has been swapped. And again I am
not returning any value over here only when the compiler sees this closing brace it will identifies the
functions execution is over the work is done so we will return to the calling function. so, the control is
return to the calling function all the changes that we are made in that change function will effectively do
this over here that is why here in the print function if you see mean x=%d, y=%d the same x and y I am
printing. The values appear changed. The values here are three and four. But, the values that are there
in the main function are four and three. The values have been swapped. So in this diagram also I am
showing how the values are been swapped. Three has comes to their four was and four has comes to
the place where the three was. How is it possible that is because 65550 and 65552 with an example of
the memory location. Here both of them are integer variables so it occupies only 2 bytes. So, the first
rebel is stored at 0 and the second variable is stored at place 2.so, here I am passing this address only
here in the change function. So, the change of ampersand exceeds that the address would have been
passed change of ampersand with the address would have been passed. And as soon as the function call
is been indicated when control passes over here. Here, a will be holding the address b will hold the
address. So to excess the value only using the symbol *. So 65550 is the address value their was 3. 65552
is the address the value is 4.so that is why using the values to swapping this values again. So this is the
difference between call by value and call by reference. Call by value if you have any ampersand symbol
pointer symbol simply passing the copy of the value to the calling function to the function which is called
and your are swapping it. All that happens ends over there. In the main function there is no change.
where is in call by reference since we are passing the address and we are using the pointer variable to
access the value at that address any change that is made to that value at that particular address will be
permanent because the address is the permanent location for a variable.
Any change that is made to that value at that particular address. Would be permanent. Because the
address is the permanent location of a variable... that is the reason whatever changes made in the call
function has its effect on the calling function. So, these two example programs it is been indicated that
changes which are made by call by reference are permanent. Where in call by reference temporary. So
next we are move on into the use on function as an argument. Here always sees an example of
arguments. Arguments may be values or variables or addresses of the variables. So even we are using
function we can have the value or a variable or address of the variable. We will see in pointer p &P and
so on. A function itself also is passed as an argument so when a function which is passes their argument
how does the complier executed? So this is the program to pass a user defined function as an augment.
In functions their are two types library built in and the user defined is what the user is been defined
according to its convenient. So, we will see how the program is been executed. How the compiler
executes such a program? He start with the main function then we are declaring two variables one is
two and x. and, here if you see x = double itself is a function within parenthesis the square I function and
the y is an argument so we are having these many arguments over here double square is a first
argument y is the inner argument for that square. So, what happens when the compiler sees such a
statement this is called the inner functions squarely is called the inner function double is called the
outer function. So, the inner function is executed first. so, let us see the inner function means the square
of k is executed first so there we saw that it was square of y and y holds the value of 2.so instead of k it
holds the value 2 and what happens when the square function we are simply returning the value 2 into 2
so, return k cross k it will returning the value of the expression so 2 * 2 =4.when this return statement is
encounter by the compiler obviously the control will return to the main function now I am control
returns here the square of y is having the value 4 that is only been return. So, next it will go to the
double of 4. When the double of 4 is compiled by the compiler. It will switch control to this function
double. Double of m that means that m now holding the value of 4. But what happens in the double
function simply retuning m into 2 who it is equal to 4*2 the value is 8.
when the compiler sees this return statement it will again return to the main function so that the return
8 is passed on to the collective variable x now the ultimate result of the operation is x=8. So here we are
printing the value of the 8. Printf %d and %f and that are what ever the value is send earlier and the
result of execution. First the value is sending to as y so that I am printing that 2 and the x is the final
value and I will printing it as 8. And then the function is closed. So this is how the compiler executes the
program when the function is passed as an argument. Next we see how a function is used with
operators. so here we are going to do addition and subtraction so again we are having the main function
I am decelerating a function integer name of the function is input and I am passing an integer variable
and I am declaring again a square function and an integer variable and I am here making the function
call here again you see x=square is the first function input of x is the inner function. And inside this
parenthesis itself I am performing an evaluation with operators. One minus and one plus one so how the
compiler does execute the program without any confusion. Always the result of the functions will be
evaluated first so as soon as the compiler first move to the inner function. So, input of x, so x is nothing
but a value which we have to include so, input of x will go to the function. so, what happen to the input
of x. x will be copied to the variable a. and here I am asking the user to enter into the this x and I am
scanning the value &a. and I am giving the value 10 to x and then returning k. and why this x and k is
used and interchanging manner because in this function only I am getting the value for the x. I am not
using the same variable x over here instead of that I am using a local variable k. so the k and k are the
just same copies of the same variable. X is actual argument and k is a formal argument and when ever I
am scanning a value I am using the variable k. only when I am printing it I am using the value x. so, that
the user will understand because here we are entering the value of x only this the user can see. He does
not know about the programming methodology. So that this is for our convenient that we are using a
different variable also now the value held by x is then and I am returning the strength to the main
function. As soon as the return statement is seen the compiler will move to the main function. Next it
have to do it has to evaluate this expression within the parenthesis. So 1‐10 because the 10 is the value
that is written +1. So 1‐10 proceedings of the operative is minus and plus have the same precedence so
the operation will start from the left hand side. So, we are starting from the left hand side 1‐10 is ‐9.‐
9+1=‐8. So the square of ‐8. That is how the values will be understood by the compiler so that is how the
value is executed. So the next compiler will execute this outer function square of ‐8 so immediately the
control will move to this square function m will be ‐‐8. And what I am doing over here and I am returning
power of that m to that mean ‐8 power 2. So ‐8 whole square is equal to 64. This power is the
predefined function is in the math.h file. So it returns ‐8 the whole square. So it will return 64. So as
soon as his return statement is seen compiler will return control over here. Now x is the collecting
variable. So that the 64 will be assigned to expand I am printing the square is 64. So that is the final
result that will be getting .so, these are the different ways in which the function can be used in a
program. So this is with binary operators. We will see the example of this function with and another
type of operator unary operators. We know there are two types of operators. With unary operators how
a function is used. So again here we are having the function declarations the same function is I am going
to use.
Only thing is instead of binary operator I am using the unary operator. Again it is the same case we have
x is equal to square of ++ y=input of x here we are having this ++ symbol which is unary incrementing
and here I am having y = input of x. in the previous program with the binary operators I did not have the
collective variable here. Why because it was a binary operation whatever the result was obtained it
would automatically be evaluated just like that. but, when we are using unary operators we need to
have a collective variable over here so that the point has to noted so what happens is the compiler first
executes the inner view or the inner function. So move to the inner function we have input into of a so
here I can receive thing I am entering the value of the x I am giving the same value 10 and when I am
scanning the and using the value a and I am returning the value of a. so here the value 10 will be return
to the main function. So, now this 10 is assigned to the collective variable yes now the y holds the value
10. And what I doing over here is ++y so ++10. So, which will be equal to 10+1? so that will be equivalent
to 11.so next the compiler will see the outer functions square of 11 so when we see its function call it
will next pass control over the calling function. So the square of into n. we saw that it was equal to
square of y. y was holding the value 11. so now square of 11 inside the function we are again calling the
power function m,2 that means it will be equivalent to 11 square which is nothing but 121. So that value
is return to the main function. So as soon as the return statement is encounter though it is return back
to the original statement so that the function is now over. That 121 will be assigned to the collective
variable is x. and I am printing the value square x. so, this is how the function can be used as unary as
well as the binary operators. When we are using binary operator automatically the value that was return
was obtain add done subtracted. Here because we are using unary operators we have the collecting the
variable and we have to print the collective value as a final result. Finally we are having recursion.
Recursion is a special case of where a function is called repeatedly by the itself. There are two types of
recursion. Direct recursion which calls itself when the condition is true. There is still a particular system
excessive. It keeps calling the same function. In direct recursion is there a function calls another function
and the called function gets back to the calling function. So, it’s like really from one function to another
then to and to the first function. So this is just a small program for the recursion. Here I am including the
process’s file because I am having the exit of 0 programs. I am declaring two variables and adding the
values of the variable in the recursive manner. Is 0 and s is 1. So that the 0+1`+2+3+4+5.and I am
printing the value each time. As soon as x reaches the 5 I am using the exit function and getting out of
the main function and because I am using exit only I am using process.h. And here I am doing main (++x).
So, this is the actual place where x gets incremented. That is how x is time gets incremented. That is how
x gets incremented becomes 1 and 2 and 3and 4 and 5. So this is the example of how this main function
calls the function itself. So, the main keeps calling itself again and again. And this is the small examples
for the recursion. so the possible questions that would be asked in this section are the methods by
which arguments can be passed to a function, call by value and call by reference, the word the recursion
is where , function called itself repetitively, direct and indirect of their types, the operators that we are
using along with such as unary, binary and all types of additions, subtraction, multiplication, division and
module.