Imagine, you are making a game. And you need to display scores frequently. One solution is to write the whole code to display the score again and again. But there is another better solution using functions. We can define that whole code in a function once and call it whenever we want.
Function is nothing but a group of codes put together and given a name. And these can be called anytime without writing the whole code again and again.
Why Functions?
Functions make our code neat and easily readable and avoid us to rewrite the same code again and again.
Types of Functions in C
There are two types of functions in C.
- Library Functions
- User-defined Functions
Library Functions
Library functions are pre-defined functions in C. We have already read about some of these library functions like printf()
and scanf()
.
User Defined Functions
We can also define our own functions in C.
Let's first see how to declare a function of our own.
How to Declare a Function?
We declare a function as follows
return_type function_name ( parameters ) ;
As an example, suppose we have to calculate the average of 2 numbers 'num1' and 'num2'. The numbers are integers and average is of type float. We will pass the numbers to a function that will return the average value of those numbers. We will declare that function as follows:
float average( int num1, int num2 );
Here, the function named 'average' is taking 'num1' and 'num2' of integer type as input and then returning the average value of type float after calculating it.
Defining Function
Syntax for defining a function is
return_type
function_name
(
parameters
)
{
//code
}
Let's see the 'average' function that we defined above.
float average( int num1, int num2 )
{
float avg; /* declaring local variable */
avg = ( num1 + num2 )/2.0;
return avg; /* returning the average value */
}
Variables declared inside function are called local variables. A local variable can only be used in the function in which it is declared. It has no use outside the function. For example, in our case, 'avg' is a local variable.
int
.
return avg
→ This means that this function will give us or return us the variable (or its value) 'avg' which is of type float. You will understand this clearly in examples.
Calling Function
To use a function, we need to call it. Once we call a function, it performs its operations and after that the control again passes to the main program.
To call a function, we need to specify the function name along with its parameters.
function_name ( parameters ) ;
So, we will call our average function as
average( num1, num2 );
Now, let's combine all to take out the average of 2 numbers using a function.
#include <stdio.h>
float average(int num1, int num2); /* declaring function named average */
int main()
{
int num1, num2;
float c;
printf("Enter first number\n");
scanf("%d", &num1);
printf("Enter second number\n");
scanf("%d", &num2);
c = average( num1, num2 ); /* calling the function average and storing its value in c*/
printf("Average is %.2f\n", c);
return 0;
}
float average( int num1, int num2 ) /* function */
{
float avg; /* declaring local variable */
avg = (num1 + num2)/2.0;
return avg; /* returning the average value */
}
5
Enter second number
4
Average is 4.50
float average( int num1, int num2 )
→ We have declared that we have defined a function named 'average' in our program so that, 'main' function can search for it while calling.
Here, a function named 'average' with 2 integer parameters ('num1' and 'num2') and return type of float is declared. This means that while calling the function, we need to give two integers as input and in return, it will give us a float as output.
By writing c = average( num1, num2 );
, we are calling the function 'average' with 2 parameters ( 'num1' and 'num2' ). As it is returning a float, and here it is 4.50, so, this expression will be equivalent to c = 4.50
as 4.50 is returned by the function and its value gets stored in a variable 'c'.
We can also define a function at the time of declaration as in the example below.
#include <stdio.h>
float average( int num1, int num2 ) /* function */
{
float avg; /* declaring local variable */
avg = ( num1 + num2 )/2.0;
return avg; /* returning the average value */
}
int main()
{
int num1, num2;
float c;
printf("Enter first number\n");
scanf("%d", &num1);
printf("Enter second number\n");
scanf("%d", &num2);
c = average( num1, num2 ); /* calling the function average and storing its value in c*/
printf("Average is %.2f\n", c);
return 0;
}
5
Enter second number
4
Average is 4.50
We haven't declared our function seperately (float average(int num1, int num2);
) as we did in the previous example. Instead, we have defined our 'average' function before 'main'. So in this case, while executing 'main', the compiler will know that there is a function named 'average' because it is defined above from where it is being called.
If a function doesn't return anything, then its return type is written as void
as in the example below
#include <stdio.h>
void display( int n ) /* function */
{
printf("Number is %d\n", n);
}
int main()
{
int n;
printf("Enter number\n");
scanf("%d", &n);
display(n); /* calling the function display*/
return 0;
}
5
Number is 5
void
means that the function will not return anything.
It is also possible to define a function without any argument. See an example over this.
#include <stdio.h>
void display()
{
printf("Function with no argument\n");
}
int main()
{
display();
return 0;
}
As you can see, there is no argument given in the function display()
.
Calling a function inside another
Yes, we can call a function inside another function. We have already done this. We were calling our functions inside the main function. Now look at an example in which there are two user defined functions. And we will call one inside another.
#include <stdio.h>
int div_2(int a)
{
if(a%2==0)
{
return 1;
}
else
{
return 0;
}
}
void div_6(int b)
{
if( div_2(b)==1 && b%3 == 0 )
{
printf("Yes, the number is divisible by 6.\n");
}
else
{
printf("No, the number is not divisible by 6.\n");
}
}
int main()
{
div_6(12);
div_6(25);
return 0;
}
No, the number is not divisible by 6.
A number is divisible by 6 if it is divisible by 2 and 3 both. We have a function div_2
which will return 1 if the given number is divisible by 2. Another function that we have defined is div_6
which calls div_2
inside itself.
if( div_2(b)==1 && b%3 == 0 )
→ If div_2
returns 1, it means that the number is divisible by 2. And if b%3==0
is true, it means that 'b' is divisible by 3. So, if it is divisible by both 2 and 3, then it is divisible by 6 also.
Recursion
Recursion is the calling of a function within the same function.
Let's consider an example in which we have to calculate the factorial of a number.
Factorial of a number n is the product of all the natural numbers till n. For example, the factorial of 5 is 1*2*3*4*5 = 120. Factorial of 0 and 1 is 1.
#include <stdio.h>
int factorial( int a ) /* function */
{
if( a == 0 || a == 1)
{
return 1;
}
else
{
return a*factorial(a-1);
}
}
int main()
{
int n;
printf("Enter number\n");
scanf("%d", &n);
int fact = factorial(n);
printf("Factorial of %d is %d\n", n, fact);
return 0;
}
4
Factorial of 4 is 24
Here, if the integer 'n' is 0 or 1, return 1;
is returning factorial value as 1. Otherwise, if the integer 'n' is greater than 1, then n*factorial(n-1)
multiplies 'n' with the factorial of 'n-1'. For example, if the number is 4, then 'n*factorial(n-1)' implies '4*factorial(3)'. So by writing 'n*factorial(n-1)', we are again calling the function 'factorial' inside itself but this time the argument is 'n-1' i.e., factorial(n-1)
.
So, first give 0 to factorial function, it will give us 1.
Again, give 1, it will return 1 again.
Now, give 2. This time the statement in the body of 'else' will be executed and it has to return n*factorial(n-1)
i.e., 2*factorial(1)
. So, it will again call the 'factorial' function with 1 as argument (as factorial(1)) and it is 1. Finally, it will return 2*1
i.e. 2.
Now, let's try this with 3. This time it has to return 3*factorial(2)
. Again factorial(2)
will be called and it will return 2*factorial(1)
. So, the expression will be return 3*2*factorial(1)
and then 3*2*1
( as factorial(1) will return 1 ).
For 4, the expression will be 4*factorial(3)
. Calling factorial(3), it will be 4*3*factorial(2)
( as factorial(3) will return 3*factorial(2) ). Again calling factorial(2), it will be 4*3*2*factorial(1)
. And at last, it will return 4*3*2*1
or 24.