Unit 4-Part 2 Modularisation Details
Unit 4-Part 2 Modularisation Details
O Level / A Level
Chapter - 6 : Functions
Types of functions
1) Predefined standard library functions – such as puts(), gets(), printf(), scanf() etc – These
are the functions which already have a definition in header files (.h files like stdio.h), so we
just call them whenever there is a need to use them.
2) User Defined functions – The functions that we create in a program are known as user
defined functions.
Need functions in C
Functions are used because of following reasons –
1) To improve the readability of code.
2) Improves the reusability of the code, same function can be used in any program rather than
writing the same code from scratch.
3) Debugging of the code would be easier if you use functions, as errors are easy to be traced.
4) Reduces the size of the code, duplicate set of statements are replaced by function calls.
Syntax of a function
return_type function_name (argument list)
{
Local Variable Declaration
Set of statements – Block of code
}
return_type: Return type can be of any data type such as int, double, char, void, short etc. Don’t
worry you 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.
Prototype of a Function
return_typefunction_name( arg type name-1,...,arg type name-n);
A function prototype provides the compiler with the description of a function that will be
defined at a later point in the program.
The function prototype includes a return type indicating the type of variable that the
functions will return; It also includes the function name, which should describe what the
function does.
The prototype also contains the variable types of the arguments (arg type) that will be
passed to the function.
Optionally, it can contain the names of the variable that will be passed.
A prototype should always end with a semicolon.
Example -
o double squared( double number );
o void print_report( int report_number );
o int get_menu_choice( void );
Function Definition
A function definition is the actual function.
The definition contains the code that will be executed.
The first line of a function definition, called the function header, should be identical to the
function prototype, with the exception of the semicolon.
A function header shouldn′t end with a semicolon.
The argument variable names the optional in the prototype, they must be included in the
function header.
Following the header is the function body, containing the statements that the function will
perform.
The function body should start with an opening bracket and end with a closing bracket.
If the function return type is anything other than void, a return statement should be included,
returning a value matching the return type.
#include<stdio.h>
Output :- Total is 5
Program : Creating a void user defined function that doesn’t return anything
#include <stdio.h>
void main()
{
/*calling function*/
introduction();
}
Program : function returning the max between two numbers
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
void main () {
return result;
}
Assignment
1. Write a function to print your name , date of birth and city name.
2. Write a function to print the A B C D E pattern 5 times.
3. Write a function to print the sum of 3 numbers.
4. Write a function to print the area of a rectangle.
5. Write a function to print the area of circle. The function prototype is int area_circle(int);