Module 07-Introduction To Functions
Module 07-Introduction To Functions
C Programming Language
Module 7-Introduction to Functions
History of C language
1. C is mother language of all programming language. It is
procedure-oriented programming language.
2. C programming language was developed in 1972 by Dennis
Ritchie at bell laboratories of AT&T(American Telephone &
Telegraph), located in U.S.A.
3. Dennis Ritchie is known as founder of c language.
4. It was developed to be used in UNIX Operating system.
5. It inherits many features of previous languages such as B and
BPCL. Language year Developed By
ALGOL 1960 International Group
BPCL 1967 Martin Richards
B 1970 Ken Thompson
Traditional C 1972 Dennis Ritchie
K&RC 1978 Kernighan & Dennis Ritchie
ANSI C 1989 ANSI Committee
ANSI/ISO C 1990 ISO Committee
C99 1999 Standardization Committee
1
Overview
1. 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.
2. 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.
3. We have already seen that C supports the use of library functions, which are used to
carry out a number of commonly used operations or calculations.
4. C also allows programmers to define their own functions for carrying out various
individual tasks. The use of programmer-defined functions allows a large program to be
broken down into a number of smaller, self-contained components/modules, each of
which has some unique, identifiable purpose.
5. The use of a function avoids the need for redundant (repeated) programming of the
same instructions.
6. The use of functions also enables a programmer to build a customized library of
frequently used routines. Hence, a single function can be utilized by many different
programs.
7. 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.
1
Example of a UDF Function
#include <stdio.h>
char lower_to_upper(char c1) /* function definition */
{
char c2;
c2 = ( c1 >= 'a' && c1 <= 'z' ) ? ( 'A' + c1 - 'a' ) : c1 ;
return(c2);
}
void main( )
{
char lower, upper;
printf("P1ease enter a lowercase character: " ) ;
scanf("%c", &lower) ;
upper = lower_to_upper(lower) ;
printf("\nThe uppercase equivalent is %c\n\n", upper) ;
}
2
Defining a Function
1. 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.
data- type name( type1 arg1, type2 arg2, . . ., typeN argN)
2. Each argument is preceded by its associated type declaration.
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.
3. An empty pair of parentheses must follow the function name if the function definition
does not include any arguments.
4. The corresponding arguments in the function reference or call are called actual
arguments, since they define the data items that are actually transferred.
5. The identifiers used as formal arguments are "local" in the sense that they are not
recognized outside of the function.
6. 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.
7. Each formal argument must be of the same data type, however, as the data item it
receives from the calling portion of the program.
3
Defining a Function
8. The remainder of the function definition is a compound statement that defines the
action to be taken by the function and is sometimes referred to as the body of the
function.
9. The Information is returned from the function to the calling portion of the program
via the return statement.
10. The return statement also causes the program logic to return to the point from
which the function was accessed.
11. In general terms, the return statement is written as
return expression;
12. Only one expression can be included in the return statement.
13. Thus, a function can return only one value to the calling portion of the program via
return.
14. The return statement can also be absent altogether from a function definition
15. 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.
16. The presence of this keyword is not mandatory, but it is good programming
practice to make use of this feature.
4
Accessing a Function
1. 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.
2. If the function call does not require any arguments, an empty pair of
parentheses must follow the name of the function.
3. The arguments appearing in the function call are referred to as actual
arguments.
4. There will be one actual argument for each formal argument. The actual
arguments may be expressed as constants, single variables, or more complex
expressions.
5. However, each actual argument must be of the same data type as its
corresponding formal argument.
6. Remember that it is the value of each actual argument that is transferred
into the function and assigned to the corresponding formal argument.
7. If the function returns a value, the function access is often written as an
assignment statement; e.g.,
y = polynomial(x);
5
Function Prototypes
1. Sometimes the function access (within main) precedes the function definition. This can
be confusing to the compiler, unless the compiler is first alerted to the fact that the
function being accessed will be defined later in the program. A function prototype is
used for this purpose.
2. Function prototypes are usually written at the beginning of a program, ahead of any
programmer-defined functions (including main). The general form of a function
prototype is
data-type name( typeI argI , type2 arg2, . . ., typeN argN ) ;
3. A function prototype resembles the first line of a function definition (though a function
prototype ends with a semicolon).
4. The names of the arguments within the function prototype need not be declared
elsewhere in the program, since these are "dummy" argument names that are
recognized only within the prototype.
5. In fact, the argument names can be omitted (though it is not a good idea to do
so);however, the argument data types are essential.
6. The data types of the actual arguments must conform to the data types of the arguments
within the prototype.
7. Function prototypes are not mandatory in C. They are desirable, however, because they
further facilitate error checking between the calls to a function and the corresponding
function definition.
6
Passing Arguments to a Function
1. When a single value is passed to a function via an actual argument, the value of the
actual argument is copied into the function.
2. The value of the corresponding formal argument can be altered within the function, but
the value of the actual argument within the calling routine will not change. This
procedure for passing the value of an argument to a function is known as passing by
value.
#include<stdio.h>
void modify( int a); /* function prototype */
main()
{
int a = 2;
printf("\n a = %d (from main, before calling the f u n c t i o n ) " , a ) ;
modify(a);
printf("\n \n a = %d (from main, after calling the f u n c t i o n ) " , a ) ;
}
void modify( int a)
{
a *= 3;
printf("\n \n a = %d (from the function, after being modified)", a ) ;
return;
}
7
Passing Arguments to a Function
3. When the program is executed, the following output is generated.
a = 2 (from main, before calling the function)
a = 6 (from the function, after being modified)
a = 2 (from main, after calling the function)
4. These results show that a is not altered within main, even though the
corresponding value of a is changed within modify.
5. Passing an argument by value has advantages and disadvantages.
On the plus side, it allows a single valued actual argument to be written as
an expression rather than being restricted to a single variable.
Moreover, if the actual argument is expressed simply as a single variable, it
protects the value of this variable from alterations within the function.
On the other hand, it does not allow information to be transferred back to
the calling portion of the program via arguments.
Thus, passing by value is restricted to a one-way transfer of in formation.
8
Passing Arguments to a Function
6. Array arguments are passed differently than single-valued data items. If an
array name is specified as an actual argument, the individual array elements
are not copied. Instead, the location of the array (i.e., the location of the first
element) is passed to the function.
7. If an element of the array is then accessed within the function, the access
will refer to the location of that array element relative to the location of the
first element.
8. Thus, any alteration to an array element within the function will carry over to
the calling routine.
9. There are also other kinds of data structures that can be passed as
arguments to a function.
9
Recursion
1. Recursion is a process by which a function calls itself repeatedly, until some
specified condition has been satisfied.
2. The process is used for repetitive computations in which each action is stated in
terms of a previous result.
3. Many iterative (i.e., repetitive) problems can be written in this form.
4. In order to solve a problem recursively, two conditions must be satisfied. First,
the problem must be written in a recursive form, and second, the problem
statement must include a stopping condition.
5. When a recursive program is executed, the recursive function calls are not
executed immediately. Rather, they are placed on a stuck until the condition that
terminates the recursion is encountered.
6. The function calls are then executed in reverse order, as they are “popped” off
the stack.
7. If a recursive function contains local variables, a different set of local variables
will be created during each call. The names of the local variables will, of course,
always be the same, as declared within the function. However, the variables will
represent a different set of values each time the function is executed.
10
Recursion
1. #include<stdio.h>
2. int factorial(int n)
3. {
4. if(n<=1) //stopping condition
5. return 1;
6. else
7. return n*factorial(n-1); //recursive call
8. }
9. int main ()
10. {
11. int n;
12. printf("enter the number: ");
13. scanf("%d",&n);
14. printf("factorial is %d",factorial(n));
15. return(0);
16. }
11
Recursion
1. #include <stdio.h>
2. #define EOLN '\n'
3. /* read a line of characters and write it out backwards */
4. void reverse (void) ; /* function prototype */
5. main()
6. {
7. printf ("Please enter a l i n e of t e x t below\n");
8. reverse() ;
9. }
10. void reverse(void)
11. {
12. char c;
13. if((c=getchar()) != EOLN)
14. reverse();
15. putchar(c);
16. }
12
Summary
1. To avoid repetition of code and bulky programs functionally related statements
are isolated into a function.
2. Function declaration specifies what is the return type of the function and the
types of parameters it accepts.
3. Function definition defines the body of the function.
4. Variables declared in a function are not available to other functions in a program.
So, there won’t be any clash even if we give same name to the variables declared
in different functions.
5. A function can be called either by value or by reference.
6. Pointers can be used to make a function return more than one value
simultaneously.
7. Recursion is difficult to understand, but in some cases offer a better solution
than loops.
8. Adding too many functions and calling them frequently may slow down the
program execution.
13
PROCESS MAKE-IN-INDIA
RE-ENGINEERING
धन्य
CAPACITY BUILDING
वाद DIGITAL LOCKER
AURANGABAD