0% found this document useful (0 votes)
3 views13 pages

c Functions

The document explains the classification of C functions into library functions and user-defined functions, with examples of each. It emphasizes the need for user-defined functions to manage program complexity, facilitate debugging, and enhance code reusability. Additionally, it details the structure of functions, including their definition, declaration, and various categories based on arguments and return values.

Uploaded by

donmathew666666
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)
3 views13 pages

c Functions

The document explains the classification of C functions into library functions and user-defined functions, with examples of each. It emphasizes the need for user-defined functions to manage program complexity, facilitate debugging, and enhance code reusability. Additionally, it details the structure of functions, including their definition, declaration, and various categories based on arguments and return values.

Uploaded by

donmathew666666
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/ 13

INTRODUCTION

• C function can be classified into two categories, namely


(i) library functions
(ii) user-defined function.
• Main() is an example of user-defined functions.
• While printf() and scanf() belong to the category of library functions.
• The main distinction between these two categories is that library functions are
not required to be written by us whereas a user- defined function has to be
developed by the user at the time of writing a program.

2
NEED FOR USER-DEFINED
FUNCTION
• main() function indicates where the • So in such situation, we may repeat the
program has to begin its execution. program statements wherever they are
• While it is possible to code any program needed.
utilizing only main function but it leads
to a lot of problem. • Another approach is to design a function
• Problem is that the program may that can be called and used whenever
become too large and complex and as a required.
result the task of debugging, testing, and
maintaining becomes difficult. • This saves both time and space.
• If a program is divided into functional • programming using
parts, then each part may be function have the following advantage.
independently coded and later combined
into single unit. 1. The length of a source program can be
• These subprograms called ‘function’ are reduced by using function at appropriate
much easier to understand, debug and places.
test. 2. It is easy to locate and isolate a faulty
• There are times when certain type of function for further investigations.
operation or calculation is repeated at
many points throughout a program. 3. A function may be used by many other
programs.

3
A MULTIPLE-FUNCTION PROGRAM
main()
• A function is a self-contained code that {
--------
performs a particular task. --------
• Once a function has been designed , it can function1(); call
--------
be treated as a block-box that takes some --------
data from the main program and return a function2(); call
--------
value. --------
• Every c program can be designed using the function3(); call
--------
collection of these black boxes known as }
function. function1()
{
• Any function can call any other function. --------
--------
• It can call itself. }
• A called function can also can call another function2()
{
function. R --------
• A function can be called more than once. E --------
}
• The function can be placed in any order. T function13)
{
• A called function can be placed either
U --------
before or after the calling function. R --------
}
N

5
ELEMENTS OF USER-DEFINED
FUNCTION
• There are three elements of user- • The program that calls the function is
defined function. referred to as the calling program or
1. Function Definition calling function.
2. Function Call • The calling program should declare
3. Function Declaration any function that is to be used later in
the program.
• This is known as the function
• The function definition is an declaration or function prototype.
independent program module that is
specially written to implement the
requirement of the function
• In order to use this function we need
to invoke it at a required place in the
program. This is known as the
function call.

7
DEFINITION OF FUNCTIONS
• A function definition, also known as a general format of a function definition to
function implementation shall include implement these two parts is given below.
the following elements. function_type function_name(parameter list)
1. Function name {
local variable declaration;
2. Function type
executable statement1;
3. List of parameters
executable statement2;
4. Local variable declarations ………….
5. Function statements ………….
6. A return statement return statement;
}
• All the six elements are grouped into • The first line function_type
two parts, namely, function_name(parameter list) is known as
the function header and the statements
1. Function header (First three elements) within the opening and closing braces
2. Function body (Second three elements) constitute the function body, which is a
compound statement.

8
Function Header Formal Parameter List
• The function header consists of three • The parameter list declares the variables
parts: The function type(also known as that will receive the data sent by the
return type, the function name and the calling program.
formal parameter list. Note that a
• They serve as input data to the function to
semicolon is not used at the end of the
carry out the specified task.
function header.
• Since they represent actual input values,
Name and Type
they are often referred to as formal
• The function type specifies the type of parameters.
value that the function is expected to
• These parameters can also be used to send
return to the program calling the function.
values to the calling programs.
• If the return type is not explicitly
• These parameters are also known as
specified, c will assume that it is an
arguments.
integer type.
Examples
• If the function is not returning anything,
then we need to specify the return type as float quadratic(int a, int b, int c) { ….. }
void. • Remember, there is no semicolon after the
• Remember, void is one of the closing parenthesis.
fundamental data types in C. • Declaration of parameter variables cannot
• The function name is any valid C be combined.
identifier and therefore must follow the • That is, int sum(int a,b) is illegal.
same rules of formation as other variable • A function need not always receive values
names in C. from the calling program.
• The name should be appropriate to the • In such cases, functions have no formal 9
task performed by the function. parameters.
• To indicate that the parameter list is • The body enclosed in braces, contains three
parts, in the order given below:
empty, we use the keyword void 1. Local declarations that specify the variables
between the parentheses as in needed by the function.
void printline(void) 2. Function statements that perform the task of the
function.
{ 3. A return statement that returns the value
………… evaluated by the function.
• If a function does not return any value we can
} omit the return statement.
• This function neither receives any input • However note that its return type should be
specified as void.
values nor returns back any value.
• Again it is nice to have a return statement
• Many compilers accept an empty set of even for void functions.
parentheses, without specifying Return Values And Their Types
anything as in • If a function return value then it is done
through the return statement.
void printline() • While it is possible to pass to the called
function any number of values, the called
• But, it is a good programming style to function can only return one value per call, at
use void to indicate a null parameter list. the most.
Function Body • The return statement can take one of the
following forms
• The function body contains the return;
declarations and statement necessary for or
performing the required task. return(expression);

10
• The first, the ‘plain’ return does not Function Calls
return any value; it acts much as the • A function can be called by simply
closing brace of the function. using the function name followed by
• When a return is encountered, the a list of actual parameters , if any,
control is immediately passed back to enclosed in parentheses.
the calling function. Example
main()
Example {
int mul (int x, int y) int y;
{ y=mul(10,5);
int result; printf(“%d\n”,y);
result =x*y; }
return result; • When the compiler encounters a
} function call, the control is transferred
to the function mul().
• It return the value of p which is the
product of the values of x and y, and • This function is then executed line by
the type of return value is int. line as described and a value is
returned
• when a return statement is
encountered.
• This value is assigned to y.

11
• A function call is a postfix
expression. The operator (..)is at a Note:
very high level of precedence.
• Therefore, when a function call is 1. If the actual parameters are more than
used as a part of an expression, it will the formal parameters, the extra
be evaluated first, unless parentheses actual arguments will be discarded.
are used to change the order of 2. On the other hand, if the actuals are
precedence. less than the formals, the unmatched
• In a function call, the function name formal arguments will be initialized to
is the operand and the parentheses set some garbage.
which contains the actual parameters 3. Any mismatch in data types may also
are the operator. result in some garbage values.
• The actual parameters must match the
function’s formal parameters in type,
order and number.
• Multiple actual parameters must be
separated by commas.

12
FUNCTION DECLARATION
A function declaration consists of four Example
parts. void main()
• Function type {
• Function name int sum(int a,int b);
• Parameter list --------
• Terminating semicolon --------
Syntax }
Function-type function-name int sum(int a, int b)
(parameter list); {
• The function declaration appear in the int c;
main function. c=a+b;
• Here function-type indicate the type return(c);
of value that the function return.
• Function-name is name of the }
function.
• Parameter list is an arguments list.

13
Points to note
1. The parameter list must be separated by commas.
2. The parameter names do not need not to be the same in the prototype declaration
and the function definition.
3. The types must match the types of parameters in the function definition, in number
and order.
4. Use of parameter names in the declaration is optional.
5. If the function has no formal parameters then the list is written as void.
6. The return type is optional, when the functions returns int type data.
7. The return type must be void if no value is returned.
8. When the declared types do not match with the types in the function definition,
compiler will produce an error.

Example

int mul(int, int);


int mul(int a, int b); all are equally acceptable statements
mul(int a, int b);
mul (int, int);

14
Prototype : yes or no Parameters Everywhere
• Prototype declarations are not Parameters (arguments0 are used in the
essentials. following places:
• If a function has not been declared 1. in declaration (prototype)
before it is used, C will assume that --- formal parameters
its details available at the time of 2. in function call
linking.
• Since the prototype is not available, C ---actual Parameters
will assume that the return type is an 3. in function definition
integer and that the types of ---formal Parameters
parameters match the formal • The actual Parameters used in calling
definition. statement may be simple constants,
• If these assumptions are wrong, the variables or expressions.
linker will fail and we will have to • The formal and actual Parameters
change the program. must match exactly in type, order and
number.
• Their names do not need to match.

15
CATEGORY OF FUNCTIONS

• A function, depending on whether arguments are present or not and


whether a value is returned or not, may belong to one of the following
categories.
Category 1: Function with no arguments and no return Values.
Category 2: Function with arguments and return values.
Category 3: Function with arguments and one return values.
Category 4: Function with no arguments and return values.
Category 5: Function that return multiple values.

16

You might also like