Functions in C
Functions in C
1. Library functions
2. User-defined functions
Library functions are those functions which are already defined in C
library, example printf(),scanf(),strcat() etc. You just need to include
appropriate header files to use these functions. These are already
declared and defined in C libraries.
2. It makes your code reusable. You just have to call the function by its
name to use it, wherever required.
Function Declaration
General syntax for function declaration is,
Like any variable or an array, a function must also be declared before its
used. Function declaration informs the compiler about the function
name, parameters is accept, and its return type. The actual body of the
function can be defined separately. It's also called as Function
Prototyping.
Function declaration consists of 4 parts:
Return type
function name
parameter list
terminating semicolon
Return type:
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.
Return type specifies the type of value(int, float, char, double) that
function is expected to return to the program which called the function.
Note: In case your function doesn't return any value, the return type
would be void.
Function Name:
The parameter list declares the type and number of arguments that the
function expects when it is called. Also, the parameters in the
parameter list receives the argument values when the function is called.
They are often referred as formal parameters.
Let's write a simple program with a main() function, and a user defined
function to multiply two numbers, which will be called from
the main() function.
Function definition Syntax
Just like in the example above, the general syntax of function definition
is,
Function Body:
Calling a Function:
In the example above, the statement multiply(i, j); inside the main()
function call.
Arguments are the values specified during the function call, for which
the formal parameters are declared while defining the function.
It is possible to have a function with parameters but no return type. It is
not necessary, that if a function accepts parameter(s), it must return a
result too.
While declaring the function, we have declared two
parameters a and b of type int. Therefore, while calling that function,
we need to pass two arguments, else we will get compilation error. And
the two arguments passed should be received in the function definition,
which means that the function header in the function definition should
have the two parameters to hold the argument values. These received
arguments are also known as formal parameters. The name of the
variables while declaring, calling and defining a function can be
different.
A function may or may not return a result. But if it does, we must use
the return statement to output the result. return statement also ends
the function execution, hence it must be the last statement of any
function. If you write any statement after the return statement, it won't
be executed.
The datatype of the value returned using the return statement should
be same as the return type mentioned at function declaration and
definition. If any of it mismatches, you will get compilation error.
In the next tutorial, we will learn about the different types of user
defined functions in C language and the concept of Nesting of functions
which is used in recursion.
This is the best type, as this makes the function completely independent
of inputs and outputs, and only the logic is defined inside the function
body.
Nesting of Function
If function2() also has a call for function1() inside it, then in that case, it
will lead to an infinite nesting. They will keep calling each other and the
program will never terminate.
Lets consider that inside the main() function, function1() is called and its
execution starts, then inside function1(), we have a call for function2(),
so the control of program will go to the function2(). But as function2()
also has a call to function1() in its body, it will call function1(), which will
again call function2(), and this will go on for infinite times, until you
forcefully exit from program execution.
Recursion
The function does not have any arguments, then to call a function you
can directly use its name. But for functions with arguments, we can call
a function in two different ways, based on how we specify the
arguments, and these two ways are:
1. Call by Value
2. Call by Reference
Call by Value:
In this case, the actual variable x is not changed. This is because we are
passing the argument by value, hence a copy of x is passed to the
function, which is updated during function execution, and that copied
value in the function is destroyed when the function ends(goes out of
scope). So the variable x inside the main() function is never changed and
hence, still holds a value of 10.
But we can change this program to let the function modify the
original x variable, by making the function calc() return a value, and
storing that value in x.
Call by Reference: