Lecture 24 - Functions - Introduction and Use
Lecture 24 - Functions - Introduction and Use
=======================
FUNCTIONS ..................................................................................................................................... 2
INTRODUCTION ............................................................................................................................. 2
TYPES OF FUNCTIONS ................................................................................................................... 2
1. PREDEFINED STANDARD LIBRARY FUNCTIONS .......................................................... 2
2. USER DEFINED FUNCTIONS ......................................................................................... 2
ADVANTAGES OF USING FUNCTIONS IN C ................................................................................ 2
ELEMENTS OF A FUNCTION .......................................................................................................... 3
1. FUNCTION DECLARATION ........................................................................................... 3
2. FUNCTION CALL ........................................................................................................... 3
3. FUNCTION DEFINITION ................................................................................................ 3
HOW TO CREATE AND USE A USER D EFINED FUNCTION ......................................................... 3
A BASIC PROGRAM TO ADD TWO NUMBERS USING FUNCTIONS ........................................... 4
PROGRAM TO C HECK WHETHER A NUMBER IS EVEN OR ODD USING F UNCTIONS .......... 5
1 | P age
F UNCTIONS
INTRODUCTION
In c, we can divide a large program into small basic building blocks of code, known as function. The
function contains the set of programming statements enclosed between {}. In other words, we can say
that a function is a group of statements that together perform a task; it takes input, performs some
computation over the given input and then produces output. Every C program has at least one function,
which is main(),
The idea behind using functions is to put some commonly or repeatedly done task together and makes
them a function so that instead of writing the same code again and again for different inputs and at
different places in a program, we can call the function only by using one single function call statement
instead of writing that whole code.
TYPES OF FUNCTIONS
There are two types of functions namely Standard library functions and User defined functions.
2 | P age
ELEMENTS OF A FUNCTION
There are three aspects of a C function.
1. FUNCTION DECLARATION
A function must be declared globally in a c program to tell the compiler about the function name,
function parameters and return type of a function.
2. FUNCTION CALL
A function can be called from anywhere in the program using a function call statment. The
parameter list must not differ in function calling and function declaration. We must pass the same
number of parameters to a function as already declared in the function declaration section.
3. FUNCTION DEFINITION
It contains the actual statements which are to be executed. It is the most important aspect to which
the control comes when the function is called. Here, we must notice that only one value can be
returned from the function.
return_type:
Return type can be of any data type such as int, double, char, void, short etc. We will understand these
terms better once you go through the examples below.
function_name:
It can be anything, however it is advised to have a meaningful name for the functions so that it would
be easy to understand the purpose of function just by seeing it’s name.
argument list:
Argument list contains variables names along with their data types. These arguments are kind of inputs
for the function. For example – A function which is used to add two integer variables, will be having
two integer argument.
Block of code:
Set of C statements, which will be executed whenever a call will be made to the function.
3 | P age
A BASIC PROGRAM TO ADD TWO NUMBERS USING FUNCTIONS
Below we’ll write two programs to add two numbers, one without using functions and another one with
using functions.
// PROGRAM TO PERFROM ADDITION OF TWO NUMBERS WITHOUT USING FUNCTIONS
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
printf("Enter first number\n");
scanf(“%d” , & a);
printf("Enter second number\n");
scanf(“%d” , & b);
// Performing Addition
sum = a + b;
printf (“Sum of %d and %d = %d ”, a,b,sum);
getch();
}
OUTPUT:
Enter first number
12
Enter second number
16
Sum of 12 and 16 = 28
OUTPUT 1:
Enter any number
12
12 is EVEN
OUTPUT 2:
Enter any number
17
17 is ODD
5 | P age