SlideShare a Scribd company logo
INTRO DUCTION TO FUNCTIONS
TYPES OF FUNCTIONS
ELEM ENTS O F USER DEFINED FUNCTIO NS
TYPES O N THE BA SIS O F A RG UM ENTS A ND
RETURN VA LUES
METHODS OF CA LLING A FUNCTION
Functions
Introduction to
Function
Block of statements that perform the particular task.
Enables modular programming.
Main() is the driver function. Has pre defined
prototype.
Same function can be accessed from different places
within a program.
Once a function execution is completed , control
return to the place from where the function was
called.
Advantag
es
Modular Programming
Length of source program can be reduced
Easy to locate and isolate faulty function
Functions can be used by other program’s
Types of
Functions
Library (Built In) Functions:
 They are written in the header files.
 To use them appropriate header files should be included.
Header Files Functions Defined
stdio.h Printf(), scanf(), getchar(), putchar(),
gets(), puts(), fopen(), fclose()
conio.h Clrscr(), getch()
Ctype.h Toupper(), tolower(), isalpha()
Math.h Pow(), sqrt(), cos(), log()
Stdlib.h Rand(), exit()
String.h Strlen(), strcpy(), strupr()
User Defined Functions
 Written by the user at the time of programming.
Elements of User defined
functions
Function Prototype Function Call
Function arguments and parameters
Function Definitions
Function
prototype
 It specify the type of value that is to be return
from the function and that is to be passed to the
function.
 It is defined in the beginning before the function
call is made.
 Syntax:
 return-type name-of-function(list of arguments);
 Example
 Void sum(int, int);
Function
Call
A function can be called by specifying name and list
of arguments enclosed in parenthesis and separated
by comma.
If there is no arguments empty parenthesis are place
after function name.
If function return a value, function call is written as
assignment statement as:
A=sum(x,y);
Function arguments and
parameters
Arguments are also called actual parameters.
Arguments are written within parenthesis at the time
of function call.
Parameters are also called formal parameters.
These are written within parenthesis at the time of
function definition.
Function
Definition
It is the independent program module.
It is written to specify the particular task that is to be
performed by the function.
The first line of the function is called function
declarator and rest line inside { } is called function
body
Functions in c
Return
statement
It is the last statement of the function that return
certain values.
It return certain types of values to the place from
where the function was invoked.
Syntax:
return(variable-name or constant);
 #include <stdio.h>
 int addition(int num1, int num2)
 {
 int sum;
 /* Arguments are used here*/
 sum = num1+num2;

 /* Function return type is integer so we are returning
 * an integer value, the sum of the passed numbers.
 */
 return sum;
 }

 int main()
 {
 int var1, var2;
 printf("Enter number 1: ");
 scanf("%d",&var1);
 printf("Enter number 2: ");
 scanf("%d",&var2);

 /* Calling the function here, the function return type
 * is integer so we need an integer variable to hold the
 * returned value of this function.
 */
 int res = addition(var1, var2);
 printf ("Output: %d", res);

 return 0;
Categories of
function
Function with no arguments and no return
Function with arguments but no return
Function with no arguments and return
Function with arguments and return
Function with no argument and no
return
#include<stdio.h>
int sum(); // function prototype
int main()
{
sum(); //function call
return 0;
}
int sum() //function defination
{
int x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
printf("sum is%d :", x+y);
}
Function with argument and no
return
#include<stdio.h>
int sum(int a, int b); // function prototype
int main()
{
int x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
sum(x,y); //function call
return 0;
}
int sum(int a, int b) //function defination
{
printf("sum is%d :", a+b);
}
Function with no argument and
return
#include<stdio.h>
int sum(); // function prototype
int main()
{
int a;
a=sum();
printf("the sum of the numbers you entered is
%d",a);
return 0;
}
int sum() //function defination
{
int x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
return(x+y);
}
Function with argument and
return
#include<stdio.h>
int sum(int,int); // function prototype
int main()
{
int a,x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
a=sum(x,y);
printf("the sum of the numbers you entered is %d",a);
return 0;
}
int sum(int x, int y) //function defination
{
return(x+y);
}
Methods of calling function
Call by value
Call by reference
Call by value
 Copies the value of actual parameters into formal
parameters.
 During execution whatever changes are made in
the formal parameters are not reflected back in
the actual parameters.
Functions in c
Call by Reference
Reference(address) of the original variable is passed.
Function does not create its own copy, it refers to the
original values by reference.
Functions works with the original data and changes
are made in the original data.
#include<stdio.h>
int swap(int*,int*); // function prototype
int main()
{
int x,y;
printf("enter two numbers:");
scanf("%d%d", &x,&y);
printf("x=%d and y=%d before calling functionn",x,y);
swap(&x,&y);
printf("x=%d and y=%d after calling function",x,y);
return 0;
}
int swap(int* x, int* y) //function defination
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
Scope of variables
• Block scope
• Function scope
• Program scope
• File scope
Block scope
 A local scope or block is collective program statements put in and
declared within a function or block (a specific region enclosed with
curly braces) and variables lying inside such blocks are termed as
local variables.
 #include <stdio.h>
 int main ()
 {
 /* local variable definition and initialization */ int x,y,z;
 /* actual initialization */ x = 20;
 y = 30;
 z = x + y;
 printf ("value of x = %d, y = %d and z = %dn", x, y, z);
 return 0;
 }

Function Scope:
 A Function scope begins at the opening of the
function and ends with the closing of it. Function
scope is applicable to labels only. A label declared
is used as a target to go to the statement and both
goto and label statement must be in the same
function.
Program scope:
 After defining a local variable, the system or the compiler won't be
initializing any value to it. You have to initialize it by yourself. It is
considered good programming practice to initialize variables before
using. Whereas in contrast, global variables get initialized
automatically by the compiler as and when defined. Here's how
based on datatype; global variables are defined.
 Life time
 Place of declaration
 Name conflict

// C program to illustrate the global scope
#include <stdio.h>
// Global variable
int global = 5;
// global variable accessed from within a function
void display()
{
printf("%dn", global);
}
// main function
int main()
{
printf("Before change within main: ");
display();
// changing value of global variable from main function
printf("After change within main: ");
global = 10;
display();
}
File Scope:
 These variables are usually declared outside of all of
the functions and blocks, at the top of the program and
can be accessed from any portion of the program.
 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.
 Static int x =10;

Variable storage classes
 automatic
 register
 static
 external
Storage class datatype vname:
Auto Storage Class
 auto comes by default with all local variables as its
storage class. The keyword auto is used to define
this storage class explicitly
 Syntax: auto datatype vname;
 int roll; // contains auto by default
 is the same as:
 auto int roll;
register Storage Class
 This storage class is implemented for classifying local
variables whose value needs to be saved in a CPU
register in place of RAM (Random Access Memory). This
is implemented when you want your variable the
maximum size equivalent to the size of the register. It
uses the keyword register.
 Syntax: register datatype vname;
 register int counter;
 Register variables are used when implementing looping
in counter variables to make program execution fast.
Register variables work faster than variables stored in
RAM (primary memory).
 &
static storage class
 This storage class uses static variables that are used
popularly for writing programs in C language. Static
variables preserve the value of a variable even when
the scope limit exceeds. Static storage class has its
scope local to the function in which it is defined.
 On the other hand, global static variables can be accessed
in any part of your program. The default value assigned is
'0' by the C compiler. The keyword used to define this
storage class is static.
 Example:
 static int var = 6
extern Storage class
 The extern storage class is used to feature a variable to be used from
within different blocks of the same program. Mainly, a value is set to
that variable which is in a different block or function and can be
overwritten or altered from within another block as well.
 #include <stdio.h>
 int val;
 extern void funcExtern();
 main()
 {
 val = 10;
 funcExtern();
 }
Recursion
 Recursion can be defined as the technique of
replicating or doing again an activity in a self-similar
way calling itself again and again, and the process
continues till specific condition reaches.
 Every recursive solution has two major cases
• Base case
• Recursive case
Recursion and its types
 Direct recursion
 Indirect recursion
 Tail recursion
 Linear and tree recursion
 Recursive functions can be classified on the
basis of :
a.) If the functions call itself directly or indirectly –
Direct / Indirect
b.) If an operation is pending at each recursive
call – Tail Recursive/ Not
c.) based on the structure of the function calling
pattern – Linear / Tree
 Direct Recursion:
 If a function explicitly calls itself it is called directly
recursive.
 When the method invokes itself it is direct.
int testfunc(int num)
{ if (num == 0)
return 0;
else
return (testfunc(num - 1));}
 Here, the function ‘testfunc’ calls itself for all positive
values of num.
Indirect Recursion
 This occurs when the function invokes other method
which again causes the original function to be called
again.
 If a method ‘X’ , calls method ‘Y’, which calls method
‘Z’ which again leads to ‘X’ being invoked is called
indirect recursive or mutually recursive as well.
int testfunc1(int num)
{ if (num == 0)
return 0;
else
return (testfunc2(num - 1));
}
int testfunc2(int num2)
{ return testfunc1(num2 - 1);}
Tail / Bottom Recursion
 A function is said to be tail-recursive, if no
operations are pending when the recursive
function returns to its caller.
 Such functions, immediately return the return
value from the calling function.
 It is an efficient method as compared to others, as
the stack space required is less and even
compute overhead will get reduced.
 Recollect the previously discussed example,
factorial of a number. We had written it in non tail
recursive way, as after call operation is still
pending.
Linear and Tree Recursion
 Depending on the structure the recursive function
calls take, or grows it can be either linear or non
linear.
It is linearly recursive when, the pending operations
do not involve another recursive call to the function.
Our Factorial recursive function is linearly recursive
as it only involves multiplying the returned values
and no further calls to function.
Tree recursion is when, pending operations involve
another recursive call to function.
For example – Fibonacci series, the pending
operations have recursive call to the fib() recursive
function to compute the results.

More Related Content

PPTX
Function in C program
PPTX
Control Structures in C
PPTX
Functions in C
PDF
Function lecture
PPT
User defined functions in C programmig
PPTX
Function in c
PDF
Functions
PPT
Prsentation on functions
Function in C program
Control Structures in C
Functions in C
Function lecture
User defined functions in C programmig
Function in c
Functions
Prsentation on functions

What's hot (19)

PPTX
Functions in c language
PPTX
Presentation on function
PPSX
Function in c
PPTX
Storage classes in C
PDF
Lecture20 user definedfunctions.ppt
PPTX
Function in c program
PPT
Recursion in c
PPTX
ODP
Function
PPT
User Defined Functions
PDF
Function in C
DOC
Storage classess of C progamming
PPT
Functions in c
DOC
Unit 8
PDF
Programming Fundamentals Functions in C and types
PPTX
C function
PPTX
PPTX
User defined functions
Functions in c language
Presentation on function
Function in c
Storage classes in C
Lecture20 user definedfunctions.ppt
Function in c program
Recursion in c
Function
User Defined Functions
Function in C
Storage classess of C progamming
Functions in c
Unit 8
Programming Fundamentals Functions in C and types
C function
User defined functions
Ad

Similar to Functions in c (20)

PPTX
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
PPTX
CH.4FUNCTIONS IN C (1).pptx
PDF
Unit iii
PPTX
Functions in c
PPTX
Detailed concept of function in c programming
PDF
functionsinc-130108032745-phpapp01.pdf
PPTX
function in c
PDF
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 3.pdf
PPTX
unit_2.pptx
PPT
function_v1.ppt
PPT
function_v1.ppt
PDF
PSPC-UNIT-4.pdf
PPT
C programming is a powerful, general-purpose language used for developing ope...
PPT
PPT
function_v1fgdfdf5645ythyth6ythythgbg.ppt
PPTX
PPT
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
DOC
Unit 4 (1)
PPTX
C concepts and programming examples for beginners
PPTX
C language 3
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C (1).pptx
Unit iii
Functions in c
Detailed concept of function in c programming
functionsinc-130108032745-phpapp01.pdf
function in c
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 3.pdf
unit_2.pptx
function_v1.ppt
function_v1.ppt
PSPC-UNIT-4.pdf
C programming is a powerful, general-purpose language used for developing ope...
function_v1fgdfdf5645ythyth6ythythgbg.ppt
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
Unit 4 (1)
C concepts and programming examples for beginners
C language 3
Ad

Recently uploaded (20)

PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
composite construction of structures.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
PPT on Performance Review to get promotions
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
DOCX
573137875-Attendance-Management-System-original
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Well-logging-methods_new................
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Welding lecture in detail for understanding
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CYBER-CRIMES AND SECURITY A guide to understanding
composite construction of structures.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPT on Performance Review to get promotions
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
CH1 Production IntroductoryConcepts.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Lesson 3_Tessellation.pptx finite Mathematics
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
573137875-Attendance-Management-System-original
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Well-logging-methods_new................
Structs to JSON How Go Powers REST APIs.pdf
Foundation to blockchain - A guide to Blockchain Tech
Welding lecture in detail for understanding
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf

Functions in c

  • 1. INTRO DUCTION TO FUNCTIONS TYPES OF FUNCTIONS ELEM ENTS O F USER DEFINED FUNCTIO NS TYPES O N THE BA SIS O F A RG UM ENTS A ND RETURN VA LUES METHODS OF CA LLING A FUNCTION Functions
  • 2. Introduction to Function Block of statements that perform the particular task. Enables modular programming. Main() is the driver function. Has pre defined prototype. Same function can be accessed from different places within a program. Once a function execution is completed , control return to the place from where the function was called.
  • 3. Advantag es Modular Programming Length of source program can be reduced Easy to locate and isolate faulty function Functions can be used by other program’s
  • 4. Types of Functions Library (Built In) Functions:  They are written in the header files.  To use them appropriate header files should be included. Header Files Functions Defined stdio.h Printf(), scanf(), getchar(), putchar(), gets(), puts(), fopen(), fclose() conio.h Clrscr(), getch() Ctype.h Toupper(), tolower(), isalpha() Math.h Pow(), sqrt(), cos(), log() Stdlib.h Rand(), exit() String.h Strlen(), strcpy(), strupr()
  • 5. User Defined Functions  Written by the user at the time of programming.
  • 6. Elements of User defined functions Function Prototype Function Call Function arguments and parameters Function Definitions
  • 7. Function prototype  It specify the type of value that is to be return from the function and that is to be passed to the function.  It is defined in the beginning before the function call is made.  Syntax:  return-type name-of-function(list of arguments);  Example  Void sum(int, int);
  • 8. Function Call A function can be called by specifying name and list of arguments enclosed in parenthesis and separated by comma. If there is no arguments empty parenthesis are place after function name. If function return a value, function call is written as assignment statement as: A=sum(x,y);
  • 9. Function arguments and parameters Arguments are also called actual parameters. Arguments are written within parenthesis at the time of function call. Parameters are also called formal parameters. These are written within parenthesis at the time of function definition.
  • 10. Function Definition It is the independent program module. It is written to specify the particular task that is to be performed by the function. The first line of the function is called function declarator and rest line inside { } is called function body
  • 12. Return statement It is the last statement of the function that return certain values. It return certain types of values to the place from where the function was invoked. Syntax: return(variable-name or constant);
  • 13.  #include <stdio.h>  int addition(int num1, int num2)  {  int sum;  /* Arguments are used here*/  sum = num1+num2;   /* Function return type is integer so we are returning  * an integer value, the sum of the passed numbers.  */  return sum;  }   int main()  {  int var1, var2;  printf("Enter number 1: ");  scanf("%d",&var1);  printf("Enter number 2: ");  scanf("%d",&var2);   /* Calling the function here, the function return type  * is integer so we need an integer variable to hold the  * returned value of this function.  */  int res = addition(var1, var2);  printf ("Output: %d", res);   return 0;
  • 14. Categories of function Function with no arguments and no return Function with arguments but no return Function with no arguments and return Function with arguments and return
  • 15. Function with no argument and no return #include<stdio.h> int sum(); // function prototype int main() { sum(); //function call return 0; } int sum() //function defination { int x,y; printf("enter two numbers:"); scanf("%d%d", &x,&y); printf("sum is%d :", x+y); }
  • 16. Function with argument and no return #include<stdio.h> int sum(int a, int b); // function prototype int main() { int x,y; printf("enter two numbers:"); scanf("%d%d", &x,&y); sum(x,y); //function call return 0; } int sum(int a, int b) //function defination { printf("sum is%d :", a+b); }
  • 17. Function with no argument and return #include<stdio.h> int sum(); // function prototype int main() { int a; a=sum(); printf("the sum of the numbers you entered is %d",a); return 0; } int sum() //function defination { int x,y; printf("enter two numbers:"); scanf("%d%d", &x,&y); return(x+y); }
  • 18. Function with argument and return #include<stdio.h> int sum(int,int); // function prototype int main() { int a,x,y; printf("enter two numbers:"); scanf("%d%d", &x,&y); a=sum(x,y); printf("the sum of the numbers you entered is %d",a); return 0; } int sum(int x, int y) //function defination { return(x+y); }
  • 19. Methods of calling function Call by value Call by reference
  • 20. Call by value  Copies the value of actual parameters into formal parameters.  During execution whatever changes are made in the formal parameters are not reflected back in the actual parameters.
  • 22. Call by Reference Reference(address) of the original variable is passed. Function does not create its own copy, it refers to the original values by reference. Functions works with the original data and changes are made in the original data.
  • 23. #include<stdio.h> int swap(int*,int*); // function prototype int main() { int x,y; printf("enter two numbers:"); scanf("%d%d", &x,&y); printf("x=%d and y=%d before calling functionn",x,y); swap(&x,&y); printf("x=%d and y=%d after calling function",x,y); return 0; } int swap(int* x, int* y) //function defination { int temp; temp=*x; *x=*y; *y=temp; }
  • 24. Scope of variables • Block scope • Function scope • Program scope • File scope
  • 25. Block scope  A local scope or block is collective program statements put in and declared within a function or block (a specific region enclosed with curly braces) and variables lying inside such blocks are termed as local variables.  #include <stdio.h>  int main ()  {  /* local variable definition and initialization */ int x,y,z;  /* actual initialization */ x = 20;  y = 30;  z = x + y;  printf ("value of x = %d, y = %d and z = %dn", x, y, z);  return 0;  } 
  • 26. Function Scope:  A Function scope begins at the opening of the function and ends with the closing of it. Function scope is applicable to labels only. A label declared is used as a target to go to the statement and both goto and label statement must be in the same function.
  • 27. Program scope:  After defining a local variable, the system or the compiler won't be initializing any value to it. You have to initialize it by yourself. It is considered good programming practice to initialize variables before using. Whereas in contrast, global variables get initialized automatically by the compiler as and when defined. Here's how based on datatype; global variables are defined.  Life time  Place of declaration  Name conflict 
  • 28. // C program to illustrate the global scope #include <stdio.h> // Global variable int global = 5; // global variable accessed from within a function void display() { printf("%dn", global); } // main function int main() { printf("Before change within main: "); display(); // changing value of global variable from main function printf("After change within main: "); global = 10; display(); }
  • 29. File Scope:  These variables are usually declared outside of all of the functions and blocks, at the top of the program and can be accessed from any portion of the program.  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.  Static int x =10; 
  • 30. Variable storage classes  automatic  register  static  external Storage class datatype vname:
  • 31. Auto Storage Class  auto comes by default with all local variables as its storage class. The keyword auto is used to define this storage class explicitly  Syntax: auto datatype vname;  int roll; // contains auto by default  is the same as:  auto int roll;
  • 32. register Storage Class  This storage class is implemented for classifying local variables whose value needs to be saved in a CPU register in place of RAM (Random Access Memory). This is implemented when you want your variable the maximum size equivalent to the size of the register. It uses the keyword register.  Syntax: register datatype vname;  register int counter;  Register variables are used when implementing looping in counter variables to make program execution fast. Register variables work faster than variables stored in RAM (primary memory).  &
  • 33. static storage class  This storage class uses static variables that are used popularly for writing programs in C language. Static variables preserve the value of a variable even when the scope limit exceeds. Static storage class has its scope local to the function in which it is defined.  On the other hand, global static variables can be accessed in any part of your program. The default value assigned is '0' by the C compiler. The keyword used to define this storage class is static.  Example:  static int var = 6
  • 34. extern Storage class  The extern storage class is used to feature a variable to be used from within different blocks of the same program. Mainly, a value is set to that variable which is in a different block or function and can be overwritten or altered from within another block as well.  #include <stdio.h>  int val;  extern void funcExtern();  main()  {  val = 10;  funcExtern();  }
  • 35. Recursion  Recursion can be defined as the technique of replicating or doing again an activity in a self-similar way calling itself again and again, and the process continues till specific condition reaches.  Every recursive solution has two major cases • Base case • Recursive case
  • 36. Recursion and its types  Direct recursion  Indirect recursion  Tail recursion  Linear and tree recursion
  • 37.  Recursive functions can be classified on the basis of : a.) If the functions call itself directly or indirectly – Direct / Indirect b.) If an operation is pending at each recursive call – Tail Recursive/ Not c.) based on the structure of the function calling pattern – Linear / Tree
  • 38.  Direct Recursion:  If a function explicitly calls itself it is called directly recursive.  When the method invokes itself it is direct. int testfunc(int num) { if (num == 0) return 0; else return (testfunc(num - 1));}  Here, the function ‘testfunc’ calls itself for all positive values of num.
  • 39. Indirect Recursion  This occurs when the function invokes other method which again causes the original function to be called again.  If a method ‘X’ , calls method ‘Y’, which calls method ‘Z’ which again leads to ‘X’ being invoked is called indirect recursive or mutually recursive as well. int testfunc1(int num) { if (num == 0) return 0; else return (testfunc2(num - 1)); } int testfunc2(int num2) { return testfunc1(num2 - 1);}
  • 40. Tail / Bottom Recursion  A function is said to be tail-recursive, if no operations are pending when the recursive function returns to its caller.  Such functions, immediately return the return value from the calling function.  It is an efficient method as compared to others, as the stack space required is less and even compute overhead will get reduced.  Recollect the previously discussed example, factorial of a number. We had written it in non tail recursive way, as after call operation is still pending.
  • 41. Linear and Tree Recursion  Depending on the structure the recursive function calls take, or grows it can be either linear or non linear. It is linearly recursive when, the pending operations do not involve another recursive call to the function. Our Factorial recursive function is linearly recursive as it only involves multiplying the returned values and no further calls to function. Tree recursion is when, pending operations involve another recursive call to function. For example – Fibonacci series, the pending operations have recursive call to the fib() recursive function to compute the results.