When you divide a large program into various functions, it becomes easy to manage each function individually. Whenever an error occurs in the program, you can easily investigate faulty functions and correct only those errors. You can easily call and use functions whenever they are required which automatically leads in saving time and space. In this tutorial, you will learn-
Library Vs. User-defined Functions
Function Declaration
Function definition
Function call
Function Arguments
Variable Scope
Static Variables
Recursive Functions
Inline Functions
Library Vs. User-defined Functions
Every ‘C’ program has at least one function which is the main function, but a program can have any number of functions. The main () function in C is a starting point of a program. In ‘C’ programming, functions are divided into two types:
Library functions User-defined functions
The difference between the library and user-defined functions in C is that we do not need to write a code for a library function. It is already present inside the header file which we always include at the beginning of a program. You just have to type the name of a function and use it along with the proper syntax. Printf, scanf are the examples of a library function. Whereas, a user-defined function is a type of function in which we have to write a body of a function and call the function whenever we require the function to perform some operation in our program. A user-defined function in C is always written by the user, but later it can be a part of ‘C’ library. It is a major advantage of ‘C’ programming. C programming functions are divided into three activities such as,
Function declaration Function definition Function call
Function Declaration
Function declaration means writing a name of a program. It is a compulsory part for using functions in code. In a function declaration, we just specify the name of a function that we are going to use in our program like a variable declaration. We cannot use a function unless it is declared in a program. A function declaration is also called “Function prototype.” The function declarations (called prototype) are usually done above the main () function and take the general form:
The return_data_type: is the data type of the value function returned back to the calling statement. The function_name: is followed by parentheses Arguments names with their data type declarations optionally are placed inside the parentheses.
We consider the following program that shows how to declare a cube function to calculate the cube value of an integer variable Keep in mind that a function does not necessarily return a value. In this case, the keyword void is used. For example, the output_message function declaration indicates that the function does not return a value: void output_message();
Function Definition
Function definition means just writing the body of a function. A body of a function consists of statements which are going to perform a specific task. A function body consists of a single or a block of statements. It is also a mandatory part of a function.
Function call
A function call means calling a function whenever it is required in a program. Whenever we call a function, it performs an operation for which it was designed. A function call is an optional part of a program. Here, is th complete code: Output:
Function Arguments
A function’s arguments are used to receive the necessary values by the function call. They are matched by position; the first argument is passed to the first parameter, the second to the second parameter and so on. By default, the arguments are passed by value in which a copy of data is given to the called function. The actually passed variable will not change. We consider the following program which demonstrates parameters passed by value: The program output is: Keep in mind that the values of a and b were passed to add function were not changed because only its value was passed into the parameter x.
Variable Scope
Variable scope means the visibility of variables within a code of the program. In C, variables which are declared inside a function are local to that block of code and cannot be referred to outside the function. However, variables which are declared outside all functions are global and accessible from the entire program. Constants declared with a #define at the top of a program are accessible from the entire program. We consider the following program which prints the value of the global variable from both main and user defined function : Result: We discuss the program details:
We declare an integer global variable with 1348 as initial value. We declare and define a test() function which neither takes arguments nor returns a value. This function only prints the global variable value to demonstrate that the global variables can be accessed anywhere in the program. We print the global variable within the main function. We call the test function in order to print the global variable value.
In C, when arguments are passed to function parameters, the parameters act as local variables which will be destroyed when exiting the function. When you use global variables, use them with caution because can lead to errors and they can change anywhere in a program. They should be initialized before using.
Static Variables
The static variables have a local scope. However, they are not destroyed when exiting the function. Therefore, a static variable retains its value forever and can be accessed when the function is re-entered. A static variable is initialized when declared and needs the prefix static. The following program uses a static variable: The program displays :
Recursive Functions
Consider the factorial of a number which is calculated as follow 6! =6* 5 * 4 * 3 * 2 * 1. This calculation is done as repeatedly calculating fact * (fact -1) until fact equals 1. A recursive function is a function which calls itself and includes an exit condition in order to finish the recursive calls. In the case of the factorial number calculation, the exit condition is fact equals to 1. Recursion works by “stacking” calls until the exiting condition is true. For example: The program displays: Here, we discuss program details:
We declare our recursive factorial function which takes an integer parameter and returns the factorial of this parameter. This function will call itself and decrease the number until the exiting, or the base condition is reached. When the condition is true, the previously generated values will be multiplied by each other, and the final factorial value is returned. We declare and initialize an integer variable with value”6″ and then print its factorial value by calling our factorial function.
Consider the following chart to more understand the recursive mechanism which consists of calling the function its self until the base case or stopping condition is reached, and after that, we collect the previous values:
Inline Functions
Function in C programming is used to store the most frequently used instructions. It is used for modularizing the program. Whenever a function is called, the instruction pointer jumps to the function definition. After executing a function, instruction pointer falls back to the statement from where it jumped to the function definition. Whenever we use functions, we require an extra pointer head to jump to the function definition and return to the statement. To eliminate the need of such pointer heads, we use inline functions. In an inline function, a function call is directly replaced by an actual program code. It does not jump to any block because all the operations are performed inside the inline function. Inline functions are mostly used for small computations. They are not suitable when large computing is involved. An inline function is similar to the normal function except that keyword inline is place before the function name. Inline functions are created with the following syntax: Let us write a program to implement an inline function. Output: Above program demonstrates the use of an inline function for addition of two numbers. As we can see, we have returned the addition on two numbers within the inline function only without writing any extra lines. During function call we have just passed values on which we have to perform addition.
Summary
A function is a mini-program or a subprogram. Functions are used to modularize the program. Library and user-defined are two types of functions. A function consists of a declaration, function body, and a function call part. Function declaration and body are mandatory. A function call can be optional in a program. C program has at least one function; it is the main function (). Each function has a name, data type of return value or a void, parameters. Each function must be defined and declared in your C program. Keep in mind that ordinary variables in a C function are destroyed as soon as we exit the function call. The arguments passed to a function will not be changed because they passed by value none by address. The variable scope is referred to as the visibility of variables within a program There are global and local variables in C programming