Functions: Unit - 4 Session 23 Computer Programming Info - 106
Functions: Unit - 4 Session 23 Computer Programming Info - 106
FUNCTIONS
FUNCTION DEFINITION
Function definition consists of a function header that identifies the function, followed
by the body of the function containing the executable code for that function
When a function defined, space is allocated for that function in the memory.
The syntax of a function definition can be given as:
return_data_type function_name(data_type variable1, data_type variable2,..)
{
………….
statements
………….
return( variable);
}
The no. and the order of arguments in the function header must be same as that given
in function declaration statement.
Computer Programming INFO106
The function call statement invokes the function.
When a function is invoked the compiler jumps to the called function to execute the
statements that are a part of that function.
Once the called function is executed, the program control passes back to the calling
function.
Function call statement has the following syntax.
function_name(variable1, variable2, …);
Points to remember while calling the function:
Function name and the number and type of arguments in the function call must be
same as that given in the function declaration and function header of the function
definition
Names (and not the types) of variables in function declaration, function call and
header of function definition may vary
Arguments may be passed in the form of expressions to the called function. In such a
case, arguments are first evaluated and converted to the type of formal parameter
and then the body of the function gets executed.
If the return type of the function is not void, then the value returned by the called
function may be assigned to some variable as given below.
variable_name = function_name(variable1, variable2, …);
Computer Programming INFO106
#include<stdio.h>
int sum(int a, int b); // FUNCTION DECLARATION
int main()
{
int num1, num2, total = 0;
printf(“\n Enter the first number : “);
scanf(“%d”, &num1);
printf(“\n Enter the second number : “);
scanf(“%d”, &num2);
total = sum(num1, num2); // FUNCTION CALL
printf(“\n Total = %d”, total);
return 0;
}
// FUNCTION DEFNITION
int sum ( int a, int b) // FUNCTION HEADER
{ // FUNCTION BODY
return (a + b);
}
calling function after the last statement of the called function is executed.
Computer Programming INFO106
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.
Passing parameters to function
Such variables are useful when the programmer writes his own header files.
indeterminate value
Local: Accessible
within the function or
Accessible within
Accessible within the Accessible within the block in which it is
all program files
Accessibility function or block in function or block in declared
that are a part of
which it is declared which it is declared Global: Accessible
the program
within the program
in which it is declared
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));
}
Computer Programming INFO106
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.
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);
}
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
A B C
A B C
A B C
A B C A B C
A B C
A B C A B C