Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Lecture 24 - Functions - Introduction and Use

Uploaded by

Zahid Wani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 24 - Functions - Introduction and Use

Uploaded by

Zahid Wani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

TABLE OF CONTENTS

=======================
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.

1. PREDEFINED STANDARD LIBRARY FUNCTIONS


These are the functions that have already been defined to the compiler under header files (.h files
like stdio.h), so we just call them whenever there exists any need of them in a program. Examples
of such include puts(), gets(), printf(), scanf(), getch() etc.

2. USER DEFINED FUNCTIONS


The functions that we ourselves as programmers create in a program are known as user defined
functions. In the following subsections, we’ll see how to create user defined function and how to
make use of it. However, before proceeding for that, we’ll go through various advantages of
functions.

ADVANTAGES OF USING FUNCTIONS IN C


Functions are used because of following advantages:

 To improve the readability of code.


 To improve the reusability of the code which means that same function can be used in any
program rather than writing the same code from scratch.
 Debugging of the code would be easier if you use functions, as errors are easy to be traced.
 Reduces the size of the code, duplicate set of statements are replaced by function calls.

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.

HOW TO CREATE AND USE A USER DEFINED FUNCTION


Syntax of a function:

return_type function_name (argument list)

Set of statements / Block of code

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

// PROGRAM TO PERFROM ADDITION OF TWO NUMBERS USING FUNCTIONS


#include<stdio.h>
#include<conio.h>
int add (int, int); // Function Declaration, also called as Function Prototype
void main()
{
int a,b,sum;
printf("Enter first number\n");
scanf(“%d” , & a);
printf("Enter second number\n");
scanf(“%d” , & b);
// Performing Addition using a function
sum = add (a , b); // Function Call
printf (“Sum of %d and %d = %d ”, a,b,sum);
getch();
}
int add (int x , int y) // Function Definition
{
int z;
z = x + y;
return z;
}
OUTPUT:
Enter first number
12
Enter second number
16
Sum of 12 and 16 = 28
4 | P age
PROGRAM TO CHECK WHETHER A NUMBER IS EVEN OR ODD USING FUNCTIONS

// PROGRAM TO PERFROM ADDITION OF TWO NUMBERS USING FUNCTIONS


#include<stdio.h>
#include<conio.h>
void evenodd (int); // Function Declaration, also called as Function Prototype
void main()
{
int a;
printf("Enter any number\n");
scanf( “ %d ” , & a);
// Checking whether the given number is Even or Odd using a function
evenodd (a); // Function Call
getch();
}
void evenodd (int x ) // Function Definition
{
if ( x % 2 == 0)
{
printf( “ %d is EVEN\n ” , x);
}
else
{
printf( “ %d is ODD\n” , x);
}
}

OUTPUT 1:
Enter any number
12
12 is EVEN

OUTPUT 2:
Enter any number
17
17 is ODD

5 | P age

You might also like