0% found this document useful (0 votes)
4 views6 pages

Function Tutorial

The document provides an overview of functions in C programming, explaining their declaration, calling, and definition, along with the distinction between library functions and user-defined functions. It also covers the concepts of call by value and call by reference, as well as recursion, highlighting their uses and memory allocation implications. Additionally, it lists various C standard library header files and their associated functions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Function Tutorial

The document provides an overview of functions in C programming, explaining their declaration, calling, and definition, along with the distinction between library functions and user-defined functions. It also covers the concepts of call by value and call by reference, as well as recursion, highlighting their uses and memory allocation implications. Additionally, it lists various C standard library header files and their associated functions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C Functions

In c, we can divide a large program into the basic building blocks


known as function. The function contains the set of programming
statements enclosed by {}. A function can be called multiple times to
provide reusability and modularity to the C program. In other
words, we can say that the collection of functions creates a program.
The function is also known as procedureor subroutinein other
programming languages.

There are three aspects of a C function.

 Function declaration A function must be declared globally in a


c program to tell the compiler about the function name,
function parameters, and return type.

 Function call Function can be called from anywhere in the


program. The parameter list must not differ in function calling
and function declaration. We must pass the same number of
functions as it is declared in the function declaration.

 Function definition It contains the actual statements which are


to be executed. It is the most important aspect to which the
control comes when the function is called. Here, we must notice
that only one value can be returned from the function.
There are two types of functions in C programming:

1. Library Functions: are the functions which are declared in the


C header files such as scanf(), printf(), gets(), puts(), ceil(),
floor() etc.
2. User-defined functions: are the functions which are created by
the C programmer, so that he/she can use it many times. It
reduces the complexity of a big program and optimizes the
code.
3. Return Value
A C function may or may not return a value from the function.
If you don't have to return any value from the function, use
void for the return type.

A function may or may not accept any argument. It may or may not
return any value. Based on these facts, There are four different
aspects of function calls.

 function without arguments and without return value


 function without arguments and with return value
 function with arguments and without return value
 function with arguments and with return value

C Library Functions

Library functions are the inbuilt function in C that are grouped and
placed at a common place called the library. Such functions are used
to perform some specific operations. For example, printf is a library
function used to print on the console. The library functions are
created by the designers of compilers. All C standard library
functions are defined inside the different header files saved with the
extension .h.
[Link].h-This is a standard input/output header file. It contains all the
library functions regarding standard input/output.
2. conio.h-This is a console input/output header file.
3. string.h-It contains all string related library functions like gets(),
puts(),etc.
4. math.h-This header file contains all the math operations related
functions like sqrt(), pow(), etc.
5. stdlib.h-This header file contains all the general library functions
like malloc(), calloc(), exit(), etc.
6. time.h-This header file contains all the time-related functions.
7. ctype.h-This header file contains all character handling functions.
8. signal.h-All the signal handling functions are defined in this header
file.
Call by value and Call by reference in C

Call by value in C

 In call by value method, the value of the actual parameters is


copied into the formal parameters. In other words, we can say
that the value of the variable is used in the function call in the
call by value method.
 In call by value method, we can not modify the value of the
actual parameter by the formal parameter.
 In call by value, different memory is allocated for actual and
formal parameters since the value of the actual parameter is
copied into the formal parameter.
 The actual parameter is the argument which is used in the
function call whereas formal parameter is the argument which
is used in the function definition.

Call by reference in C

 In call by reference, the address of the variable is passed into the


function call as the actual parameter.
 The value of the actual parameters can be modified by changing
the formal parameters since the address of the actual
parameters is passed.
 In call by reference, the memory allocation is similar for both
formal parameters and actual parameters. All the operations in
the function are performed on the value stored at the address of
the actual parameters, and the modified value gets stored at the
same address.

Recursion in C

Recursion is the process which comes into existence when a function


calls a copy of itself to work on a smaller problem. Any function
which calls itself is called recursive function, and such function calls
are called recursive calls. Recursion involves several numbers of
recursive calls. However, it is important to impose a termination
condition of recursion. Recursion code is shorter than iterative code
however it is difficult to understand.

Recursion cannot be applied to all the problem, but it is more useful


for the tasks that can be defined in terms of similar subtasks. For
Example, recursion may be applied to sorting, searching, and
traversal problems.

Generally, iterative solutions are more efficient than recursion since


function call is always overhead. Any problem that can be solved
recursively, can also be solved iteratively. However, some problems
are best suited to be solved by the recursion, for example, tower of
Hanoi, Fibonacci series, factorial finding, etc.

Memory allocation of Recursive method

Each recursive call creates a new copy of that method in the


memory. Once some data is returned by the method, the copy is
removed from the memory. Since all the variables and other stuff
declared inside function get stored in the stack, therefore a separate
stack is maintained at each recursive call. Once the value is returned
from the corresponding function, the stack gets destroyed.
Recursion involves so much complexity in resolving and tracking the
values at each recursive call. Therefore we need to maintain the
stack and track the values of the variables defined in the stack.

You might also like