Functions in C Programming Language Tutorial With Example

Functions in C Programming Language Tutorial With Example is today’s topic. A function is the set of statements that take inputs, do some specific computation and returns the output. The main idea is to put some common task together and make the function so that instead of writing the same code again and again for the different inputs, we can call that function and get the result depending on the different inputs. Every C program has at least one function, which is the main() function, and all the most trivial programs can define additional functions. You can divide your code into separate functions. How you divide up your code is up to you, but logically the division is such that each function performs a specific task.

Functions in C Tutorial With Example

Dividing the complex problem into small components makes the program easy to understand and use.

Types of Functions

There are mainly two types of functions.

  1. Standard Library Functions(SLF).
  2. User Defined Functions(UDF).

Standard Library Functions

The standard library functions are the built-in functions in C programming to handle tasks such as the mathematical computations, I/O processing, string handling, etc. These functions are defined inside the header in C file. When you include this header file, these functions are available for use.  The C programming libraries like stdio.h and conio.h are the examplesThe printf() function is the standard library function to send formatted output to the screen (display output on the screen). This function is defined in “stdio.h” header file.

User Defined Functions

C programming language allows programmers to define their own functions. Those functions created by the user are called user-defined functions. We can create as many user-defined functions as we want.

Defining a Function

Following is the syntax of C Program.

return_type function_name( parameter list ) {
   the body of the function
}

The function definition in C programming consists of the function header and the function body. Here are all the parts of a function −

  • Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
  • Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.
  • Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as an actual parameter or argument. The parameter list refers to the type, order, and the number of the parameters of the function. Parameters are optional; that is, a function may contain no parameters.
  • Function Body − The function body contains a collection of statements that define what the function does.

Parameter Passing to Functions

The parameters passed to the function are called actual parameters.
The parameters received by the function are called formal parameters.
There are two ways to pass the parameters. Pass by Value and Pass by Reference.

Pass by Value: In this parameter passing method,  the values of actual parameters are copied to the function’s formal parameters, and the two types of parameters are stored in the different memory locations. So if we made the change inside functions, then that is not reflected in the actual parameters of the caller.

Pass by Reference: Both the actual and formal parameters refer to the same locations, so any changes made inside the function are reflected in the actual parameters of the caller.

In C programming language, the parameters are always passed by value.

Example of Functions in C

Create one file called func.c and add the following code inside it.

#include <stdio.h> 
int fun(int z) 
{
  return z * z; 
} 
  
int main(void) 
{ 
    int x = 20; 
    int y = fun(x); 
    printf("y = %d \n", y); 
    return 0; 
}

Save the file and compile the file using the following command.

gcc func.c

Now, run the file.

./a.out

 

Functions in C Programming Language Tutorial With Example

The above program is simple call by value. Here, 20 is the actual parameter which is passed to a function func. The function will receive that parameter called the formal parameter. So x is an actual parameter and z is the formal parameter.

We can write the same example as a pass by reference parameter.

#include <stdio.h> 
int fun(int *ptr) 
{
  return *ptr * *ptr; 
} 
  
int main(void) 
{ 
    int x = 20; 
    int y = fun(&x); 
    printf("y = %d \n", y); 
    return 0; 
}

So, here we have passed the reference of the x, and then the function gets its value and returns its square. Here, the actual parameter is the address of x and not the value of x. But inside the function, we have taken the pointer which returns the value of that address. So finally, we get the values and multiple with each other and return that values.

Every C program has the function called main() that is called by the operating system(OS) when the user runs a program.  Every function has its return type. If the function doesn’t return any value, then the void datatype is used as return type. If the function’s return type is void, we still can use the return statement in the body by not specifying any constant, variable, etc.

Advantages of the User-defined function

  1. If we use UDF, then the program will be easier to understand and maintain.
  2. We can create a reusable code using UDF that can be used in other programs.
  3. An extensive program can be divided into smaller modules. Hence, a large project can be distributed among many programmers.

Finally, Functions in C programming language tutorial with an example is over.

Post Your Thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.