2. Functions
• A function is a group of statements that together
perform a task.
• Functions are sets of statements that take inputs,
perform some operations, and produce results
Functions are used to perform certain actions, and
they are important for reusing code:
Define the code once, and use it many times.
Mehak Bhatia, IIMT College of Science
and Technology
3. Functions
Within a function, there are a number of
programming statements enclosed by {}, having
certain meanings and performing certain
operations.
The operation of a function occurs only when it
is called.
Rather than writing the same code for different
inputs repeatedly, we can call the function
instead of writing the same code over and over
again.
Mehak Bhatia, IIMT College of Science
and Technology
4. Creating a Function
Syntax:
void function_name()
{
// code to be executed
}
Example:
void myFunction()
{
printf("Hello World!");
}
Mehak Bhatia, IIMT College of Science
and Technology
5. Explanation of Example
myFunction() is the name of the function.
Inside the function (the body), we have to add
the code that defines what the function should
do.
void means that the function does not have a
return value.
Mehak Bhatia, IIMT College of Science
and Technology
6. Why use Functions?
The function can reduce the repetition of the
same statements in the program.
The function makes code readable.
There is no fixed number of calling functions it
can be called as many times as you want.
The function reduces the size of the program.
Once the function is declared you can just use it
without thinking about the internal working of
the function.
Mehak Bhatia, IIMT College of Science
and Technology
8. Defining a Function
The general form of a function definition in
programming language is as follows :
return_type function_name( parameter list )
{
body of the function
}
A function definition in C programming consists of
a function header and a function body.
Mehak Bhatia, IIMT College of Science
and Technology
9. Here are all the parts of a function −
Mehak Bhatia, IIMT College of Science
and Technology
Return Type − 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.
10. 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.
NOTE: 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.
Mehak Bhatia, IIMT College of Science
and Technology
12. Given below is the source code for a function called max().This
function takes two parameters num1 and num2 and returns the
maximum value between the two −
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
} Mehak Bhatia, IIMT College of Science
and Technology