Module3 Functions
Module3 Functions
✔ “Function is a small program or program segment that carryout some specific well-
defined tasks”.
Advantages of Functions
i. Reduces the Complexity.
✔ When a program contains more number of instructions (complex program), then such large
program can be divided into number of sub programs called as functions.
ii. Improves the Readability.
iii. Easy to debug the errors.
iv. Reusability: The set of instructions specifying the task performed by the function is written just
once but it can be used any number of times.
v. Easy to do the modifications.
Types of Functions
i. Library Functions/Pre-Defined/ Built-in Functions
✔ C Library of C Compiler has a collection of various functions which perform some standard
and pre-defined tasks.
✔ These functions written by designers of C Compilers are called as
Library functions/Pre-Defined/Built-in functions.
Ex: sqrt(n)- computes square root of
𝑦
n. pow(x,y)- computes 𝑥 .
printf()- used to print the data on the screen.
scanf()- used to read the data from the
keyboard. abs(x)- computes absolute value of x.
Example: Write a C program to demonstrate the usage of Built-in functions.
#include<stdio.h> #include<math.h> void main()
{
float n,res; float sqrt(float num)
{
printf(“Enter the value of n:\n”); scanf(“%f”,&n);
res=sqrt(n); statement-1; statement-2;
printf(“Square root=%f”,res); …………… return statement;
} }
1
Principles of Programming using BPOP10
Example: in the above program main() function invokes sqrt() function, the function main() is
calling function and function sqrt() is called function.
Function Prototype/Declaration
✔ As we normally declare the variables before they are used, the functions also should
be declared before they are used.
✔ The process of declaring the functions before they are used (or called) in the program is
called Function Prototype.
✔ The function prototype is also called as Function declaration.
✔ It is same as the Function Header but terminated with semicolon.
✔ It does not contain the body of the function.
Syntax
return_type function_name(parameter
list);
Where,
return_type: This is the data type of the value that the function is expected to return (int, float,
double, char).
function_name: It is the name of the function. It can be any valid Identifier.
parameter list: The parameters are the list of variables enclosed within parenthesis. Variables
are separated by comma.
2
Principles of Programming using BPOP10
Function Definition
✔ The program module that is written to achieve a specific task is called function
definition.
✔ Each function definition consists of two parts:
Function Header
Function Body
Syntax: return_type function_name (parameter list)
{
declaration part;
executable part;
return
} statement;
return_type:
✔ This is the data type of the value that the function is expected to return (int, float, double,
char).
✔ If the function is not returning any value, then we need to specify the return type as ‘void’.
✔ The default return value of any function is integer.
function_name:
✔ It is the name of the function. It can be any valid Identifier.
Parameters:
✔ The parameters are the list of variables enclosed within parenthesis. Variables are separated
by comma.
Data type of parameters
Parameter name
return type Function name
Function Body
✔ Declaration part: All the variables used in the function body should be declared in this part.
✔ Executable par: This part contains the statements or instructions that perform the specified
activity.
✔ return statement: It is a keyword used to return the control to the calling function (main)
with/without a value.
Syntax:
1.
// Returns control without sending any value to main program
3
Principles of Programming using BPOP10
Function Call
✔ Once the function is defined, it has to be called so as to achieve the task. This method
of calling a function to achieve a specified task is called Function Call.
✔ The function can be called by writing the name of the function and passing the appropriate
number of arguments.
✔ The number of arguments in the function call and number of parameters in the function
definition must match.
✔ Also the order of arguments in the function call and parameters in the function definition
must match.
Example: add(m,n);
Example Program: Write a C program to perform the addition of two numbers using function
#include<stdio.h>
int add(int a,int b); /*Function
Prototype*/ int add(int a,int b)
{
int sum;
sum=a+b;
return
sum;
}
void main()
{
int result;
result=add(10,20);
printf(“sum=%d\n”,result);
getch();
}
4
Principles of Programming using BPOP10
Function Parameters
✔ “The list of variables defined in the function header within the parenthesis are called
Function parameters”.
✔ There are 2 types of parameters in ‘C’ functions.
i. Actual parameters
ii. Formal parameters
5
Principles of Programming using BPOP10
Location of Functions
✔ The placement of the function definition in relation to the main program is very important.
✔ There are a number of ways to arrange the main program and a function.
6
Principles of Programming using 22POP
✔ Even though the function header comes after the main program, the function prototype
appears before the call to the function and it is called Bottom-up approach.
✔ Function prototype is compulsory in this method.
Example Program
#include<stdio.h>
int add(int a,int b);
void main()
{
int m,n,res;
printf(“Enter the values for m,n:\n”);
scanf(“%d%d”,&m,&n);
res=add(m,n); printf(“add(%d,%d)=
%d\n”,m,n,res);
}
7
Principles of Programming using 22POP
This is the way library functions are used.
✔ Here the file method3.c contains a main program and also a separate file.
✔ The file method4.c is a separate file containing a function and it performs some specific tasks
and returns the value or result to the main program in a file ‘method3.c’. i.e., a separate file.
Example Program
Categories of Functions
✔ Based on the parameters and return value, the functions have been classified into four categories.
1. void and parameter less functions (Functions with no parameters and no return values)
2. non void and parameter less functions(Functions with no parameters and return values)
3. void with parameters functions (Functions with parameters and no return values)
4. non void with parameters functions (Functions with parameters and return values)
8
Principles of Programming using 22POP
1. void and parameter less functions (Functions with no parameters and no return values)
✔ In this category there is no data transfer between the calling function and the called function.
✔ So calling function cannot send values and hence called function cannot receive the data and
it will not send any parameter back to function.
Example: Program showing function call with no parameters and returns no value.
#include<stdio.h>
void add()
void main() {
{ int a=10, b= 20,sum;
add();
sum=a+b;
} printf(“Sum=%d”,sum);
/*No return value*/
}
2. non void and parameter less functions (Functions with no parameters and return values)
✔ In this category there is no data transfer from the calling function and the called function.
✔ But, there is data transfer from the called function to the calling function.
✔ When the function returns a value, the calling function receives one value from the called
function.
✔ We can write a function that has no parameters but returns an answer to the main program
as shown in the syntax.
Example: Program showing function call without parameters, with return value.
#include<stdio.h>
int add()
void main() {
{ int res; int a=10, b= 20,sum;
res= add();
printf(“Sum=%d”,res); sum=a+b;
} return sum;
3. void with parameters functions (Functions with parameters and no return values)
✔ In this category, there is a data transfer from the calling function to the called function
using parameters.
✔ But, there is no data transfer from the called function to the calling function
9
Principles of Programming using 22POP
Example: Program showing a function call with parameters and no return value.
#include<stdio.h>
void add(int m, int n)
void main() {
{
int a=10, b=20; int sum;
add(a,b);
sum= m + n;
} printf(“Sum=%d”,sum);
}
4. non void with parameters functions (Functions with parameters and return values)
✔ In this category, there is data transfer between the calling function and the called function.
✔ When parameters are passed, the called function can receive values from the calling function.
✔ When the function returns a value, the calling function can receive a value from the called
function.
Example: Program showing a function call with parameters and with returns value.
#include<stdio.h>
void add(int m, int n)
void main() {
{
int a=10, b=20, res; int
sum; res=add(a,b);
printf(“Sum=%d”, res); sum= m + n;
} return sum;
}
1
Principles of Programming using 22POP
Example Program: Write a C program to add two numbers using call by value.
#include<stdio.h>
void add (int a, int b) // Formal parameters a = 10, b =20
{
int sum;
sum = a + b;
return sum;
}
void
main()
{
int m=10,n=20, res;
res = add(m,n); // Actual parameter m=10, n
=20 printf(“result = %d \n”, res);
}
Example Program: Write a C program to add two numbers using call by reference.
#include<stdio.h>
void add (int *a, int *b) // Formal parameters *a = 10, *b =20
{
int sum;
sum = *a + *b;
return sum;
}
void main()
{
int m=10,n=20, res;
res = add(&m, &n); // Actual parameter m=10, n
=20 printf(“result = %d \n”, res);
}
1
Principles of Programming using 22POP
Example Program: Write a C program to swap two numbers using call by reference.
#include<stdio.h>
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int m=10,n=20;
swap(&m,&n);
printf(“m=%d\n n=%d\n”,m,n);
}
1
Principles of Programming using 22POP
2. Passing the whole array
✔ Suppose, we want to pass whole array to a function. In such situation, we must follow the
two rules:
1. The function must be called by passing only the name of the array.
2. In the function definition, the parameter must be declared as an array of the same type as
that of actual parameter. There is no need to specify the size of the array.
Example Program: Write a C program to read the array elements using functions.
#include<stdio.h>
void read_array(int a[ ], int n) // no need to specify the size of the array
{
int i;
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
}
void main()
{
int n,b[10],i;
printf(“Enter the number of elements:”);
scanf(“%d”,&n);
printf(“\nEnter the array elements:\n”);
read_array (b, n); // function must be called by passing only the name of the array
printf(“The array elements are:\n);
for(i=0; i<n; i++)
{
printf(“%d”, b[i]);
}
}
1
Principles of Programming using 22POP
Recursion
✔ “The process in which a function calls itself again and again is called as Recursion”.
✔ A function which calls itself again and again is called as Recursive function.
✔ While using recursion, user need to be careful to define exit condition from function; otherwise it
will go in infinite loop.
✔ Recursive functions are very useful to solve many mathematical problems like to calculate
factorial of a number, generating Fibonacci series, etc.
Syntax:
Void main() int recursion ()
{ {
recursion ( ); recursion ( );
} }
1
Principles of Programming using 22POP
printf(“Enter a value for n and r”);
scanf(“%d”,&n);
1
Principles of Programming using 22POP
fact=factorial(n) / (factorial(n-r) * factorial(n));
printf(“\n nCr value =%d”,fact);
}
void main()
{
int n, i, result;
printf(“Enter a value for n ”);
scanf(“%d”, &n);
printf(“ The Fibonacci series is: \
n”); for ( i =0; i < n ; i ++)
{
res = fibonacci (i); printf(“%d\
n”, res);
}
}
1
Principles of Programming using 22POP
ASSIGNMENT QUESTIONS
11. Write a C program to calculate gcd and lcm of 2 integers using functions.