C Functions

In this tutorial, I'll answer the question: C functions. I'll also show you function declaration, function call in C and much more.

Let's get started.

C functions

As we learn before and try to write code sometime our code get bigger in number of lines and also increases in size to store. It is not possible for coder to track which line of code perform which task.

C function is a way to break the code into small pieces or chunks or group of line code whose aim is same. So that it is possible for coder or a person who writing the code and reuse in future as it needed. 

What is C functions

Functions is a block of code or piece of code which performs a particular task. functions can be reuse any number of time as it needed in the given program.

C functions

 

How to create a function in C

Now, we learn how to create a function in detail. To create a function you'll need three aspects of functions are:

  1. Function Declaration
  2. Function Definition
  3. Function Call

Function Declaration: It is also known as Function Prototyping. It is a way to tell the language compiler about the function name, function parameter, return type, etc. we are going to define and call in the program.

returnType functionName(type1 argument1, type2 argument2, ...);

Function Definition: It contains the line of instruction which are to be executed during the call of function.

returnType functionName(type1 argument1, type2 argument2, ...){

// lines of instruction

}

Function call: It is a way to tell the compiler to execute the function body at the time of call will made. The program execution start from the main function in the sequence the instruction of line written.

functionName(type1 argument1, type2 argument2, ...);

Each function in program was called directly or indirectly from the main() function.

Types of function

The two types of functions are present in C programming are:

  1. Standard Library Functions
  2. User Defined Functions 

Standard Library Functions: These functions are pre-built inside the C header files.

For example, The scanf() is a standard library function to take input from keyboard. This function are defined in stdio.h header files. So, To use scanf() function. we need to include the stdio.h file.

math(), floor(), ceil(), printf(), etc. are examples of built-in functions.

User Defined Functions: We can create our own functions as per our requirements and this types of functions are known as user definded functions.

Passing arguments to a function

Arguments are the variables passed to a function. In this, we can pass variables to functions and we get return variable from function.

The above example, We are create a prototype means that addFunction is a function which takes variables a and b of type integer and return c of type integer. 

NOTE: A function can return only one value at a time. 

What are the different types of functions in C  

The functions are taking arguments or not, return value or not on this basis. It was divided into four types are:

  1. Functions without arguments and without return value
  2. Functions without arguments and with return value
  3. Functions with arguments and without return value
  4. Functions with arguments and with return value

If you have any doubts, Please let me know

Previous Post Next Post

Contact Form