C Programming Language is the most popular computer language and most used programming language till now. It is very simple and elegant language. This lecture series will give you basic concepts of structured programming language with C.
2. Outline
Definition of Functions
Advantages of Functions
Accessing a Function
Function Prototypes
Function Arguments
3. What are Functions?
We have already seen that C supports a number of
library functions.
However, C allows programmers to define their own
functions for carrying out various individual tasks that
allows a large program to be broken down into a
number of smaller, self-contained components, each
of which has some unique, identifiable purpose.
C programs can be modularized through the
intelligent use of such functions.
4. What are Functions? (cont..)
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.
Remember, one of these functions must be called
main because execution of the program will always
begin by carrying out the instructions in main.
5. What are Functions? (cont..)
A function will carry out its intended action
whenever it is accessed / called from some other
portion of the 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.
6. Advantages of Functions
There are several advantages of user-defined functions:
Many programs require that a particular group of
instructions be accessed repeatedly from several
different places within the program.The repeated
program can be placed within a single function, which
can then be accessed whenever it is needed.
Functions give the logical clarity as it decomposes the
program into several concise functions, where each
function represents some well-defined part of the
overall problem.
7. Advantages of Functions (cont..)
The use of a function avoids the need for redundant
(repeated) programming of the same instructions.
Functions make the program easier to write and
debug.
The use of functions enables a programmer to build a
customized library of frequently used routines or of
routines containing system-dependent features.
It promotes portability since programs can be written
that are independent of system-dependent features.
8. Defining a Function
A function definition in C programming consists of a
function header and a function body. Here are all the parts
of a function −
ReturnType: A function may return a value.The
return_type is the data type of the value the function
returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is
the keyword void.
Function Name: This is the actual name of the
function.The function name and the parameter list
together constitute the function signature.
9. Defining a Function (cont..)
Parameters:A parameter is like a placeholder.When
a function is invoked, you pass a value to the
parameter.This value is referred to as actual
parameter or argument.The parameter list refers to
the type, order, and number of the parameters of a
function. Parameters are optional; that is, a function
may contain no parameters.
Function Body: The function body contains a
collection of statements that define what the function
does.
10. Defining a Function (cont..)
A function declaration tells the compiler about a
function's name, return type, and parameters.
A function definition provides the actual body of the
function.
return_type function_name( parameter list ) {
body of the function
}
12. Functions Example2
// function returning the max between two numbers
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
13. Accessing a Function
A function can be accessed (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 argument, an
empty pair of parentheses must follow the name of
the function.
If the function returns a value, the function access is
often written as an assignment statement-
y = polynomial(x);
14. Accessing A Functions (cont..)
On the other hand, if the function does not return anything,
the function access appears by itself
display (a, b, c);
15. Function Prototypes
A function prototype or function interface is a
declaration of a function that specifies the function's
name and type signature, but omits the function body.
While a function definition specifies how the function
does what it does (the "implementation"), a function
prototype merely specifies its interface, i.e. what data
types go in and come out of it.
16. Function Prototypes: Purposes
The Function prototype serves the following purposes:
It tells the return type of the data that the function
will return.
It tells the number of arguments passed to the
function.
It tells the data types of the each of the passed
arguments.
Also it tells the order in which the arguments are
passed to the function.
17. Function Prototypes (example)
#include <stdio.h>
int myfunction(int n); /* Prototype */
int main() {
printf("%dn", myfunction());
}
int myfunction( int n) {
if (n == 0)
return 1;
else
return n * myfunction(n - 1);
}
18. Function Arguments
If a function is to use arguments, it must declare
variables that accept the values of the arguments.
These variables are called the formal parameters of
the function.
Formal parameters behave like other local variables
inside the function and are created upon entry into
the function and destroyed upon exit.
19. Function Arguments (cont..)
While calling a function, there are two ways in which
arguments can be passed to a function:
1. Call by value: This method copies the actual value of an
argument into the formal parameter of the function. In this
case, changes made to the parameter inside the function have
no effect on the argument.
2. Call by reference: This method copies the address of an
argument into the formal parameter. Inside the function, the
address is used to access the actual argument used in the call.
This means that changes made to the parameter affect the
argument.
By default, C uses call by value to pass arguments.