Module 3
Module 3
Chapter1-Functions
Introduction
Every C program should consist of one or more functions. Among these
functions main() function is compulsory. All programs start execution
from main function.
Functions
• A function is a block of code to perform a specific task.
• Every c program has atleast one function main(). Without main() function, there is
technically no c program.
• In modular programming, the program is divided into separate small programs
called modules.
• Each module is designed to perform a specific task.
• Modules make our actual program shorter. Hence easier to read and write.
Advantages of Functions
Functions based modular programming is advantageous in many ways:
• Managing huge programs and software packages is easier by dividing them into
functions/modules—Maintenance is easier
• Error detection is easier—Debugging is easier
• Functions once written can be re-used in any other applications – Reusability is
enhanced
• We can protect our data from illegal users—Data protection becomes easier
Top-down modular programming using
functions
Modular programming
• Modular programming is defined as organizing a large program into small,
independent program segments called modules that are separately named and
individually callable program units.
• It is basically a “divide-and-conquer” approach to problem solving.
Characteristics of Modular programming:
1. Each module should do only one thing.
2. Communication between modules is allowed only by calling module.
3. No communication can take place directly between modules that do not have
calling-called relationship.
4. All modules are designed as single-entry, single-exit systems using control
structures.
5. A module can be called by one and only higher module.
Function types
1. Built-in(library) function
2. User defined function
Advantages of user defined functions
• Decompose the large program into small segments which makes the programmer
easier to understand, maintain and debug.
• If repeated code occurs in a program, then function can include those codes and
execute when needed by calling that function.
• Programmer working on large project can divide the workload by making
different functions.
Basic structure of c program with functions
Actual and Formal parameters/arguments
• Arguments refer to data that is passed to function(function
definition) while calling
• Arguments listed in function call are referred to as actual arguments.
• The arguments used in function declaration are called formal
parameters/arguments. They receive values from actual parameters
• The number of actual and formal arguments and their datatypes
should always be same.
Location of Functions
#include<stdio.h>
void func(int arr[]);
int main()
{
int arr[10], i;
printf("Enter 10 array elements: ");
for(i=0; i<10; i++)
scanf("%d", &arr[i]);
printf("\nPassing array to the function...\n");
func(arr);
return 0;
}
void func(int arr[])
{
int i;
printf("\nThe array is:\n");
for(i=0; i<10; i++)
printf("%d ", arr[i]);
}
Categories of Functions
Void Functions without parameters – No arguments and no return values
• When a function has no arguments, it does not receive any data from
the calling function.
• Similarly, when it does not return a value, the calling function does
not receive any data from the called function.
Void Functions with parameters – Arguments but no return values
• The calling function accepts the input, checks its validity and pass it
on to the called function.
• The called function does not return any values back to calling
function.
• The actual and formal arguments should match in number, type and
order.
Functions with parameters and return values – Arguments with return values
• The calling function sends the data to called function and also accepts
the return values from called function.
• Actual Parameters: When a function is called, the values that are
passed in the call are called actual parameters.
• Formal Parameters:The values which are received by the function,
and are assigned to formal parameters.
Functions without parameters and with return values – No arguments but returns values
• The calling function does not send any data to called function and
but, accepts the return values from called function
Inter-Function Communication
float r, ar = 0.0 ; }
printf(" Enter the radius of circle : ") ;
scanf("%f", &r) ;
ar = area(r) ;
printf("\n Area of circle : %f ", ar) ;
return (0);
}
C Program to check even or odd
number
#include <stdio.h> /* If isEven() function returns 0 then the
int isEven(int num) number is even */
{ if(isEven(num))
return !(num & 1); {
} printf("The number is even.");
int main() }
{ else
int num; {
printf("The number is odd.");
/* Input number from user */ }
printf("Enter any number: ");
scanf("%d", &num); return 0;
}
Module-3
Chapter 1
Arrays
Contd...
Arrays are used to represent multiple data items
of the same type using single name.
Structure of an Array
Example of an array
One-dimensional array
A list of items can be given one variable name using
only one subscript and such a variable is called as
one-dimensional array.
• Ex: int marks[4];
Declaration of one-dimensional array
Syntax:
data_type array_name[array_size];
2 3 0 0 0
Initialization during program
execution (runtime initialization)
The below example shows the reading and writing to
the one-dimensional array:
return 0;
}
OUTPUT
Operations performed
on one-dimensional
array
Searching
• It is the process of finding the location of the
specified element in a list.
• The specified element is often called the search key.
• If it is found search is successful, otherwise it is
unsuccessful.