Introduce with function in C language

A function in C can be said as a block of statement which performs a specific task. We use function to do the same task again and again without writing the code. What we have to do is just call the function. In this article we will learn about function in C programming.

Normally we use two type of function in C programming.

  • Library function
  • User defined function

Library functions in C

Library functions are the built in function in C programming. We need not defined them. They are declared inside the header files.

There are many library functions in C and we use them in every program. Some example of C library functions are as follows.

// some C library functions
printf();
scanf();
sqrt();
abs();
abort();
asin();
ceil();
floor();
free();
malloc();
calloc();
....................

If you want to know more about library function then you should see or C library function article.

User defined function in C

User defined functions are created by the programmers to do a specific task again and again inside the program. User defined function can reduce the complexity and optimize the code. A user can create number of functions he needs inside a C program.

Here are a simple syntax of user defined function in C.

// syntax of user defined function

#include <stdio.h>
return_type function_name(){
    // code to execute
    ... .. ...
}

int main(){
    // code inside main function
    ... .. ...

    function_name();    // calling the function

    // code to execute after function
    ... .. ...
}

Advantages of using function

There are many advantages of using a function inside our program. Some of them are as like as follows;

  • Function divides a program to some smaller part by which we can track a large program easily.
  • Function can call a number of times inside the same program.
  • We can avoid writing the same code again and again.
  • We can use one function as many times we need it.
  • Debugging of code would be easy when use several functions inside a program.
  • It reduces the size of program.

C program using function

Now, we will see a C program using function. Don’t worry if you don’t understand it. We have discussed more about function calling in our function call by value and function call by reference guide. Now lets see the program and try to understand about function parameter, return value, executed code etc.

Here in this program, we will find the largest number among three user input integers using function in C.

// C program using function
#include <stdio.h>

int find_large_number(int a, int b, int c){    // function definition
    if(a > b && a > c){
        return a;
    }else if(b > a && b > c){
        return b;
    }else{
        return c;
    }
}

int main(){
    int first, second, third;
    printf("Enter first number : ");
    scanf("%d", &first);
    printf("Enter second number : ");
    scanf("%d", &second);
    printf("Enter third number : ");
    scanf("%d", &third);

    int large = find_large_number(first, second, third);   // function calling

    printf("\n%d is the largest number.\n", large);

    return 0;
}

Output of function in C program:

c function, function in c, c function example

What is return type of a function?

Return type is the type of data which the function will give us executing the code inside it. Return type can be of any data types like int, float, double, void, char, short, long etc. Return type should be declared at the beginning of function.

// return type of a function

int print_age(int, int, int){   // return type is int
    ........
    ........
}

char find_vowel(){   // return type is char
    ........
    ........
}

double large_weight(){   // return type is double
    ........
    ........
}

Giving the name of a function in C

We  can give any name to a function without the keywords in C language. Other restriction of naming a function is as like as rules for naming a variable in C. However, it is recommended to give a meaningful name of function by which an user can easily understand what type of function it is.

// giving valid function name

int char(){   // error! char is a keyword
    ............
}

double 2nd_height(){   // error! using number at the beginning
    ............
}

Parameters or arguments of a function

We can pass one or more than one variable to a function in C. These are called argument or parameters. However, a function can work without any parameters also.

Parameter contains variables with their data types. These parameter works as the input of the function.

What is function definition?

Writing the body of a function is called function definition. The body of a function can contain some instruction to perform a specific task. However, the function body is the mandatory part of a function.

// function definition

float weight(int, int, int){   // This is function definition
    // function body
    ............
}

Calling a function in C

We have to call a function to execute the code inside it. If a function takes three parameters then we need to call it with three parameters. However we can also call a function by just its name followed by parenthesis for a function which need not any input arguments.

// function call in C

int myFuncOne(int, int){   // code to execute
    ............
}

int myFuncTwo(){   // code to execute
    ............
}

int main(){
    myFuncOne();       // invalid function calling, needs 2 argument
    myFuncOne(3, 5);   // valid calling
    myFuncTwo();       // valid calling
}

We will learn more about our next two article about call function by value and call function by reference. To go there simply click on next button bellow.