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, these 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.


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:

In the above example the declaration is stated as:

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 value. Hence any operation performed on formal parameters affects the value of actual parameters.

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


Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More