0% found this document useful (0 votes)
28 views12 pages

C Theory - Unit 2.1

Uploaded by

pramodimmadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views12 pages

C Theory - Unit 2.1

Uploaded by

pramodimmadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Unit ii - FUNCTIONS

Dr. B D V Chandra Mohan Rao


Prof. of Civil Engg.
VNRVJIET
FUNCTIONS
• C Functions can be classified into two categories, namely, Library
functions and User defined functions.
• main is an example of user defined function and printf and scanf are the
examples of library functions.
• Library functions are available in C software, whereas User defined
functions are to be developed by the user.
• If only main function is used, the program may become too large and
complex and as a result, the task of debugging, testing and maintaining
becomes difficult.
• If a program is divided into functional parts, then each part may be
independently coded and later combined into a single unit. These
independently coded programs are called as Subprograms / Functions, that
are much easier to understand, debug and test.
Basic concept of functions
• C enables programmers to break up a program into segments, 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.
• From the figure, main ( ) calls a function named func1 ( ). main ( ) is
known as the calling function and func1 ( ) is known as the called
function.
• When 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.
Calling and called functions
Basic concept of functions
• It is not necessary that main ( ) function can call only one function, it can
call as many functions as it wants and as many times as it wants.
• 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.
• A function can call any other function. Next figure shows one function
calling another, and this function in turn calling some other function.
• 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. This approach is referred to as top-down
approach.
Functions Calling another functions
Calling function multiple times
Top-down approach
terminology
• A function f that uses another function g is known as the calling function
and g is known as the called function.
• The inputs that a function takes are known as arguments / parameters.
• When a called function returns some result back to the calling function, it is
said to return that result.
• The calling function may or may not pass parameters to the called
function.
• Function declaration is a declaration statement that identifies a function
with its name, a list of arguments that it accepts and the type of the data it
returns.
• Function definition consists of a function header that identifies the
function followed by the body of the function containing the executable
code for that function.
Function declaration
• The general format for declaring a function is
return_data_type function_name (data_type_var1, data_type_var2, ..);
function_name is a valid name for the function. Naming a function
follows the same rules as naming variables.
return_data_type specifies the data type of the value that will be
returned to the calling function as a result of the processing performed
by the called function.
data_type_var1, data_type_var2 … is a list of variables of specified
data types. These variables are passed from the calling function to the
called function. They are also known as arguments (or) parameters
that the called function accepts to perform its task.
Function definition
• A function can be defined either before (or) after main ( )
• When a function is defined, space is allocated for that function in the memory.
• A function definition comprises two parts i.e. Function Header and Function Body.
• The syntax of the function definition is
return_data_type function_name (data_type_var1, data_type_var2, ..)
{
-------------
statements
-------------
return (variable);
}
• return_data_type function_name (data_type_var1, data_type_var2, ..) is known as the
function header.
• The rest of the portion comprising of program statements within { --- } is the function body
which contains the code to perform the specific task.
Function definition
• The function header is same as function declaration. The only difference between
the two is that a function header is not followed by a semicolon ;
• The list of the variables in the function header is known as the formal parameter
list. The parameter list may have zero (or) more parameters of any data type.
• The function body contains instructions to perform the desired computation in a
function.
• The number of arguments and the order of arguments in the function header must
be same as that given in the function declaration statement.
• The parameter list in the function definition as well as function declaration must
match.
• The arguments in the function declaration and function definition need not be the
same. However, the data types of the arguments specified in function declaration
must match with that given in function definition.

You might also like