Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, , EEE-II) 2019-2020, semester -I
Subject : C Programming Unit : III
Teacher:- K.Srinivas
FUNCTIONS
Definition:- A function is a piece of code that performs a particular task which can be tested and run individually.
It is a self contained block of statements which perform a specific task. It also Know as subprogram.
Advantages of Functions:-
1. Complex task can be divided into small sub task.
2. Each sub task can be individually tested and debugged easily.
3. It is easy to understand and develop the programs.
4. Functions can be shared by other programs.
C functions are classified into two types:-
1) Pre-defined functions/Built in functions(library functions)
The Pre-defined functions means the task of the function is defined and it can be used by the user in his programs.
Ex. Header Files:- <math.h> MATH LIBRARY FUNCIONS
Math library functions allow the programmer to perform common mathematical calculations.
Some C math library functions are
Function Description Example
sqrt(x) Square root of x sqrt(9.000) is 3.00
exp(x) Exponential function ex exp(1.0) is 2.718282
log(x) Natural logarithm of x(base e) log(2.7182) is 1.0
log10(x) Logarithm of x(base 10) log10(1.0) is 0.0
fabs(x) absolute value of x fabs(5.0) is 5.0
ceil(x) rounds x to the smallest integer ceil(9.2) is 10.0
not less than x
floor(x) rounds x to the largest integer floor(9.2) is 9.0
not greater than x
pow(x,y) x raised to power y pow(2,7) is 128.0
sin(x) Trigonometric sine of x sin(0.0) is 0
(x in radians)
cos(x) Trigonometric cosine of x cos(0.0) is 1
(x in radians)
tan(x) Trigonometric tangent of x tan(0.0) is 0.0
(x in radians)
2) User-defined functions
User defined function has to be developed by the user to write the code for the function then the user can use it in his program. It
can also be used in any programs.
How to use Functions: - Functions can be used in user programs by including the header file related to and call the function name.
This is known as calling the function.
Pre-defined functions:-
Based on the task (similar) of the functions, the functions are grouped into one file that is known as header file.
Ex. stdio.h - it is related to all input/output tasks. Ex. scanf(),printf().
math.h - it is related to all mathematical tasks. Ex. sqrt(),pow().
First in user program they have to include the header files then start using related function in his program.
User defined functions:- In order to make use of user defined function, we need to establish three elements that are
required.
1) Function declaration
2) Function definition
3) Function call
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, , EEE-II) 2019-2020, semester -I
Subject : C Programming Unit : III
Teacher:- K.Srinivas
1. Function Declaration: - Like variables all functions in a c program must be declared, before they are called/invoked. A function
declaration also known as function prototype.
Its advantage is to validate function calls by the compliers.
It consists of three parts.
1) Function type(return type) 2) Function name 3) Argument list/Parameter List
The general format is :- function return type function_name(Argument list/parameter list);
Ex. int square(int m);
float mul(float m, float n);
* If we don’t specify the return type of function, default it returns integer type.
When a function does not take any parameter list and does not return any value, its prototype is written as
void display(void); or display();
A prototype declaration may be placed in two places in a program
1) Above all the functions (including main)
2) Inside a function definition.
When we place the declaration above all the functions (in the global declaration section), the prototype is referred to as a global
prototype. Such declarations are available for all the functions in the program.
When we place it in a function definition (in the local declaration section), this prototype is called local prototype.
2. Function Definition:- A function definition is also known as function implementation. A general format/syntax of function
definition consists of, function header and its task.
Function header: → function return type fuction_name(parameter list)
{ Local variable declarations;
stmt 1;
stmt 2;
--------
return statement;
}
The statements within the openings and closing braces constitute the function implementation statements. This is also known as body of the
Function.
Function Header:-The function header consists of three parts the function type (return type), the function name and the formal
parameter list. Important that semicolon is not supposed to use at the end of the function header.
The function name is any valid C identifier. The name should be appropriate to the task performed by the function. Additional care
must be taken to avoid duplicating library function names.
Arguments Lists / Parameter List:- The parameter list declares the variables that will be receive the data sent by the calling
program. They serve as input data to the function to carry out the specific task. These parameters are also known as formal parameters
list.
Ex: float mul(float x, float y) // x, y are formal parameters.
{ ---------
---------
}
Ex. int sum(int a,b) is illegal/wrong syntax.
Function Body:- The function body contains the declarations and statements necessary for performing the required task.
Local declarations that specify the variable needed by the function.
1) Function statements that performs the task of the function.
2) A return statement that returns the value evaluated by the function.
If a function does not return any value we can omit the return statement. However note that the return type should be specified as
void.
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, , EEE-II) 2019-2020, semester -I
Subject : C Programming Unit : III
Teacher:- K.Srinivas
When a function reaches its return statement, the control is transferred back to the calling function. In the absence of return statement, the
closing brace acts as void return.
3. Function Call:- A function can be called by simply using the function name followed by a list of parameters (parameter list). The
parameter list is known as actual parameter list, if any, enclosed in parenthesis.
When the compiler encounters a function call the control is transferred to the function definition. The function is executed line
by line as described and a value is returned when a return statement is encountered.
The function call can be done in two ways 1) Call by value 2) Call by Address/reference
Ex. Steps involved Functions use sage
Step. 1. void Add(void); /*Declaration of the function/prototype*/
main()
{ printf(“Welcome to C\n”);
Step. 3. Add(); /* Function calling*/
printf(“the control comes back to main\n”);)
Step. 3. Add();/* Function calling*/
}
Step. 2. void Add() /*Function definition */
{ int a,b,c;
printf(“Enter a and b \n”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“a= %d and b= %d sum = %d\n”a,b,c);
}
The above program contains two user-defined function
main() function
Add() function
The program execution begins with main function. During execution of the main function, the first statement is
Add()
Which indicates that the function Add() is to be executed. At this point, the program control is transferred to the function Add(). After
executing the Add() function, which output a, b, c values, then the control is again back to main. Now, the execution continues at the point
when the function call was executed. After executing the printf() statement, the control is again transferred to the Add() function for
printing the a, b, c values once more.
Return Values and Their Types:- A function may or may not send back any value to the calling function. If it does, it is done
through return statement. While it is possible to pass to the called function any number of values, the called function can only return one
value per call, at the most. A Function can have more than one return statements but it can return only one return value.
The return statement can take one of the following forms
return; or return(expression) or return(value);
The first one does not return any value; it acts much as closing brace of the function. When a return is encountered, the control is
immediately passed back to the calling function.
The user defines function may be written in four (4) ways:-
1. Functions without(no) arguments and no return value
2. Functions without(no) arguments and with return value
3. Functions with arguments and without(no) return value
4. Functions with arguments and with return value
1) Functions with no arguments and no return value:- A function is invoked without passing any formal parameters to
the called function. The called function does not return any value to the calling function.
Ex. /* Function without(no) arguments and with no return value */
#include<stdio.h>
void Add();
main()
{ void Add();
printf(“Function control comes back to main\n”);
}
void Add()
{ int a,b,c;
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, , EEE-II) 2019-2020, semester -I
Subject : C Programming Unit : III
Teacher:- K.Srinivas
printf(“Enter a,b values\n”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“a = %d , b = %d and sum of a and b = %d\n”,a,b,c);
}
Input : 4 5
Output : a =4 , b = 5 and sum of a and b = 9
2) Functions without (no) arguments and with return values.
This type of function passes no formal arguments to a called function but the function return a value to the calling function.
Ex. /* Function without(no) arguments and with return value */
#include<stdio.h>
int Add();
main()
{ int r;
r=Add();
printf(“sum of a and b = %d\n, r);
}
int Add()
{ int a,b,c;
printf(“Enter a,b values\n”);
scanf(“%d%d”,&a,&b);
c=a+b;
return c;
}
3) Function with arguments and no return value.
This type of function passes some formal arguments to a called function but the function does not return a value to the calling function.
/* Functions with arguments and with no return value */
#include<stdio.h>
void Add(int,int);
main()
{ int x,y;
printf(“Enter a,b values\n”);
scanf(“%d%d”,&x,&y);
Add(x,y);
}
void Add(iny a,int b)
{ int a,b,c;
c=a+b;
printf(“a = %d , b = %d and sum of a and b = %d\n”,x,y,c);
}
4) Function with arguments and with return value.
This type of function passes formal arguments to a called function and the function return a value to the calling function.
/* Functions with arguments and with return value */
#include<stdio.h>
void Add(int,int);
main()
{ int x,y,r;
printf(“Enter a,b values\n”);
scanf(“%d%d”,&x,&y);
r=Add(x,y);
printf(“x = %d , y = %d and sum of a and b = %d\n”,x,y,r);
}
void Add(iny a,int b)
{ int a,b,c;
c=a+b;
return c; }
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, , EEE-II) 2019-2020, semester -I
Subject : C Programming Unit : III
Teacher:- K.Srinivas
Example of functions
//1. Sum of the digits of given number //2. Factorial of a given number
#include<stdio.h> #include<stdio.h>
int sumofdigits(int n); int factorial(int n);
void main() void main()
{ int n, sum; { int n, fact;
printf("Enter the number N\n"); printf("Enter the number N\n");
scanf("%d",&n); scanf("%d",&n);
sum=sumofdigits(n); fact=factorial(n);
printf("Sum of digits of %d = %d \n",n,sum); printf("Factorial of given number %d = %d \n",n,fact);
} }
int sumofdigits(int n) int factorial(int n)
{ int sum=0,d; { int i, f=1;
for(;n>0;n=n/10) for(i=1;i<=n;i++)
{ d=n%10; f = f *i;
sum =sum+d; return i;
} }
return sum;
}
Passing Parameter Techniques:-
The function call /invoking i.e using parameters passing can be done in two ways 1) Call by value 2) Call by reference.
These are known as parameter passing techniques.
Call by value:- In this the actual parameters/list of arguments will not be affected by changing its value in the function task.
In function call the arguments are known as actual parameters.
In function header
In this method the actual parameters values are copied to formal parameters and the changes are done on the formal parameters.
And also the scope of variable rules the formal parameters are within the function block once the control comes out the life of the
variables lost. Because of this the changes will not affect the actual parameters.
Ex. /* swapping the Elements or interchanging the elements using call by value method */
#include<stdio.h>
void SWAP(int,int);
main()
{
int x,y;
printf(“Enter the values of x,y\n”);
scanf(“%d%d”,&x,&y);
printf(“Before calling SWAP function \n x = %d , y = %d \n”,x,y);
SWAP(x,y);
printf(“After calling SWAP function \n x = %d , y = %d \n”,x,y);
}
void SWAP(int x,int y)
{ int c;
c=x;
x=y;
y= c;
printf (“with in SWAP function \n x = %d , y = %d \n”,x,y);
}
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, , EEE-II) 2019-2020, semester -I
Subject : C Programming Unit : III
Teacher:- K.Srinivas
INPUT:
Enter the values of x, y 10 20
OUTPUT:
Before calling SWAP function x = 10, y = 20
Within SWAP function x = 20, y = 10
After calling SWAP function x = 10, y = 20
1) Call by reference:- In this the actual parameters/list of arguments will be affected by changing its value in the function Block.
In this method the actual parameters address are passed are copied to formal parameters and the changes are done on the formal
parameters affects the actual parameters. Because they refer to same addresses. This is done using address operator and pointers.
Passing arrays as arguments to a function:- Arrays are by default passed as call by reference method.
How to pass single and multidimensional arrays as parameters to a function?
As follows
1. Single dimension
int a[10];
function returntype function name (parameters list);//function prototype
int add(int [size],int) //(int [] this tells as array is parameter integer array with size)
Function definition
int add(int a[],int n)
{statements
}
Calling Function
add(a,n);
// single dimension array passing as parameter to function
#include<stdio.h>
void add(int [20],int);
void main()
{ int i,n,a[20];
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the value of array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
add(a,n);
printf("Addition of array\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
void add(int b[20],int n)
{ int i;
for(i=0;i<n;i++)
b[i]=b[i]+2;
}
OUTPUT:- Enter the value of n 5
Enter the value of array elements
12345
Addition of array
34567
Department of Computer Science and Engineering
M.V.S.R.Engineering College, Nadergul, Hyderabad.
B.E(1st Year, , EEE-II) 2019-2020, semester -I
Subject : C Programming Unit : III
Teacher:- K.Srinivas
2. Two dimension
int a[10][10],b[10][10],c[10][10];
function returntype function name (parameters list);//function prototype
int add(int [][10],int[][10],int [][10],int) //(int [] this tells as array is parameter and integer 2D as
array with size )
Note:- When we are passing two dimensional array as arguments rows size is optional, columns size is must.
Function definition
int add(int a[][10],int b[][10],intc[][10],int n)
{statements
}
Calling Function
add(a,b,c,n);
//Two dimensions arrays passing as parameters to function
//matrix addition
#include<stdio.h>
matadd(int [][5],int[][5],int[][5],int);
void main()
{ int i,j,n,a[5][5],b[5][5],c[5][5];
printf("Enter the matrix size\n");
scanf("%d",&n);
printf("Enter the %d matrix A elements\n",n*n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the %d matrix B elements \n",n*n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
matadd(a,b,c,n);
printf("The matrix addition display in main\n");
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
printf("%d ",c[i][j]);
printf("\n");
}
}
matadd(int a[][5],int b[][5],int c[][5],int n)
{ int i,j;
printf("The matrix addition process going on wait\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("The matrix addition process completed\n");
printf("addition matrix in function\n");
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
printf("%d ",c[i][j]);
printf("\n");
}
}
OUTPUT:- Enter the matrix size 2
Enter the matrix A elements
12
34
Enter the matrix B elements
11
11
The matrix addition display in main
23
45