Introduction
to function in C
- NEED OF FUNCTION
- ADVANTAGE
- TYPES OF FUNCTION ASPECTS
- DIFFERENT ASPECTS OF FUNCTION CALLING
- C LIBRARY FUNCTION
- CALL BY VALUE
- CALL BY REFERENCE
- RECURSION
- RECURSIVE FUNCTION
- MEMORY ALLOCATION OF RECURSIVE FUNCTION
The basic building block of any C program is functions. A function is a group of statements that receives input, processes it, and then outputs the results. Their symbol is the curly bracket ({}). The ability to call functions repeatedly in C programming allows for more modularity and reuse. This means that you can call the function several times by simply putting in new arguments, saving you from having to write the same code for each input. Putting the code inside a function is one way.
Why do we need a function in C
Because
functions offer so many benefits to the developer, we require them in C
programming as well as other programming languages. The following are a few
main advantages of employing functions:
- Minimizes redundancy and permits reusability
- Creates a
modular code
- ·Offers
abstraction features
- ·The
software becomes simple to use and comprehend.
- ·Divides a complex program into manageable chunks
Advantage of function in C
Programming time can be saved by using functions instead of repeatedly writing the same logic or code.
· You can
call C functions from anywhere in your program, and you can do it as often as
you'd like.
· A large C
program that is split up into several functions is easy to manage.
· The main
accomplishment of C functions is their reusability.
· In C
applications, however, calling functions is wasteful by nature.
We may classify function aspects into three categories.
The C
function has three components.
Statement of purpose A function must be defined globally in C for the compiler to understand its name, parameters, and return type.
Call to function Within the program, functions can be invoked from any location. Function declaration and function calling cannot be different in the argument list. The number of functions that are declared in the function declaration must be passed.
Definition of function the actual assertions that need to be performed are contained in it. When the function is called, control is applied to the most crucial feature. It is important to note that the function can only return a single value at this point.
Syntax of
creating function in C
Reutn_type
function_name (data_type parameter)
{
\\ code to
be executed
}
Types of function
In C programming, functions come in two varieties:
Numerous methods, including scanf(), printf(), gets(), puts(), ceil(), and floor(), are specified in the C header files. These features are included in the library.
A user-defined function in C is one that the programmer creates for their own usage in the future. Large program code is optimized and made simpler by it.
A C function's return value isn't always guaranteed. If there is no requirement for the function to return any value, use void as the return type. To demonstrate this idea, let's look at a simple C function that outputs nothing.
Example without
return value:
Void
hello()
{
Printf(“hello
c”);
}
Any data type, including char, long, and int,
can be used to return any value from the function. The return type of a
function is determined by the value that it is expected to return.
This is a simple C function that returns an
integer value when given an integer parameter.
Example
with return value
Int get()
{
Return (value);
}
In the above example we can define any type
of data type in the return type whether it is integer or any thing but if we
have to pass the float value we have to define first the float and then we can
return the value.
For ex
Float get()
{
Return 10.2;
}
Now we have to call the function and get the
value.
There are different aspects of
function-calling
A function can accept or reject an argument.
It can return a value or not. Based on the above, there are four types of
function calls:
A function that does not require
parameters
Yields no value in return, as
well as one that does
Process that requests parameters
but yields no output
Execute a program with parameters
and display the outcomes
let us understand this by an example of a function
without argument and return value.
Now
let's understand this for function without argument and with return value
Let us understand this by with function and with return value
printf("\nEnter two numbers:");
C comes with a set of functions that are
stored in a library. With the help of these capabilities, you may accomplish
certain activities more easily. The (printf) library function, for instance,
allows you to output to the console.
Compiler designers create library functions.
Several header files that conclude in. h are used to define the functions that
comprise the C standard library. We must include the library functions defined
in these header files for our software to use them.
For example, we need to include stdio. h in
our application to use library functions like (printf) and (scanf). The
library's standard input/print functions are all contained in this header file.
Let us better understand this by the table
given below
Call by value in C
There
are two methods to pass the value into the function
1)
call by value
2)
call by reference
1) call by value
- Transforming the value of the real parameters into their formal counterparts is the essence of the call-by-value technique. Another way of putting it is that the value method call is where the function call makes use of the variable's value.
- A call-by-value method does not allow the use of
formal parameters to modify the value of actual parameters.
- Separate memory is allocated for
the two sorts of parameters because, in a call-by-value, the value of the real
parameter is copied to the formal parameter.
- In function calls, parameters are considered real, although
in function definitions, they are considered formal.
Let us better understand the concept of call by value in a C program
Call by reference in C
- In a comparison call, the address
of the variable is actually passed as an argument to the function call.
- Their addresses are included,
therefore the parameters are passed.
- Memory is allotted in the same
way for formal and actual parameters by reference. All function operations use
the value stored in the real parameter address; the updated value is likewise
recorded at the same address.
Let us try a C program having call by
reference
Difference between call by value and call by reference
Recursion in C language
When a function calls itself to solve a
lesser problem, it is called recursion. When a function calls itself, we say
that it is recursive. Another name for this type of call is "recursive
call.".
A recursion is a way of invoking itself
again. However, to end the loop, you must declare the condition. Recursive code
is both shorter and more complex than iterative code, which is lengthier and
easier to grasp.
Recursion works well for projects with linked
subtasks, albeit it isn't suitable for every task.
For instance, sorting, finding, and
navigation difficulties may all be tackled with recursion.
Recursion is usually less efficient than
iterative methods as the function call is always unnecessary.
Any problem with a recursive solution can
have iterative solutions. However, for some problems, such as factorization,
the Tower of Hanoi, and the Fibonacci sequence, recursion is the best approach.
Let us understand recursion by a simple C program
Recursive function
To complete an operation, a
recursive function breaks it down into smaller stages. One way a subtask might
perform a function is by adhering to an end condition. Once the function
returns the final result, the recursion is ended.
The recursive scenario depicts a function
that calls itself to perform a subtask, whereas the basic example describes a
function that does not iterate. This format works with all recursive functions.
The pseudocode for writing any recursive function is given below.
Let
us now understand this by a C program having Fibonacci series
Memory allocation of recursive function
With every iteration of this method, a fresh memory copy is produced.
After data is returned by the process, the copy is removed from memory. Since
the stack holds all of the variables and other items defined inside a function,
each recursive call maintains its own stack.
Every time a value is returned by the related function, the stack is
cleared. Recursion becomes extremely difficult when one considers the tracking
and resolution of the values of each recursive call. This makes stack
management and monitoring the values of the variables declared there very
important.
To have a better understanding of memory allocation in recursive functions, let's examine the following example
No comments:
Post a Comment