0% found this document useful (0 votes)
16 views15 pages

Csir11-Introduction To Computer Programming: Unit Iii

The document discusses modular programming and functions in C programming. It defines modular programming as breaking a large program into smaller, self-contained components called functions. Functions allow avoiding redundant code and building reusable libraries. The document then describes how to define and access functions, including specifying return types, arguments, and returning values. It emphasizes that functions should perform well-defined tasks and code consistency between function definitions and calls.

Uploaded by

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

Csir11-Introduction To Computer Programming: Unit Iii

The document discusses modular programming and functions in C programming. It defines modular programming as breaking a large program into smaller, self-contained components called functions. Functions allow avoiding redundant code and building reusable libraries. The document then describes how to define and access functions, including specifying return types, arguments, and returning values. It emphasizes that functions should perform well-defined tasks and code consistency between function definitions and calls.

Uploaded by

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

CSIR11-INTRODUCTION TO

COMPUTER PROGRAMMING
UNIT III
(Modular Programming- Functions)

DATE: 03/10/2023
Modular programming
• The use of programmer-defined functions allows a large program to be
broken down into a number of smaller, self-contained components, each
of which has some unique, identifiable purpose.
• Thus a C program can be modularized through the intelligent use of such
functions.
• The use of a function avoids the need for redundant (repeated)
programming of the same instructions.
• The use of functions also enables a programmer to build a customized
library of frequently used routines or of routines containing system-
dependent features. Each routine can be programmed as a separate
function and stored within a special library file.
Modular programming
• A function is a self-contained program segment that carries out some specific, well-defined task.
• Every C program consists of one or more functions. One of these functions must be called main.
• Execution of the program will always begin by carrying out the instructions in main.
• Additional functions will be subordinate to main, and perhaps to one another.
• If a program contains multiple functions, their definitions may appear in any order, though they must be
independent of one another. That is, one function definition cannot be embedded within another.
• A function will carry out its intended action whenever it is accessed (i.e., whenever the function is "called") from
some other portion of the program. The same function can be accessed from several different places within a
program. Once the function has carried out its intended action, control will be returned to the point from which
the function was accessed.
• Generally, a function will process information that is passed to it from the calling portion of the program, and
return a single value.
• Information is passed to the function via special identifiers called arguments (also called parameters), and
returned via the return statement.
• Some functions, however, accept information but do not return anything , whereas other functions return multiple
values.
Modular programming
Functions
• DEFINING A FUNCTION
• A function definition has two principal components:
• the first line (including the argument declarations), and the body of the
function.
• The first line of a function definition contains the type specification of the
value returned by the function, followed by the function name, and
(optionally) a set of arguments, separated by commas and enclosed in
parentheses.
• Each argument is preceded by its associated type declaration.
• An empty pair of parentheses must follow the function name if the function
definition does not include any arguments.
Functions
• where data - type represents the data type of the item that is returned by the function, name represents the
function name, and type 1,type 2, .. . , type n represent the data types of the arguments arg 1, arg 2, . . . , arg n.

• The data types are assumed to be of type int if they are not shown explicitly. However, the omission of the data
types is considered poor programming practice, even if the data items are integers.

• The arguments are called formal arguments, because they represent the names of data items that are
transferred into the function from the calling portion of the program. They are also known as parameters or
formal parameters. (The corresponding arguments in the function reference are called actual arguments, since
they define the data items that are transferred. Some textbooks refer to actual arguments simply as arguments,
or as actual parameters.)

• The identifiers used as formal arguments are "local" in the sense that they are not recognized outside of the
function. Hence, the names of the formal arguments need not be the same as the names of the actual
arguments in the calling portion of the program. Each formal argument must be of the same data type,
however, as the data item it receives from the calling portion of the program.
Functions
• The remainder of the function definition is a compound statement that defines the
action to be taken by the function. This compound statement is sometimes referred to
as the body of the function.
• Like any other compound statement, this statement can contain expression statements,
other compound statements, control statements, and so on. It should include one or
more return statements, in order to return a value to the calling portion of the program.
• A function can access other functions.
• In fact, it can even access itself (this process is known as recursion)
• Information is returned from the function to the calling portion of the program via the
return statement. The return statement also causes the program logic to return to the
point from which the function was accessed.
• return expression;
Functions
• The value of the expression is returned to the calling portion of the
program.
• The expression is optional. If the expression is omitted, the return
statement simply causes control to revert back to the calling portion of the
program, without any transfer of information.
• Only one expression can be included in the return statement. Thus, a
function can return only one value to the calling portion of the program via
return.
• A function definition can include multiple return statements, each
containing a different expression.
• Functions that include multiple branches often require multiple returns.
Functions
Functions
• The return statement can be absent altogether fiom a function definition, though this
is generally regarded as poor programming practice.
• If a function reaches the end without encountering a return statement, control simply
reverts back to the calling portion of the program without returning any information.
• The presence of an empty return statement (without the accompanying expression)
is recommended in such situations, to clarify the logic and to accommodate future
modifications to the function.
• If the data type specified in the first line is inconsistent with the expression appearing
in the return statement, the compiler will attempt to convert the quantity
represented by the expression to the data type specified in the first line.
• This could result in a compilation error, or it may involve a partial loss of data (e.g.,
due to truncation). In any event, inconsistencies of this type should be avoided.
Functions

• The function expects to return an ordinary integer quantity, since there is no explicit type declaration in the
first line of the function definition.
• However, the quantity being returned (prod) is declared as a long integer within the function.
• This inconsistency can result in an error. (Some compilers will generate a diagnostic error and then stop
without completing the compilation.)
• The problem can be avoided, however, by adding a long int type declaration to the first line of the function
definition,
Functions
• The keyword void can be used as a type specifier when defining a function that does not return
anything, or when the function definition does not include any arguments.
• The presence of this keyword is not mandatory, but it is good programming practice to make use
of this feature.
Functions
• ACCESSING A FUNCTION
• A function can be accessed (i.e., called) by specifying its name, followed by a list of arguments
enclosed in parentheses and separated by commas.
• If the function call does not require any arguments, an empty pair of parentheses must follow the
name of the function.
• The function call may be a part of a simple expression (such as an assignment statement), or it may
be one of the operands within a more complex expression.
• The arguments appearing in the function call are referred to as actual arguments, in contrast to the
formal arguments that appear in the first line of the function definition. (They are also known simply
as arguments, or as actual parameters.)
• In a normal function call, there will be one actual argument for each formal argument.
• The actual arguments may be expressed as constants, single variables, or more complex expressions.
• However, each actual argument must be of the same data type as its corresponding formal argument.
• Remember that it is the value of each actual argument that is transferred into the function and
assigned to the corresponding formal argument.
Functions
• If the function returns a value, the function access is often written as an assignment statement; e.g.,
• y = polynomial(x);
• This function access causes the value returned by the function to be assigned to the variable y.
• On the other hand, if the function does not return anything, the function access appears by itself;
e.g.,
• display(a, b, c);
• This function access causes the values of a, b and c to be processed internally (i.e., displayed) within
the function.
• There may be several different calls to the same function from various places within a program.
• The actual arguments may differ from one function call to another.
• Within each function call, however, the actual arguments must correspond to the formal arguments
in the function definition; i.e., the number of actual arguments must be the same as the number of
formal arguments, and each actual argument must be of the same data type as its corresponding
formal argument.
Thank you

You might also like