Functions and recursion Flashcards

1
Q

Function declaration,call and definition

A

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”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Properties of a function

A

-Execution always starts from main
-A function gets called directly or indirectly from main
-There can be multiple functions in a program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Different function types

A

Library function
User defined function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

function to print sum of two numbers

A

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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Argument vs Parameter

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Recursion

A

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);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Sum of N natural numbers using recursion

A

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 ;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Factorial using recursion

A

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 ;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Properties of recursion

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

covert fahrenheit to celsius

A

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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

sum of digits of a number

A

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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Fibonacci number

A

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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

square root using babylonian method

A

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; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Power function

A

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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly