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

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More