In this chapter we will learn about functions.

What are functions in C?

Functions in c are block of code used to perform specific tasks. For example, printf() is a function used to print a message on consol.

C is a procedural language and Functions are very crucial part of it. Their are many built in functions to perform different tasks. For example the main() function, it is the entry point for the program. Every c program must have a main() function. When a program is executed, the compiler looks for the main() function and if it does not find the main() function then it will throw an error.

There are two types of functions -

Standard or built in functions

User defined function

Before understanding each function, lets understand the basic syntax of a function in c.

Syntax of function

return_type function_identifier(arguments)
    // function code
  • return_type -

    This is the data type of the value that the function will return like int, float, char etc.

  • function_identifier -

    This is the name of the function, it can be any valid identifier name.

  • arguments/parameter -

    Function arguments are also called as function parameters. these are the variable types that the function will use in the function body. There can be any number of arguments seperated by comma in the form of (data_type1 arg1, ... data_typeN argN). See the use of this in the example section of this chapter.

  • function code -

    This is the logic that a programmer write for the function. Whenever a function is called, function code is executed.

The function can be called any number of times with different values or same values.

Standard or built in functions

There are some standard functions that comes pre-defined. They are called bult in or pre-defined functions. Followings are some common functions and their uses -

  • main() -

    This is the entry point of the program, means this is the first funtion that is executed

  • printf() -

    Display message on console. For detail goto c_input_output.

  • scanf() -

    Accept input from console. For detail goto c_input_output.

The above functions are the standard functions that comes along with c.

User defined functions

Ofcourse C comes with lots of in built functions but thats not enough. Suppose we wish to add two integer numbers, for this task there is no pre defined function in c. In such case user defined functions plays very important role. It allows the programmer to write their own function with user specific logic.

We can write our own function to perform addition of two integer numbers as follows -

Function exampe
//User defined Function
int addition(int x, int y)
    return x + y;
void main() // main function
    int num1,num2,result;
    num1 = 10;
    num2 = 90;

    //function call
    result = addition(num1,num2);
    printf("Addition of %d + %d : %d \n", num1,num2,result);

    num1 = 20;
    num2 = 30;
    result = addition(num1,num2); // function call again
    printf("Addition of %d + %d : %d", num1,num2,result);
 Addition of 10 + 90 : 100
 Addition of 20 + 30 : 50

In the next chapter we will learn about Pointers.