Chapter 5 Functions
Chapter 5 Functions
Chapter 5 & 6
Regarding C-Function
• It is Module that is define outside main function.
• It helps in reusing the written code multiple times.
• It receives some parameters as input arguments and returns some
parameter as output
• Function Declaration
• Function Calling from main function
• Calling by value
• Calling by reference
• Function Definition
Function Declaration
Example:
int funName(int, char);
𝑛+1 𝑚=0
𝐴𝑐𝑘𝑒𝑟𝑚𝑎𝑛𝑛 𝑚, 𝑛 = ൞ 𝐴𝑐𝑘𝑒𝑟𝑚𝑎𝑛𝑛 𝑚 − 1, 1 𝑚 > 0 𝑎𝑛𝑑 𝑛 = 0
𝐴𝑐𝑘𝑒𝑟𝑚𝑎𝑛𝑛 𝑚 − 1, 𝐴𝑐𝑘𝑒𝑟𝑚𝑎𝑛𝑛 𝑚, 𝑛 − 1 𝑚 > 0 𝑎𝑛𝑑 𝑛 > 0
int temp;
temp = varA;
varA = varB;
varB = temp;
// Method 2
// varA = varA+varB;
// varB = varA-varB;
// varA = varA-varB;
printf("\nvarA = %d varB = %d", varA, varB);
}
Call by Value Versus Call by Reference
S. No. Calling by Value Calling by Reference
Pass a pointer that contains the memory
1 Copies the value of an object. address of an object that gives access to its
contents.
Guarantees that changes that alter the state of
Changes that alter the state of the parameter
the parameter will only affect the named
2 will reflect to the contents of the passed
parameter bounded by the scope of the
object.
function.
More difficult to keep track of changing values
Simpler to implement and simpler to reason
3 that happens for each time a function may be
with.
called.
Call By Value
#include <stdio.h> int main(){
int x = 10; printf("Values before swap: x = %d, y = %d\n", x,y);
int y = 11; swap(x, y);
printf("Values after swap: x = %d, y = %d", x,y);
void swap(int x, int y){ }
int temp = x;
x = y;
y = temp;
}
Call By Reference
#include <stdio.h> int main(){
int x = 10;
void swap(int *x, int *y){ int y = 11;
int temp = *x; printf("Values before swap: x = %d, y = %d\n", x, y);
*x = *y; swap(&x, &y);
*y = temp; printf("Values after swap: x = %d, y = %d", x, y);
} }
Recursion
• Recursion is the technique of making a function call itself.
• Advantages
1. The code may be easier to write.
2. To solve such problems which are naturally recursive such as tower of Hanoi.
3. Reduce unnecessary calling of function.
4. Extremely useful when applying the same solution.
5. Recursion reduce the length of code.
6. It is very useful in solving the data structure problem.
7. Stacks evolutions and infix, prefix, postfix evaluations etc.
• Disadvantages
1. Recursive functions are generally slower than non-recursive function.
2. It may require a lot of memory space to hold intermediate results on the system stacks.
3. Hard to analyze or understand the code.
4. It is not more efficient in terms of space and time complexity.
5. The computer may run out of memory if the recursive calls are not properly check
Recursion
void recurse()
{
... .. ...
recurse();
... .. ...
}
int main()
{
... .. ...
recurse();
... .. ...
}
Adding Numbers - Recursion
int sum(int k) { int main() {
if (k > 0) { int result = sum(10);
return k + sum(k - 1); printf("%d", result);
} else { return 0;
return 0; }
}
}
Explanation
• 10 + sum(9)
• 10 + ( 9 + sum(8) )
• 10 + ( 9 + ( 8 + sum(7) ) )
• ...
• 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
• 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0
Factorial - Recursion
#include<stdio.h> long int multiplyNumbers(int n) {
long int multiplyNumbers(int n); if (n>=1)
int main() { return n*multiplyNumbers(n-1);
int n; else
printf("Enter a positive integer: "); return 1;
scanf("%d",&n); }
printf("Factorial of %d = %ld", n,
multiplyNumbers(n));
return 0;
}