What is Functions in C with examples

Now in this section, we will put everything we have learnt in the previous sections into work and write a few different programs that use user-defined functions to perform tasks.

In each example, there is a labeled program to demonstrate function declaration, function call, and function definition. Also, each illustrated example has an explanation of the code for better understanding.

Now let’s move further and understand these examples.

Example: Program to count the number of printed lines

Sample code:

#include<stdio.h>
int Lines();
int i,n,a=0;
int main()
{
    printf("Enter the no. of line you want: ");
    scanf("%d",&n);
    Lines();
    printf("Number of lines printed are %d.",a);
    return 0;
}
int Lines()
{
    for (i=a;i<n;i++)
    {
        printf("_\n",a);
        a=a+1;
    }
    return a;
}

Output:

Enter the no. of line you want: 5
_
_
_
_
_
Number of lines printed are 5.

Explanation:

In line 2 of the above code, function Lines() is declared which has a return type int but no argument. So we can use this function multiple times in any function within this program.

In line 7, function scanf() which is an inbuilt library function, is called inside the function main() to take input from the user and store the value in variable n.

In line 8, function Lines() is called inside the function main() to perform the count.

In line 12, function Lines() is defined, and the count value a of type int is returned by the called function Lines() to the calling function main().

In line 9, function main() finally prints the returned value a evaluated inside function Lines().

Example: Program to swap the values of two variables

Sample code:

#include<stdio.h>
void Swap();
int a,b,temp;
int main()
{
  printf("Enter the value of a: ");
  scanf("%d",&a);
  printf("Enter the value for b: ");
  scanf("%d",&b);
  printf("Value of a is %d and b is %d before swapping.\n",a,b);
  Swap();
  printf("Value of a is %d and b is %d after swapping.\n",a,b);
  return 0;
}
void Swap()
{
  temp=a;
  a=b;;
  b=temp;
}

Output:

Enter the value of a: 5
Enter the value for b: 3
Value of a is 5 and b is 3 before swapping.
Value of a is 3 and b is 5 after swapping.

Explanation:

In line 2 of the above code, function Swap() is declared globally which has no return type (hence, void is used) and no argument (hence, empty braces). We can use this function multiple times anywhere within this program.

In line 11, function Swap() is called inside function main() that will be executed and swap the values present in a and b.

In line 16, called function Swap() is defined which has no return type that means it will not return any value back to the calling function main().

In line 13, now function main() will print the swapped values inside the variables a and b respectively to the output screen using function printf() that is an inbuilt library function in C.

Example: Program to check greater of two numbers

Sample code:

#include<stdio.h>
int a,b;
void CheckNum();
int main()
{
  printf("Enter the value of a: ");
  scanf("%d",&a);
  printf("Enter the value of b: ");
  scanf("%d",&b);
  CheckNum();
  return 0;
}
void CheckNum()
{
  if(a>b)
  printf("a=%d is greater than b=%d",a,b);
  else
  printf("b=%d is greater than a=%d\n",b,a);
}

Output:

Enter the value of a: 5
Enter the value of b: 10
b=10 is greater than a=5

Explanation:

In line 3 of the above code, function CheckNum() is declared globally which has no return type i.e. void and no argument. We can call this function anywhere within this program to find greater of two values.

In line 10, function CheckNum() is called by function main() which will evaluate the greater number among the values stored in variables a and b.

In line 13, function CheckNum() is defined that evaluated between two values stored in a and b using if statement and it prints the result on the output screen using function printf().

Example: Program to check whether a number is even or odd

Sample code:

#include<stdio.h>
void EvenOdd();
int n;
int main()
{
    printf("Enter a number: ");
    scanf("%d",&n);
    EvenOdd();
    return 0;
}
void EvenOdd()
{
    if (n%2==0)
    {
        printf("%d is an even number.\n",n);
    }
    else
    {
        printf("%d is an odd number.\n",n);
    }
}

Output:

Enter a number: 10
10 is an even number.

Explanation:

In line 2 of the above code, function EvenOdd() is declared globally that has no return type i.e. void and has no argument too. We can use this function anywhere in this program.

In line 8, function EvenOdd() is called inside function main() which will evaluate the value present in variable n to find out whether it is even or odd.

In line 11, function EvenOdd() is defined that has no return type which means it will not return any value to the calling function main().

In line 13, if statement is used to check whether the value inside variable n is even or odd using test expressions and it will print the statements below accordingly.

Example: Program to find the square of a number

Sample code:

#include<stdio.h>
int n,a;
int Square();
int main()
{
    printf("Enter a number: ");
    scanf("%d",&n);
    Square();
    printf("Square of %d is %d.\n",n,a);
    return 0;
}
int Square()
{
    a=n*n;
    return a;
}

Output:

Enter a number: 10
Square of 10 is 100.

Explanation:

In line 2 of the above code, variables n and a are declared globally which means it can be accessed by any function within this program.

In line 3, function Square() is declared globally which means it can be called multiple times by anywhere in the program. This function has a return type int but no argument.

In line 8, function Square() is called inside function main() that will find the square of value present inside variable n.

In line 12, function Square() is defined which has return type int that will return the value evaluated inside the called function Square() and return it back to the calling function main().

In line 15, after evaluation the value of variable a is returned back to the calling function main().

In line 9, the final result present inside variable a is printed on the output screen using function printf().

In the next section, we will learn about the concept of Arrays in C programming language.