CH 10 Functions
CH 10 Functions
FUNCTIONS
main() func1()
{ {
………….. Statement Block;
………….. }
func1();
…………
………..
© return
Oxford University Press 2012. All0;rights reserved.
}
INTRODUCTION CONTD….
● It is not necessary that the main() can call only one function, it can call as many
functions as it wants and as many times as it wants. For example, a function call placed
within a for loop, while loop or do-while loop may call the same function multiple times
until the condition holds true.
● It is not that only the main() can call another functions. Any function can call any other
function. In the fig. one function calls another, and the other function in turn calls some
other function.
● A function, f that uses another function g, is known as the calling function and g is known as the
called function.
● The inputs that the function takes are known as arguments
● When a called function returns some result back to the calling function, it is said to return that
result.
● The calling function may or may not pass parameters to the called function. If the called function
accepts arguments, the calling function will pass parameters, else not.
● Main() is the function that is called by the operating system and therefore, it is supposed to
return the result of its processing to the operating system.
● The return statement is used to terminate the execution of a function and return control to the
calling function. When the return statement is encountered, the program execution resumes in
the calling function at the point immediately following the function call.
● Programming Tip: It is an error to use a return statement in a function that has void as its
return type.
● A return statement may or may not return a value to the calling function. The syntax of return
statement can be given as
return <expression> ;
● Here expression is placed in between angular brackets because specifying an expression is
optional. The value of expression, if present, is returned to the calling function. However, in case
expression is omitted, the return value of the function is undefined.
● Programmer may or may not place the expression within parentheses.
● By default, the return type of a function is int.
● For functions that has no return statement, the control automatically returns to the calling
● There are two ways in which arguments or parameters can be passed to the called function.
● Call by value in which values of the variables are passed by the calling function to the called
function.
● Call by reference in which address of the variables are passed by the calling function to the
called function.
Variable Scope
PROGRAM SCOPE
●If
you want that functions should be able to access some variables which are not passed to
them as arguments, then declare those variables outside any function blocks. Such variables
are commonly known as global variables. Hence, global variables are those variables that can
be accessed from any point in the program.
#include<stdio.h>
int x = 10;
void print();
int main()
{ printf("\n The value of x in the main() = %d", x);
int x = 2;
printf("\n The value of local variable x in the main() = %d", x);
print();
}
void print()
{ printf("\n The value of x in the print() = %d", x);
}
● When a global variable is accessible until the end of the file, the variable is said to have file
scope.
● To allow a variable to have file scope, declare that variable with the static keyword before
specifying its data type, like this:
static int x = 10;
● A global static variable can be used any where from the file in which it is declared but it is not
accessible by any other files.
● Such variables are useful when the programmer writes his own header files.
● The storage class of a variable defines the scope (visibility) and life time of variables and/or
functions declared within a C Program. In addition to this, the storage class gives the following
information about the variable or the function.
● It is used to determine the part of memory where storage space will be allocated for that
variable or function (whether the variable/function will be stored in a register or in RAM)
● it specifies how long the storage allocation will continue to exist for that function or variable.
● It specifies the scope of the variable or function. That is, the part of the C program in which the
variable name is visible, or accessible.
● It specifies whether the variable or function has internal, external, or no linkage
● It specifies whether the variable will be automatically initialized to zero or to any indeterminate
value
● A recursive function is a function that calls itself to solve a smaller version of its task until a final
call is made which does not require a call to itself.
● Every recursive solution has two major cases, they are
base case, in which the problem is simple enough to be solved directly without making any
further calls to the same function
recursive case, in which first the problem at hand is divided into simpler sub parts. Second the
function calls itself but with sub parts of the problem obtained in the first step. Third, the result is
obtained by combining the solutions of simpler sub-parts.
● Therefore, recursion is defining large and complex problems in terms of a smaller and more
easily solvable problem. In recursive function, complicated problem is defined in terms of
simpler problems and the simplest problem is given explicitly.
FIB(6) FIB(5)
Recursion
DIRECT RECURSION
A function is said to be directly recursive if it explicitly calls itself. For example, consider the function
given below.
int Func( int n)
{
if(n==0)
retrun n;
return (Func(n-1));
} © Oxford University Press 2012. All rights reserved.
INDIRECT RECURSION
A function is said to be indirectly recursive if it contains a call to another function which
ultimately calls it. Look at the functions given below. These two functions are indirectly recursive
as they both call each other.
int Func1(int n) int Func2(int x)
{ {
if(n==0) return Func1(x-1);
return n; }
return Func2(n);
}
TAIL RECURSION
● A recursive function is said to be tail recursive if no operations are pending to be performed when
the recursive function returns to its caller.
●That is, when the called function returns, the returned value is immediately returned from the
calling function.
●Tail recursive functions are highly desirable because they are much more efficient to use as in the
case, the amount of information that has to be stored on the system stack is independent of the
number of recursive calls.
int Fact(n) int Fact1(int n, int res)
{ {
return Fact1(n, 1); if (n==1)
} return res;
return Fact1(n-1, n*res);
}
© Oxford University Press 2012. All rights reserved.
LINEAR AND TREE RECURSION
● Recursive functions can also be characterized depending on the way in which the recursion
grows- in a linear fashion or forming a tree structure.
● In simple words, a recursive function is said to be linearly recursive when no pending operation
involves another recursive call to the function. For example, the factorial function is linearly
recursive as the pending operation involves only multiplication to be performed and does not
involve another call to Fact.
● On the contrary, a recursive function is said to be tree recursive (or non-linearly recursive) if the
pending operation makes another recursive call to the function. For example, the Fibonacci
function Fib in which the pending operations recursively calls the Fib function.
● Tower of Hanoi is one of the main applications of a recursion. It says, "if you can solve
n-1 cases, then you can easily solve the nth case?"
A B C
A B C
If there is only one ring, then simply move the ring from source to the destination
A B C
A B C A B C
If there are two rings, then first move ring 1 to the spare pole
and then move ring 2 from source to the destination. Finally
move ring 1 from the source to the destination
A B C © Oxford University Press 2012. All rights reserved.
● Consider the working with three rings.
A B C
A B C
A B C
A B C A B C
A B C
A B C A B C