C enables 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.
Unit III :
Functions and Pointer
CHAPTER
7 : FUNCTIONS
Takeaways
•
Using functions
•
Function declaration
•
Function definition
•
Function call
•
Call-by-value and call-by-reference
•
Scope of variables
•
Storage classes
•
Recursive functions
• Tower of Hanoi
INTRODUCTION
C
enables 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 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 it are transmitted back. This
interface is specified by the function name. For example, look at Figure 4.1
which explains how the main() function calls another function to perform a
well-defined task.
From
the figure 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, instead of executing the next statement in the
calling function, the control jumps to the statements that are part of the
called function. After the called function is executed, the control is returned
back to the calling function.
It
is not necessary that the main() function can call only one function, it 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 can call the same function multiple times until
the condition holds true.
Another
point is that it is not only the main() function that can call other functions.
A function can call any other function. For example, look at Figure 4.2 which
shows one function calling another, and this function in turn calling some
other function. From this we see that every function encapsulates a set of
operations and when called it returns information to the calling function.
Why are Functions Needed?
Let
us analyse the reasons for segmenting a program into manageable chunks as it is
an important aspect of programming.
•
Dividing a 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. Figure 4.3 shows that the main() function calls other functions
for dividing the entire code into smaller sections (or functions). This
approach is referred to as the top-down
approach.
•
Understanding, coding, and testing multiple separate functions are far easier
than doing it for one big function.
•
If a big program has to be developed without the use of any function other than
main() function, then there will be countless lines in the main() function and
maintaining this program will be very difficult. A large program size is a
serious issue in micro-computers where memory space is limited.
•
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.
• When a big program is broken into comparatively smaller functions, then different programmers working mon that project can divide the workload by writing to different functions.
•
Like C libraries, programmers can also write their functions and use them at
different points in the main program or in any other program that needs its
functionalities.
Consider
a program that executes a set of instructions repeatedly n times, though not
continuously. In case the instructions had to be repeated continuously for n
times, they can be placed within a loop. But if these instructions have to be
executed abruptly from anywhere within the program code, then instead of
writing these instructions wherever they are required, a better idea is to
place these instructions in a function and call that function wherever
required. Figure 4.4 explains this concept.
Programming in C: Unit III (a): Functions : Tag: : with Example C Programs - Introduction to Functions
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation