Module2
FUNCTIONS
A function is a set of instructions that are used to perform specified tasks which repeatedly occurs
in the main program.
Can be called multiple times to provide reusability and modularity.
Also called as procedures or subroutine.
Need of functions -- reusability, reduces redundancy , code modular, abstraction ,easy to
understand and manage, breaks an extensive program to smaller parts.
Advantages – modularity, reusable code, call function
Function implementation includes
1. Function name
2. Function type
3. List of parameters
4. Local variable declarations
5. Function statements
6. A return statement
Six elements are grouped into two parts
Function header (1st three elements)
Function body(2nd three elements)
FUNCTION HEADER:
• Function type : explicitly in C taken as int
• Function name : valid C identifier, name of the task
• List of parameters : variables that will receive the data sent by the calling program, it is like input data
Example : Float mul (float x, float y) {….}
int sum (int a, int b) {….}
FUNCTION BODY
• Local variable declaration : variables needed by the function
• Function statements : statements that perform the task
• Return statements : returns the value evaluated by the function
Calling function name:
Called function name:
Called function doesn't take any arguments
#include <stdio.h> #include <stdio.h>
int calculate_square(int num) void print_square(int num)
{ {
int square_result = num * num; int square_result = num * num;
return square_result; printf("Square of %d is: %d\n", num,
} square_result);
int main() }
{ int main()
int result = calculate_square(5); {
printf("Square of 5: %d\n", result); print_square(5);
return 0; return 0;
} }
TYPES OF FUNCTIONS
Built in /library functions are declared in C header
files.
User defined functions are created by programmer.
BUILT IN \LIBRARY FUNCTIONS
Also called as pre defined /standard functions.
They are defined in library of c compiler
They are defined inside the diff header files saved with extension .h.
Ex: stdio.h , conio.h , string.h , math.h , time.h, ctype.h, stdlib.h
#include<stdio.h>
#include<math.h>
{
float Num, root;
printf(“enter a number: “);
scanf(“%f”, &num);
root=sqrt(num);
printf(“square root of %.2f = %.2f”, num, root);
return 0;
}
USER DEFINED FUNCTIONS
Functions defined by the user to perform a particular task that is repeatedly used in main program.
Helpful to break down a large program in to a number of smaller functions.
Need for user defined functions :
complex program under main() , debugging , testing and maintenance becomes difficult
Advantages of user defined functions :
length of source program,
easy to locate and debug an error
used in many other source programs
avoid coding of repeated programming
build a customized library of repeatedly used routines
facilitate top down programming approach
Elements of User defined functions
Function definition
Function declaration
Function call
FUNCTION DECLARATION: Before they are defined and invoked .
FUNCTION DEFINITION : process of specifying and establishing user defined function by specifying all its
elements and characteristics.
PARAMETERS
provide the communication between
the calling function and called function
ACTUAL : Parameters transferred from the calling function to called function.
FORMAL : transferred into the calling function from the called function.
#include <stdio.h>
// Function definition with formal parameters a and b
int add(int a, int b) {
return a + b;
}
int main() {
int x = 5;
int y = 10;
int sum;
// Function call with actual parameters x and y
sum = add(x, y);
printf("The sum of %d and %d is %d\n", x, y, sum);
return 0;
}
LOCAL AND GLOBAL VARIABLE
LOCAL VARIABLES -- defined within the body of the function.
value (int a, int b)
{
int c,d; -------> local variables
}
GLOBAL VARIABLES -- defined outside the main() , multiple functions can use these variables.
int a,b;
main()
{
value();
}
The return statement:
used to end a function's execution and optionally return a value to the calling function.
SYNTAX :
#include <stdio.h>
return; // for void functions
int add(int a, int b) {
return(exp); //for functions with return value
return a + b;
Function can have multiple return statements
}
Points to remember:
void printMessage() {
1)Return only one value
printf("Hello, world!\n");
2)Can be present anywhere
return;
3)No return, use void
}
int main() {
int sum = add(5, 3);
printf("Sum: %d\n", sum);
printMessage();
return 0;
}
ADVANTAGES OF FUNCTIONS
It reduces the length of source program.
Breaks the complexity of a program.
Break small program into small program.
It is easy to maintain, modify and understand.
A program can be divided into smaller subprograms.
It facilitates top down modular programming.
The length of the source program can be reduced using functions
Critical in microcomputers, where memory space is limited.
Avoid rewriting the same sequence of code at two or more locations in a
program.
FUNCTION DECLARATION
Funtion_type function_name(parameter list);
Points to note :
1. The parameter list must be separated by commas.
2. If the function has no formal parameters, the list is written as void.
3. The return type is optional when the function returns int type data.
4. When the declared type does not match the types in the function definition compiler will produce an
error.
TYPES OF FUNCTIONS
In general, a function based on whether the Argument is present, whether the value is returned or not is
classified into 4 types.
They are
Functions with no Arguments and no return values.
Functions with Arguments and no return values.
Functions with no Arguments but with return values.
Functions with Arguments and with return values.
Functions with multiple return values