Functions

Functions:
A function is itself a block of code which can solve simple or complex task/calculations. A function performs calculations on the data provided to it is called "parameter" or "argument". A function always returns single value result. 

Types of function:

 1. Built in functions(Library functions)
 a.) Inputting Functions.
 b.) Outputting functions. 

2. User defined functions. 
a.) fact();
 b.) sum();

 Parts of a function: 
1. Function declaration/Prototype/Syntax. 
2. Function Calling. 
3. Function Definition.
 
1.)Function Declaration:
 Syntax: <return type><function name>(<type of argument>)
The declaration of function name, its argument and return type is called function declaration.

 2.) Function Calling:
 The process of calling a function for processing is called function calling.
 Syntax: <var_name>=<function name>(<list of arguments>)

3.) Function defination:
The process of writing a code for performing any specific task is called function defination.
 Syntax: 
<return type><function name>(<type of argument>)
<statement 1>
<statement 2>
return(<value>
}

 Example: program based upon function: 
WAP to compute cube of a no. using function.
#include<stdio.h>
#include<conio.h>
void main() 
int c,n;
 int cube(int);
 printf("Enter a no.");
scanf("%d",&n);
 c=cube(n); 
printf("cube of a no. is=%d",c);
}
 int cube(int n)
 {
 c=n*n*n; 
return(c);
 } 

Recursion 

Firstly, what is nested function? 
When a function invokes another function then it is called nested function.
 But, 
When a function invokes itself then it is called recursion.

 NOTE: In recursion, we must include a terminating condition so that it won't execute to infinite time.
Example:
 program based upon recursion:
 WAP to compute factorial of a no. using Recursion:
 #include
#include
void main() 
{
 int n,f;
 int fact(int)
 printf("Enter a no.");
scanf("%d",&n);
 f=fact(n); 
printf("The factorial of a no. is:=%d",f);
}
 int fact(int n)
 int f=1; 
if(n=0) 
return(f);
 else
 return(n*fact(n-1));
 }

Comments