Module3 Functions
Module3 Functions
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 sqrt(float num)
{ {
float n,res; statement-1;
printf(“Enter the value of n:\n”); statement-2;
scanf(“%f”,&n); ……………
res=sqrt(n); return statement;
printf(“Square root=%f”,res); }
}
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.
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
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();
}
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
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.
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);
}
separate file
✔ 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)
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
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;
}
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);
}
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]);
}
}
{ {
recursion ( ); recursion ( );
} }
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);
}
}
ASSIGNMENT QUESTIONS
11. Write a C program to calculate gcd and lcm of 2 integers using functions.