C – Function4 min read

A function refers to a block of code that performs a specific task. We can divide and create a separate function in a program so that each function performs a different specific task and can be called as many times as needed in the program.

  • At least one pre-defined function is always executed by the C compiler that is the main() function.
  • It allows the user to reuse the code as many times without rewriting the code and therefore act as a time saver.
  • The function is declared within the curly braces {}.

C function is defined into two categories:

  1. Library functions
  2. User-defined functions

1. Standard Library functions:

These functions are built-in function in C programming that is already defined in C Library. Example: printf()scanf()strcat() etc. To use these functions an appropriate header needs to be included in the program.

2. User-defined functions:

On the other hand, user-defined functions are the ones that are defined by the users while writing the code and they perform the task assigned by the users.

User-defined functions are used for code re-usability and for saving time and space.


Defining a function:

The general way to define a function in C:

Each part of a function:

  • Return type: A function may return a value unless it is a void function, which does not return any type of value. return_type is a data type of the function. It could return arithmetic values (int, float, etc.), pointers, structures, etc.
  • Function name: Function name refers to identifiers through which function can be identified uniquely. It is a function’s name that is stated to function by following some naming rules.
  • Parameter list: Parameter is also referred to as Argument. We can pass the values through the parameter when the function is invoked. The parameter list refers to the type, order, and number of the parameters of a function
  • Function Body: The function body is enclosed within the curly braces'{}’. It defines the task of a method within the curly braces.

Example:


Declaring a Function:

Declaration of function includes the function name and its return type and parameters (if any).

When the function is called, the execution control passes to the function and does some specific task. When the return statement of the function is executed or when it executes the end braces, it means the program control is returned to the main function.

The function declaration contains:

return_type function_name( parameter list );

In the above example the declaration is stated as:

int min(int num1, int num2);

Declaration of function is required when the source is on one file and you call that function from different files.

Note: Also note that, if you defined the function before the main function then the separate function declaration is not required in a program else you need to mention the declaration. Example: In the syntax below, a declaration is not required


Functions Arguments

In a function in c, information is exchanged or passed by the means of argument or parameters. A function is called with an argument or without an argument in C.

There are two ways in which the argument can be passed through a function:

  • Call by Value.
  • Call by Reference.

Call by Value in C.

In this method, the actual argument is copied to the formal parameter of the function. Hence any operation performed on the argument does not affect the actual parameters.

1. Call by Value: In this method, the actual argument is copied to the formal parameter of the function. Hence any operation performed on the argument does not affect the actual parameters.

2. Call by Reference: Unlike call by value method, here the address of the actual parameter is passed to a formal parameter rather than a value. Hence any operation performed on formal parameters affects the value of actual parameters.

By default, C uses call by value to pass arguments.


MORE

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …