Functions in C examples



Share



Learn function declaration, function call, types of functions, library & user defined functions, void, inline, recursive, reentrant functions in c with examples. Subscribe to enable complete access to all , and .

This article will help you to gain required knowledge to successfully complete , which are required to complete and earn course completion certificates for software jobs.

Function prototype, function definition, function call

Frequently asked topics, concepts.
  • What is a c programming function?
  •     C is a structured programming language. It divides the large problem into small modules called functions. Each function is written with a specific purpose. The basic purpose of writing functions is code re-usability. With functions it is easy to debug the program. Each function has three parts, function declaration/prototype, function definition, function call.
    A function is a set of statements that take inputs, do some specific computation and produces output.
    The idea is to put some commonly or repeatedly done task together and make a function, so that instead of writing the same code again and again for different inputs, we can call the function.
  • What is function prototype?
  •     Function prototype tells compiler about number of parameters function takes, data-types of parameters and return type of function. By using this information, compiler cross checks function parameters and their data-type with function definition and function call.
    If we ignore function prototype, program may compile with warning, and may work properly. But some times, it will give strange output and it is very hard to find such programming mistakes.
    Always declare a function prototype and its definition before it is called in the program.
  • What is function definition?
  •     A function definition actually defines the purpose for which the function is written. While writing a function definition we need to write the right arguments it takes as input and returns from it as output. Using the function input arguments logic is implemented inside the function definition.
    To illustrate the features of functions we have written a simple function addFunc.
    Program illustrating functions prototype, definition, call.
    
    #include <stdio.h>
    int addInt(int ,int );
    int main(){
      int a1 = 20,a2 = 30;
      printf("a1 + a2 = %d",addInt(a1,a2));
    }
    int addInt(int a,int b) {
      return a + b;
    }
    
    
  • function1.c program analysis
  •     Let's analyse function addFunc in function1.c. A function declaration is made to inform the compiler on type of arguments it takes, the type it returns. This information is provided in addFunc function prototype. What it says is addFunc function takes two integers as arguments and return an integer.
    In the addFunc function definition we wrote, when we add two integers we expect the result to be another integer. Hence we started with int data type. We named the function as addFunc. You can give any name to a function but it should reflect in some way the purpose for which it is written. Now in the arguments list addFunc function takes two integers which are included separated by a comma. Let's look at some very important rules to remember while writing a function definition.
    Following are the rules to follow while writing a function definition:
    • Inside a function definition only variables declared in function arguments or explicitly declared inside the function are accessible. So inside AddFunc function arguments a, b and explicitly declared variable r can only be accessed. If you try to use other variables inside the function addFunc, compiler will detect errors.
    • The variable returned from a function must match the data type of return variable. In addFunc case we know the result of two integers will be another integer. So the return data type of addFunc must be integer. We declared an integer variable r, stored the adding result of a and b, returned r from the function.
    • The data type of variable returned from function, used as function arguments must match as per the function prototype. In addFunc case the return type and function arguments as per prototype declared are int. Hence addFunc return data type is int and variables passed to the function are of int data type.
    • The number of arguments passed to the function in the definition and the order in which they are passed must match as per the function prototype. In addFunc case there are two int variables passed as arguments to the function addFunc as per its prototype. We have done exactly that while declaring the function addFunc.
        To use a function you must call it. If a function is not called the code inside the function definition is not executed. In our case to add two integers we must call addFunc. Following are the few rules to remember while calling a function.
    • The variable used to get output from the function should match the return data type of the function. So while calling addFunc function we used an integer variable "r" to get output from addFunc function.
    • The variables used as function arguments should match the variables that are passed while calling the function. So while calling addFunc, we have used two integer variables to pass to the function. Compiler will detect errors if the passed arguments data types do not match as per the defined function prototype.

    Library, User defined functions

    Frequently asked topics, concepts.
  • What are library functions?
  •     A function is called library function if it is already defined as part of any of the header file. If we want to use a function defined in any header file we just need to include the header file where the function is defined and then we need to call the function. In function1.c printf is already defined in stdio.h. Hence to include stdio.h is enough before calling it. We don't need to write the prototypes and definitions for these functions.
  • What are user defined functions?
  •     A function is called user defined function if it is not defined in any of the header file. User need to declare, define a user defined function before calling the function. In function1.c addInt, is an user defined function. Hence it is declared and defined separately before calling it from the main function.

    Startup, Cleanup functions

    Frequently asked topics, concepts.
  • What are startup and Cleanup functions?
  •     With GCC family of C compilers, we can mark some functions to execute before and after main(). So some startup code can be executed before main() starts, and some cleanup code can be executed after main() ends. For example, in the following program, myStartupFun() is called before main() and myCleanupFun() is called after main().
    Program illustrating startup and cleanup function.
    
    #include <stdio.h>
    
    /* Apply the constructor attribute to StartupFun() so that it
        is executed before main() */
    void StartupFun(void) __attribute__ ((constructor));
    
    
    /* Apply the destructor attribute to CleanupFun() so that it
       is executed after main() */
    void CleanupFun(void) __attribute__ ((destructor));
    
    
    /* implementation of myStartupFun */
    void StartupFun(void)
    {
        printf ("startup code before main()\n");
    }
    
    /* implementation of myCleanupFun */
    void CleanupFun(void)
    {
        printf ("cleanup code after main()\n");
    }
    
    int main()
    {
      printf("Here is my first statement\n");
      return 0;
    }
    
    

    return 0 vs exit(0)

    Frequently asked topics, concepts.
  • What is the difference between return 0 and exit(0)?
  •     return 0; is a statement that returns control back to the calling function. So functions called after the current function will continue to be executed. exit(0) is a system call which terminates the current process i.e the currently executing program. So any function called after the current function will not be executed.
    In main function return 0 or exit(0) are same but if you write exit(0) in different function then your program will exit from that position. Also returning different values from functions like return 1 or return -1 means that program is returning error.
    Program illustrating difference between return 0 and exit(0).
    
    #include <stdio.h>
    #include <stdlib.h>
    int func1();
    void func2();
    void func3();
    int main()
    {
      printf("Here is my main statement\n");
      func1();
      return 0;
    }
    int func1()
    {
      printf("Hello func1\n");
      func2();
      return 0;
    }
    void func2()
    {
      printf("Hello func2\n");
      exit(0);
      func3();
    }
    void func3()
    {
      printf("Hello func3\n");
    }
    
    

    To Read Full Article

    You need to subscribe & logged in

    Subscribe

    Exercises, Solutions

    Medium
    Low
    Low
    High
    Medium
    Medium
    Medium
    Low
    Low

    Want to contribute a new article? Write and upload your article information .
    Share

     Articles

     C Programming

     Linux Software

     Embedded Systems

     Python Programming

     Search Code Snippets

     Popular C search examples

     Popular C search MCQs

     Online certificate courses

     Test your skills:online tests

     C Programming

     Python Programming

     Linux Software

     Quantitative Aptitude

     Embedded System Software