Functions and recursion Flashcards
Function declaration,call and definition
void PrintHello(){
printf(“Hello\n”);
}
int main(){
PrintHello();
}
void-our function here only prints something and does not return anything
#include<stdio.h></stdio.h>
void PrintHello(); //declaration/prototype
int main() {
PrintHello();//function call
return 0;
}
//function definition
void PrintHello(){
printf(“Hello\n”);
}
Properties of a function
-Execution always starts from main
-A function gets called directly or indirectly from main
-There can be multiple functions in a program
Different function types
Library function
User defined function
function to print sum of two numbers
include<stdio.h></stdio.h>
int sum(int a,int b);
int main() {
int a,b;
printf(“enter a :”);
scanf(“%d”,&a);
printf(“enter b :”);
scanf(“%d”,&b);
int s=sum(a,b); printf("the sum is :%d",s); return 0;
}
int sum(int x,int y){
return x+y;
}
Argument vs Parameter
Arguments
-values that are passed in a function call
-used to send value
-actual parameter
Parameter
-values in function declaration and definition
-used to receive value
-formal parameter
Notes
-function can only return one value at a time
-changes to parameter in a function don’t change the values in the calling function (changes made to the argument while defining it is not reflected in the main function)-because only a copy of original is send
Recursion
include<stdio.h></stdio.h>
when a function calls itself
int main() {
int count;
printhello(5);
return 0;
}
void printhello(count){
if (count==0){
return;
}
printf(“helloworld\n”);
printhello(count-1);
}
Sum of N natural numbers using recursion
include<stdio.h></stdio.h>
int sum(int n);
int main() {
printf(“the sum is %d”,sum(10));
}
int sum(int n){
if (n==1){
return 1;
}
int SumN=sum(n-1) +n ;
}
Factorial using recursion
include<stdio.h></stdio.h>
int factorial(int n);
int main() {
printf(“the factorial is %d”,factorial(8));
}
int factorial(int n){
if (n==1){
return 1;
}
int factN=factorial(n-1)*n ;
}
Properties of recursion
-Anything that can be done with iteration,can be done with recursion and vice-versa
-Recursion can sometimes give the most simple solution
-Base case is the condition which stops the recursion
-iteration has infinite loop and recursion has stack overflow
stack overflow means that memory is full and system crash
covert fahrenheit to celsius
include<stdio.h></stdio.h>
float farh(float celsius);
int main() {
float far=farh(37.0);
printf(“farh%f”,far);
return 0;
}
float farh(float celsius){
float far=celsius*(9.0/5.0)+32;
return far;
}
sum of digits of a number
include<stdio.h></stdio.h>
int sum_digit(int number,int sum);
int main() {
int number;
printf(“enter number :\n”);
scanf(“%d”,&number);123
printf("the sum of digit is :%d",sum_digit(number,0));
}
int sum_digit(int number,int sum){
int digit,temp;
if (number==0){
return sum;
}
temp=number;
digit=temp%10;
number=temp/10;
sum=sum+digit;
sum_digit(number,sum);
}
Fibonacci number
include<stdio.h></stdio.h>
int fibonacci(int number);
int main() {
int number;
printf(“enter the number :\n”);
scanf(“%d”,&number);
printf(“fibonacci number is :%d”,fibonacci(number));
}
int fibonacci(int number){
int FN;
if (number==1){
return 1;
}
if (number==0){
return 0;
}
FN=fibonacci(number-1)+fibonacci(number-2);
}
square root using babylonian method
include<stdio.h></stdio.h>
double sqrt(number);
int main() {
int number;
printf(“enter the number :\n”);
scanf(“%d”,&number);
printf(“square root of number %d is : %lf”,number,sqrt(number));
}
double sqrt(number){
double x=number;
double y=1;
double e=0.000001;
while(x-y>e){ x=(x+y)/2; y=number/x; } return x; }
Power function
include<stdio.h></stdio.h>
int pow(int a,int b);
int main() {
int a,b,powerab;
printf(“enter a :\n”);
scanf(“%d”,&a);
printf(“enter b :\n”);
scanf(“%d”,&b);
powerab=pow(a,b);
printf(“power of %d raise to %d is : %d “,a,b,powerab);
}
int pow(int a,int b){
int power=1;
for (int i=0;i<b;i++){
power*=a;
}
return power;
}