C programming

Chapter 5

Functions & Recursion

Sometimes our program gets bigger in size and it’s not possible for a programmer to track which piece of code is doing what.

Function is a way to break our code into parts so that it is possible for a programmer to reuse them.

What is a function ?

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

A function can be reused by the programmer in a given program any number of times.

Example and syntax of a function.

// Simple program of function

void printline();
#include<stdio.h>

void main()
{

printline();
printf("\n It is The Programming \n");
printline();

}
void printline(void)
{
int i;
for(i=1;i<23;i++)
{
printf("-");
}
}

Output:
-----------------------
It is The Programming
-----------------------

A  Function have three major parts

1. Function prototype.
2. Function call
3. Function definition

Function prototype

Function prototype is a way to tell the compiler about the function we are going to define in the program avoid indicates that the function returns nothing.

Function call

Function call is a way to tell the compiler to execute the function body at the time the call is made not that the program execution starts from the main function in the sequence the instructions are written

Function definition

This part contains the exact set of instructions which are executed during the function call when a function is called from main () the main function falls asleep and gets temporarily suspended during this time the control goes to the function being called when the function body is done executing main() resumes

Main ()should call all of these in order 1 2 3

Important points

  • Execution of a C program starts from the main ()
  • A C program can have more than one function
  • Every function gets called directly or indirectly from main ()
  • There are two types of functions in C let’s talk about them

Types of  functions

  1. Library functions : commonly required functions grouped together in a library file on disk
  2. User defined functions : these are the functions declared and defined by the user.

Why use functions ?

  • To avoid rewriting 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)

The above prototype means that sum is a function which takes values a of type int and b of type int and returns a value of type int

Function definition of sum can be

int sum ( int a,int b){
int c;
c = a+b;
return c;
}

Now we can call sum (2,3) from Main to get 5 in return

int d = sum (2,3);.  d becomes 5

Notes

1. Parameters are the values or variable placeholders in the function definition
2. Arguments are the actual values passed to the function to make a call
3. If function can return only one value at a time
4. If the passed variable is it changed inside the function the function call doesn’t change the value in the calling function
Some misnomer

int change (int a){
a = 77;
return 0;
}
Change is a function which changes a to 77 now if we call it from main like this

Int b = 22;
change (b);
printf("b is %d",b);
Note || the values of a b remains 22
...This happens because a copy of b is passed to the change function.

Recursion

If function defined in C can call itself this is called recursion a function calling itself is also called a recursive function.

Example of recursion

A very good example of recursion is factorial

Since we can write factorial of a number in terms of itself we can program it using recursion

include<stdio.h &gt;

long factorial(int);

int main()
{
  int n;
  long f;

  printf("Enter an integer to find its factorial\n");
  scanf("%d", &n);

  if (n < 0)
    printf("Factorial of negative integers isn't defined.\n");
  else
  {
    f = factorial(n);
    printf("%d! = %ld\n", n, f);
  }

  return 0;
}
long factorial(int n)
{
  if (n == 0) // Base case
    return 1;
  else
    return (n*factorial(n-1));

Important notes

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