Skip to content

Latest commit

 

History

History

chapter05

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Chapter 5- Functions and Recursion

What is a Function?

A function is the self contain block of statement that perform a some specific task

Function is a way to break our code into chunks so than it is posible for a programmer tp reuse them


A function is a block of code which performs a particular task

Every C program can be a collection of these functions. Any C program contain atleast one function(). Progam execute always begin with main().

There is no limit on the number of function that might be present in C language.

Types of functions

Library Functions
the functions that are already beed defined in the C language library are called library function.Like-- printf(), scanf()
User defined functions
A user defined function is developed by user at the time of writing a program main() function is an ex.

Main Function

main() is specially used function in C language. Every C program must have a main() function because in every C program execution always begin with main().

  1. Syntax of Using Functions
#include
void display(); //function prototype
int main(){
    display();// function call
    return 0;
}
void display(){
    printf("Hey I am a function"); //function definition
}

Here void indicates that the function returns nothing

Function Prototype

Function prototype is a way to tell the compiler about the function we are going to define in the program.

Function call

Function call is a way to tell the compiler to execute the funtion body at the time the call is made.

  • The program execution starts from the main function in the sequence the instructions are written.

Function Definition

Contains the exact set of instructions which are executed during the function call when a function is called from main().

  • Good morning function which prints "Good Morning"
  • good afternoon function which prints "Good After noon"
  • good night function which returns "Good Night"

Important points

Why use functions?

  • To avoid returning the same logic again and again
  • To keep track of what we are doing in a program
  • To test and check logic independently

Passing Values to functions

We can pass values to a function and can get a value in return from a function.

int sum(int a, int b);

tha above prototype means that sum is a function which takes two values a(of type int) and b(of type int) and returns a value of type int.

// function definition
int sum(int a,int b){ // a and b are parameters
    int c; 
    c=a+b;
    return c;
}

// calling function
sum(12,14); //12 and 14 are arguments
Parameters
Parameters are tha values or variable placeholderers in the function definition.
Arguments
Arguments are the actual values passed to the function to make a call.
  • A function can return only one value at a time
  • If the passed variable is changed inside the function, the function call doesn't change the value in the calling function.
int change(int a){
    a =77;   //misnomer
    return 0;
}

5.change is a function which changes a to 77. Calling this function

int b=22;
change(b);
printf("b is %d",b);// output is 22

Recursion

A function defined in C can call itself, this is called recursion. A function calling itself is also called "recursive function".

Example of a recursion

A very good example of recursion is factorial.

factorial(n)= n x (n-1) x (n-2) x (n-3) x ⋯  x 3 x 2 x 1
factorial(n)= n x factorial(n-1)
5! = 5 x 4 x 3 x 2 x 1 = 120

Important Notes-

  • Recursion is sometimes the most direct way to code an algorithm
  • the condition which doesn't call the function any further in a recursive function is called as the base condition
  • sometimes due to a mistake made by the programmer, a recursive function can keep running without returning resulting is a memory error.

Exercises

  1. Write a program using functions to find average of three numbers
  2. Write a function to convert celcius temperature into fahrenheit.
  3. write a function to calculate force if attraction on a body of mass m exerted by earth(g = 9. m/s^2)
  4. Write a program using recursion to calculate nth element of fibonacci series.
  5. Write a recursive function to calculate the sum of first n natural numbers.
  6. Write a program using functions to print the following pattern(first n lines)
*
***
*****

Problem--

There is a series, , where the next term is the sum of pervious three terms. Given the first three terms of the series, , , and respectively, you have to output the nth term of the series using recursion.