Functions in C Programming Language

Functions:

Functions
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 use the functions.

Example:-

Advantage Of Functions:-
  • It provides modularity to our program.
  • It makes our code reusable. We can call the function by its name to use it, wherever need
  • In case of large programs, debugging and editing becomes easier if we use functions. 
  • It makes the program more readable and easy to understand. 

Type Of Functions:-

There are two types of function in C programming language, namely
  • Built-in Functions
  • User-defined Functions

1. Standard library functions (built-in functions):-

The standard library functions are built-in functions in C programming to handle tasks such as mathematical computations, I/O processing, string handling etc.
Read Also:
These functions are defined in the header file. When we include the header file, these functions are available for use.
For example: printf() is a standard library function to send formatted output to the screen (display output on the screen).
This function is defined in "stdio.h" header file.
There are other numerous library functions defined under "stdio.h", such as scanf), printf), getchar) etc. Once we include "stdio.h" in our program, all these functions are available for use.

2. User-defined functions:-

The functions created by the user are called user-defined functions, depending upon the complexity and requirement of the program, we can create as many user-defined functions as we want.
  1. Structures
  2. Union
  3. Pointers
The execution of a C program begins from the main() function, When the compiler encounters functionName(); inside the main function, control of the program jumps to void functionName() as shown below:
User defined functions

Type Of User-defined Functions:-

A user-defined function, depending on whether arguments are present or not and whether a value is returned or not, may belong to one of the following categories:-
  • Function with arguments but no return value:- Here the called function receives the data from the calling function. The arguments and parameters should match in number, data type and order.But, the called function does not return any value back to the calling function. Instead, it prints the data in its scope only. It is a one way data communication between the calling function and the called function.
  • Function with no arguments and return value:- Here, the called function does not receive any data from the calling function, It manages with its local data to carry out the specified task. However, after the specified processing, the called function returns the computed data to the calling function. It is also a one way data communication between the calling function and the called function. 
  • Functions with arguments and one return value:- The function value receives data from the calling function through arguments, but does not send back any value. Rather, it displays the results of calculation at the terminal. However, we may not always wish to have the result of a function displayed. We may use it in the calling function for further processing. Moreover, to assure a high degree of portability between programs, function should generally be coded without involving any I/O operations. 
  • Funetions with no arguments and no return value:- When a function has no arguments, it does not receive any data from calling function. In effect, there is no data transfer between calling function and called function.Here, the called function does not receive any data from the calling function. And, it does not return any data back to the calling function. Hence, there is no data transfer between the calling function and the called function. 
Advantages of user-defined function:-
  • The program will be easier to understand, maintain and debug. 
  • Reusable codes that can be used in other programs. 
  • A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers.

Functions Declaration:-

General syntax for function declaration is,
returntype functionName(typel parameter1, type2 parameter2,..); 
Like any variable or an array, a function must also be declared before it Answer. Function declaration informs the compiler about the function name, parameters it accepts, and its return type. The actual body of the function can be defined separately. It is also called as function prototyping.
Function declaration consists of following four parts:-
  • 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. returntype specifies the type of value(int, float, char, double) that function is expected to return to the program which called the function.
  • FunctionName:- functionName is an identifier and it specifies the name of the funetion. The functionName is any valid C identifier and therefore must follow the same naming rules like other variables in C language. 
  • Terminating semicolon:- The semicolon is a statement terminator in C to help the parser figure out where the statement ends.
  • Parameter list:- The parameter list declares the type and number of arguments that the function expects when it is called. The parameters in the parameter list receive the argument values when the function is called. They are often referred as formal parameters.
There are two types of parameters and they are as follows:-
  • Actual parameters:- The actual parameters are the parameters that are specified in calling function .
  • Formal parameters: The formal parameters are the parameters that are declared at called funetion. When a function gets executed, the copy of actual parameter values is copied into formal parameters. Parameter passing mechanism: It is a mechanism of passing values from one function to another function.
There are two methods to pass parameters from calling function to called function and they are as follows:
1.Call by value: When the value of arguments is passed from the calling function to the called function, the values are copied into the calle function.It any changes are made to the values in the called function, then there is no change in the original values within the calling function.
For Example:

Here variable 'a' and 'b' have been passed to the function 'add' in the program, 'a' and 'b' are copied to the values ​​'x' and 'y' variable.
2. Call by reference:- In this method, the actual values are not passed, instead their addresses are passed. There is no copying of values since their memory locations are referenced. If any modification is made to the values in the called function, the original values get changed within the calling function.
For example:

Here the variable 'a' and 'b' address are passed to the function 'add' in the program,  'a' and 'b' are not copied to the values ​​'x' and 'y' variable.
It only holds the address hold of the variables.

Post a Comment

1 Comments