Functions and Recursions

Photo by Nico Smit on Unsplash

Functions and Recursions

A function is a block of code that runs, only when it's called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.

Syntax :

void printHello( ); //declaration prototype

int main( ){

printHello( ); //function call

return0; }

void printHello( ) //function definition

{printf("Hello"); }

In the Declaration prototype, declaration indicates that the given variable, constant, or function exists and tells its type, for example, const int i, double sqrt( ). A prototype provides information on the arguments to a function as well as its return type.

A function call is an expression containing the function name followed by the function call operator.

A function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit.

Q1 Write a function that prints Namaste if a user is Indian and Bonjour if the user is French in C.

#include <stdio.h>
#include<string.h>
void printhello(char ch);//declaration prototype
int main()
{
 char character;
 printf("Enter i if you are Indian and f if you are French : ");
 scanf("%c",&character);
  printhello(character);
}
void printhello(char ch)//function defination
{
  if(ch=='i'){
    printf("\tNamaste _||_ :-) Indian %c",ch);
  }
  else if(ch=='f'){
    printf("\tBonjour :-) French %c",ch);
  }
}

Output :

Properties

  • Execution always starts from the main Properties.

  • A function gets called directly or indirectly from the main.

  • There can be multiple functions in a program

There are 2 types of functions :

Library function: They are special functions inbuilt in C. Eg- scanf( ), printf( ).

User Defined- Declared and defined by the programmer.

Passing Arguments

Functions can take value & give some value {parameter} {return value}

void printHello( );

void printTable(int n);

int sum(int a, int b);

Difference between Argument and Parameter

Argument

  1. Values that are passed in the function call

  2. Used to send value

  3. Actual parameter

Parameter

  1. Values in function declaration & definition

  2. Used to receive value

  3. Formal parameters

Note: a. Function can only return one value at a time. b. Changes to parameters in functions don't change the values in calling function. Because a copy of the argument is passed to the function.

Q2 A program to accept a value from the user and calculate the gst in C.

#include <stdio.h>
#include<string.h>

void gst(float v);
int main()
{
   float val;
   printf("Enter a value to calculate gst\n");
   scanf("%f",&val);
   gst(val);
   printf("The initial value is=%f\n",val);
   return 0;
}
void gst(float val){
   val=val +(0.18*val);
   printf("Final price is=%f\n", val);
}

Output :

Q3 Use library functions to calculate the square of a number given by the user in C.

#include <stdio.h>
#include<string.h>

void sq(float sq);
int main()
{
    float j;
    printf("Enter the number to calculate the sq\n");
    scanf("%f",&j);
    sq(j);

   return 0;
}
void sq(float sq){        //We use void because there is no return                 value
   sq=sq*sq;
    printf("The square of the value is %f\n",sq);
}

Output :

Q4 Write functions to calculate area of a square, a circle and rectangle in C.

#include<stdio.h>
#include<math.h> 
float square(float side);
float circle(float rad);
float rect(float a,float b);

int main(){
   float a,b;
   printf("Enter a value=\n");
   scanf("%f",&a);

    int number;
    printf("Enter 1=area of circle\n 2=square area \n 3=area of rect\n");
    scanf("%d",& number);
    switch(number)
    {
      case 1:printf("The area of the circle is=%f",circle(a));
      break;
      case 2:printf("The area of the square is=%f",circle(a));
      break;
      case 3:
       printf("Enter a value=\n");
       scanf("%f",&b);
       printf("The area of the rectangle is=%f",circle(a));
      break;
      default:printf("Wrong Input");
    }

}
float circle(float rad){
   return 3.14* rad* rad;}

   float square(float side){
   return side*side;}

   float rect(float a,float b){
      return a*b;
   }

Output :

RECURSION

When a function calls itself, it's called recursion.

Property of Recursion

a. Anything that can be done with Iteration, can be done with recursion and vice-versa. Properties of Recursion.

b. Recursion can sometimes give the most simple solution.

c. Base Case is the condition that stops recursion.

d. Iteration has an infinite loop & Recursion has stack overflow

Example for recursion:

Example for recursion:
#include<stdio.h>
#include<math.h> 

void printhw(int count);
int main(){
   printhw(10);
   return 0;
}
void printhw(int count){
   if (count==0)
   {return;}
   printf("Hello World\n");
   printhw(count-1);
}

Note- If you use void then the compiler goes from the main program to the mini program but if you use the Int or Float recursion it is the opposite the function is done in the side program and the answer is given in the main program.

Q5 Write a code for the sum of first n natural numbers using (recursion tree) in C.

#include<stdio.h>
#include<math.h> 

int sum(int n);

int main(){
   int a;
   printf("Enter a number\n");
   scanf("%d",&a);
   printf("The ans %d",sum(a));
   return 0;
}
int sum(int n){
   if (n==1)
   {
      return 1;
   }  

   int sumN1=sum(n-1);
   int sumn= sumN1+n;
   return sumn;

}

Output :

Q6 Write a code to find the Factorial of n. (the product of an integer and all the integers below it; e.g. factorial four ( 4! ) is equal to 24.) in C.

Mathematics: How are Complex Numbers used in the Real World?

(I know this sounds like complicated math stuff byt hang on its easier than you think.)

#include<stdio.h>
#include<math.h> 

int sum(int n);

int main(){
   int a;
   printf("Enter a number\n");
   scanf("%d",&a);
   printf("The ans %d",sum(a));
   return 0;
}
int sum(int n){
   if (n==1)        //{[(BASE CASE)]}        :-)
   {
      return 1;
   }
      int sumN1=sum(n-1);
   int sumn= sumN1*n;
   return sumn;

}

Output :

Note- Base case is really important or else the program will repeat itself until it crashes.

Q7 Write a function to convert celsius to Fahrenheit in C.

#include<stdio.h>
#include<math.h> 

float temp(float n);

int main(){
   //A program to convert from celcius to 
   //Farahanite temperature
   float a;
   printf("Enter a number\n");
   scanf("%f",&a);
   printf("The ans %f",temp(a));
   return 0;
}
float temp(float n){
   float d=n*(9.0/5.0)+32;
   return d;
}

Output :

Q8 Write a code to calculate the percentage of a student's marks in science math and Sanskrit.

#include<stdio.h>
#include<math.h> 

float percent(float n,float m,float o);

int main(){

   float science,math,sanskrit;
   printf("Science marks\n");
   scanf("%f",&science);
   printf("Math marks\n");
   scanf("%f",&math);
   printf("Sanscrit marks\n");
   scanf("%f",&sanskrit);
   printf("The ans in percent-%f",percent(science,math,sanskrit));
   return 0;
}
float percent(float n,float m,float o){
   float q=((n+m+o)/300)*100;
   return q;
}

Output :

Q9 Print a Fibonacci sequence. or Write the n'th number of the Fibonacci sequence in C.(a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc.)

Nature's patterns: Golden spirals and branching fractals - CNET

Fibonacci sequence in the universe

Formula for fibonacci sequence fb(n)=fb(n-1)+fb(n-2)

#include<stdio.h>
#include<math.h> 

int fib (int n);
int main(){
   int a;
   printf("Enter a number\n");
    scanf("%d",&a);
   printf("The ans %d",fib(a));
}
int fib(int n){
   if(n==0){
      return 0;
   }
   if(n==1){
      return 1;
   }
   int fib1=fib(n-1);
   int fib2=fib(n-2);
   int fib=fib1+fib2;
   printf("fib of%d is=%d\n",n,fib);
   return fib;
}

Output :

Hope you enjoyed learning about Functions, this helps us to re use code many times in your coding journey.And hope this blog was useful.

Thank you...