LERAN C PROGRAMMING

Learn one of the most powerful programming languages in the world and become a rockstar developer.

Functions

In this tutorial, you will be introduced to functions (both user-defined and standard library functions) in C programming. Also, you will learn why functions are used in programming.


function is a block of code that performs a particular task.

There are many situations where we might need to write same line of code for more than once in a program. This may lead to unnecessary repetition of code, bugs and even becomes boring for the programmer. So, C language provides an approach in which you can declare and define a group of statements once in the form of a function and it can be called and used whenever required.

These functions defined by the user are also know as User-defined Functions

 

C functions can be classified into two categories,

  1. Library functions
  2. User-defined functions

c function image


Library functions are those functions which are already defined in C library, example printf()scanf()strcat() etc. You just need to include appropriate header files to use these functions. These are already declared and defined in C libraries.

User-defined functions on the other hand, are those functions which are defined by the user at the time of writing program. These functions are made for code reusability and for saving time and space.

Benefits of Using Functions

  1. It provides modularity to your program's structure.
  2. It makes your code reusable. You just have to call the function by its name to use it, wherever required.
  3. In case of large programs with thousands of code lines, debugging and editing becomes easier if you use functions.
  4. It makes the program more readable and easy to understand.

User-defined function

You can also create functions as per your need. Such functions created by the user are known as user-defined functions.

Function Declaration

General syntax for function declaration is,

  returntype functionName(type1 parameter1, type2 parameter2,...);

Like any variable or an array, a function must also be declared before its used. Function declaration informs the compiler about the function name, parameters is accept, and its return type. The actual body of the function can be defined separately. It's also called as Function Prototyping. Function declaration consists of 4 parts.

  • returntype
  • function name
  • parameter list
  • terminating semicolon

returntype

When a function is declared to perform some sort of calculation or any operation and is expected to provide with some result at the end, in such cases, a return statement is added at the end of function body. Return type specifies the type of value(intfloatchardouble) that function is expected to return to the program which called the function.

Note: In case your function doesn't return any value, the return type would be void.


functionName

Function name is an identifier and it specifies the name of the function. The function name is any valid C identifier and therefore must follow the same naming rules like other variables in C language.


parameter list

The parameter list declares the type and number of arguments that the function expects when it is called. Also, the parameters in the parameter list receives the argument values when the function is called. They are often referred as formal parameters.

Advantages of user-defined function

  1. The program will be easier to understand, maintain and debug.
  2. Reusable codes that can be used in other programs
  3. A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers.


Previous Next
x
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine