C Function with Detailed Explanations of Example

In this tutorial, we will learn how the functions in C work and we will also discuss the two types of functions which are the standard library functions and the user-defined functions.

What is C Function?

In C program, a function is a block of code which only performs a specific task when it is called. We can pass data, which is also known as parameters into a function.

Furthermore, the functions are used to complete a certain actions and it is important for reusing a code. Which is to defined the code once and we will use it many times.

Also read: C Decision Making with Code Examples

Types of C Functions

Here are the two types of functions in C:

  • Standard library functions
  • User-defined functions

Standard library functions

In C programming language, standard library functions is an inbuilt functions.

In addition, the header files are functions are defined. 

Here are the examples:

  • A printf() is a standard library function that shows the formatted output.
  • The printf() is a standard library function to send formatted output to the screen (display output on the screen). This statement is defined in the stdio.h header file.
    However, for using the printf() function, we must include the stdio.h header file using #include <stdio.h>.
  • A sqrt() is a function to determine the square root of a number. This function is used to define the math.h header file.  

We will use the printf() function, which is the header file <stdio.h> must be included.

#include <stdio.h>
int main()
{
   printf("This is the first program to run"); 
}

If we try to use printf() which is not included in the stdio.h header file, we will get an error.

Read also the other C tutorial: Operators in C with Code Examples

The advantages of using C library functions

  • Its Work
    • The most important reasons we need to use library functions are it is easy to use and because they are working. These functions have ended over multiple correct testing, thus it easy and simple to use.
  • Optimized a functions performance
    • Therefore, a function which is “standard library functions“, a group of developers is constantly improving. In means, they are able to make the most useful code to optimize for maximum performance.
  • To saves reasonable improvement time
    • Therefore, the regular functions such as printing on a screen, and determining the square root. We shouldn’t concerned about creating them again.
  • The functions are convenient
    • In every need in the real world, will change it in constant, the function is expected to work every time, and everywhere. We can use these library functions since its works on all computers.

For example:

We assume we will find the square root of a number.

To calculate the square root of a number, we will use the sqrt() library function. These functions well defined the math.h header file.

#include <stdio.h>
#include <math.h>
int main()
{
   float number, root;
   printf("Enter a number: ");
   scanf("%f", &number);

   // To calculate the square root of number and stored in root.
   root = sqrt(number);

   printf("Square root of %.2f = %.2f", number, root);
   return 0;
}

Output:

Enter a number: 64
Square root of 64.00 = 8.00

Library Functions in Various Header Files

Header Files in CDescription
<assert.h>Program assertion statement
<ctype.h>Character type statement
<locale.h>Localization statement
<math.h>Mathematics statement
<setjmp.h>Jump statement
<signal.h>Signal handling statement
<stdarg.h>Variable arguments handling statement
<stdio.h>Standard Input/Output statement
<stdlib.h>Standard Utility statement
<string.h>String handling statement
<time.h>Date time statement

User-defined function

We can also make functions as we required. All functions which are created by the user are called user-defined functions.

How user-defined function works?

#include <stdio.h>
void conditionName()
{
    ********  ********
    ********  ********
}

int main()
{
   ********  ********
   ********  ********

    conditionName();
    
   ********  ********
   ********  ********
}

In C program, the execution will start from the main() function.

If the compiler will encountered a conditionName(); To control the program it will jump to

void conditionName()

Therefore, the compiler will begin to execute the codes inside conditionName().

A control of the program will jump back to the main() function, which once the code inside of the function definition will be executed.

The Advantages of user-defined function

  • The program will be simple to understand, retain and debug.
  • The scripts that can be reused and can be applied into programs.
  • A wide program can be divided into lesser modules. However, a large project can be divided into many programmers.

Example of user-defined function

#include <stdio.h>
int addition_of_Numbers(int x, int y); 

int main()
{
    int num1,num2,sum;

    printf("Enters two numbers: ");
    scanf("%d %d",&num1,&num2);

    sum = addition_of_Numbers(num1, num2); 
    printf("The sum of two numbers is: = %d", sum);

    return 0;
}

int addition_of_Numbers(int x, int y)  
{
    int result;
    result = x+y;
    return result;  
}

Output:

Enters two numbers: 64 12
The sum of two numbers is: = 76

Create a Function in C

To make a function, we will defined the name of the function, and it is followed by a parentheses () and a curly brackets {}:

Syntax Used:

void statementName() {
  // This is a code to be executed
}

Explanation of the code above:

  • The statementName() which is the name of the function.
  • The void means that a function has no return value.
  • Inside of the function in the body, we can add the code that will defined what the function must do.

Call a C Function

In C program, the declared functions are not executed directly. They are “saved for later use”, and if they are called, they will be executed.

To call the function, we will write the function’s name and it will be followed by two parentheses () and a semicolon ;

For example, The statementName() will be used to print a text, if it is called:

void statementName() {
  printf("I run the first program in this example of a function!");
}

int main() {
  statementName();
  return 0;
}

Output:

I run the first program in this example of a function!

Conclusion

To conclude, we already learned the C function and we give examples, and also we discussed the advantages of types of functions.

We aim in this tutorial that we could improve your skills functions in C and learn how we can use it for your software development projects.

Leave a Comment