Functions in C
Functions in C
C enables its programmers to break up a program into segments commonly known as functions, each of which can be written more or less independently of the others. Every function in the program is supposed to perform a well-defined task. Therefore, the program code of one function is completely insulated from the other functions.
Every function interfaces to the outside world in terms of how information is transferred to it and how results generated by the function are transmitted back from it. This interface is basically specified by the function name.
main()
{
...........
...........
func1();
...........
...........
return ;
}
we can see that main() calls a function named func1(). Therefore, main() is known as the calling function and func1() is known as the called function. The moment the compiler encounters a function call, the control jumps to the statements that are a part of the called function. After the called function is executed, the control is returned to the calling program. The main() function can call as many functions as it wants and as many times as it wants. For example, a function call placed within a for loop, while loop, or do–while loop may call the same function multiple times till the condition holds true.
Why Functions are Needed
1.Dividing the program into separate well-defined functions facilitates each function to be written and tested separately. This simplifies the process of getting the total program to work.
2.Understanding, coding, and testing multiple separate functions is easier than doing the same for one big function.
3.If a big program has to be developed without using any function other than main(), then there will be countless lines in the main() function and maintaining that program will be a difficult task.
4.All the libraries in C contain a set of functions that the programmers are free to use in their programs. These functions have been pre-written and pre-tested, so the programmers can use them without worrying about their code details. This speeds up program development, by allowing the programmer to concentrate only on the code that he has to write.
5.Like C libraries, programmers can also write their own functions and use them from different points in the main program or any other program that needs its functionalities.
6.When a big program is broken into comparatively smaller functions, then different programmers working on that project can divide the workload by writing different functions.
Types of functions
There are mainly two types of functions these are:-
1.Predefined functions 2.User defined functions
Predefined functions
These functions are defined in the language compilers also known as system functions by developers or manufacturer’s examples are printf(), scanf(), sqrt() etc. There are a great varieties of libraries where thousands of functions are defined.
User defined functions-
These are the functions which are defined by programmer while coding or creating a program here programmer is you me or a learner.
Now we can classify functions based on their passing arguments these are passing by value and passing by reference.
Passing by value In these types of functions actual parameters are passed means the value of variables is passed. If we change these values in the calling function the change will not effect the values in the called function. like
function_name(variable1, variable2, ...);
Passing by reference Means when we passes the address of the variables, here if we change the values in calling function the change will be reflected in the called function automatically. In this we use & operator to pass the address of the variables. syntax
function_name(&variable1, &variable2, ...);
>
Using Functions
Working with functions includes three steps like:
1.Function declaration.
2.Function definition.
3.Function call.
Function declaration
This means whenever we uses a function we need to declare it before we use same as an variable is used but if we defines our function before main() function function declaration is not needed. Declaring a function means we makes compiler aware of that we are using function in our program. This is also known as function prototype.
The syntax for function prototype is
return data_type function_name(parimeter_list/argument_list);
Function definition
Function definition includes defining the behavour of function means what our function can do.
syntax for defining a function is :
return data_type function_name(data_type variable1, data_type variable2,..)
{
.............
statements
.............
return(variable);
}
Function call
Function call is the last step in modular programming which means that we calls our function form main() function
syntax for function call
function_name();
If function returns and passes values then the syntax will be
variable_name = function_name(variable1, variable2, ...);