Y. N. D. Aravind presents on functions in C programming. The presentation covers:
- The objectives of functions, parameters, arrays, and recursion.
- The definition of a function as reusable block of code that performs a specific task.
- The four categories of functions based on arguments and return values.
- Passing arguments to functions by value (copying) versus by reference (address).
3. A function is a block of code that performs a specific task. It has a name and it is reusable
.It can be executed from as many different parts in a program as required, it can also return
a value to calling program.
All executable code resides within a function. It takes input, does something with it, then
give the answer.
A computer program cannot handle all the tasks by itself. It requests other program like
entities called functions in C. We pass information to the function called arguments which
specified when the function is called. A function either can return a value or returns nothing.
Function is a subprogram that helps reduce coding.
return_type function_name (argument list)
{
Set of statements â Block of code
}
@2020 Presented By Y. N. D. Aravind
3
3
Function in C
4. Basically there are two reasons because of which we use functions
1. Writing functions avoids rewriting the same code over and over.
For example - if you have a section of code in a program which
calculates the area of triangle. Again you want to calculate the
area of different triangle then you would not want to write the
same code again and again for triangle then you would prefer to
jump a "section of code" which calculate the area of the triangle
and then jump back to the place where you left off. That section of
code is called ..........â function'.
2. Using function it becomes easier to write a program and keep
track of what they are doing. If the operation of a program can be
divided into separate activities, and each activity placed in a
different function, then each could be written and checked more
or less independently. Separating the code into modular functions
also makes the program easier to design and understand.
@2020 Presented By Y. N. D. Aravind
4
4
Why use Function
5. ï¶ Once a function is defined and called, it takes some data from the calling
function and returns a value to the called function.
ï¶ When ever a function is called, control passes to the called function and
working of the calling function is stopped.
ï¶ When the execution of the called function is completed, a control returns
back to the calling function and executes the next statement.
ï¶ The values of actual arguments passed by the calling function are received
by the formal arguments of the called function.
ï¶ The number of actual and formal arguments should be the same. If the
formal arguments are more than the actual arguments then the extra arguments
appear as garbage.
ï¶ The function operates on formal arguments and sends back the result to the
calling function.
@2020 Presented By Y. N. D. Aravind
5
5
How Functionâs Work
6. Def:- The function which is referring another function is called âcalling
functionâ.
Def:- The function which is referenced by another function is called âcalled
functionâ
@2020 Presented By Y. N. D. Aravind 6
Calling Function and Called Function
Actual Arguments and Formal Arguments
Def:- The arguments of calling functions are called âactual argumentsâ.
Def:- The arguments of called function are âformal argumentsâ.
Local Variables and Global Variables
Local variables:-
The local variables are defined within the body of the function or the block.
Other function cannot access these variables.
Global variables:-
Global variables are defined outside the main() function. Multiple functions
can use them.
8. C provides library functions for performing some operations. These
functions are present in the c library and they are predefined.
For example sqrt() is a mathematical library function which is used for
finding the square root of any number .The function scanf and printf()
are input and output library function similarly we have strcmp() and
strlen() for string manipulations. To use a library function we have to
include some header file using the preprocessor directive #include.
For example to use input and output function like printf() and scanf() we
have to include stdio.h, for math library function we have to include
math.h for string library string.h should be included.
@2020 Presented By Y. N. D. Aravind
8
8
Library Functionâs in C
9. A user can create their own functions for performing any specific
task of program are called user defined functions. To create and
use these function we have to know these 3 elements.
1. Function Declaration
2. Function Definition
3. Function Call
@2020 Presented By Y. N. D. Aravind 9
User defined Functionâs in C
10. 1. Function Declaration
The program or a function that calls a function is referred to as the calling program or calling
function. The calling program should declare any function that is to be used later in the program
this is known as the function declaration or function prototype.
2. Function Definition
The function definition consists of the whole description and code of a function. It tells that what
the function is doing and what are the input outputs for that. A function is called by simply writing
the name of the function followed by the argument list inside the parenthesis. Function definitions
have two parts:
Function Header
The first line of code is called Function Header. int sum( int x, int y)
It has three parts
(i). The name of the function i.e. sum
(ii). The parameters of the function enclosed in parenthesis
(iii). Return value type i.e. int
Function Body
Whatever is written with in { } is the body of the function.
3. Function Call
In order to use the function we need to invoke it at a required place in the program. This is known
as the function call.
@2020 Presented By Y. N. D. Aravind 10
User defined Functionâs in C
11. A function depending on whether arguments are present or not and
whether a value is returned or not may belong to any one of the
following categories:
I. Functions with no arguments and no return values.
II. Functions with arguments and no return values.
III. Functions with arguments and return values.
IV. Functions with no arguments and return values.
@2020 Presented By Y. N. D. Aravind 11
Categories Of Functionâs
12. There is no data communication between the calling portion of a
program and a called function block. The function is invoked bya calling
environment by not passing any formal arguments and the function also
does not return back any value to the caller.
No Input
No Output
No data communication between functions
@2020 Presented By Y. N. D. Aravind 12
Functionâs with no arguments and no return values
function1()
{
---------------------
---------------------
---------------------
function2(); /* function call*/
---------------------
---------------------
}
function2()
{
---------------------
---------------------
---------------------
---------------------
---------------------
}
13. #include<stdio.h>
#include <conio.h>
void maximum();
{
int x,y,z,max;
printf( â Enter three Numbers nâ);
scanf(â%d%d%dâ, &x,&y,&z);
max = x;
if(y > max)
max = y;
if(z > max)
max = z;
printf(âMaximum = %d nâ,max);
}
void main()
{
clrscr();
maximum();
getch();
}
@2020 Presented By Y. N. D. Aravind 13
Functionâs with no arguments and no return values
Input- Output
Enter three Numbers
1 2 3
Maximum = 3
14. The second type of user defined function passes some formal arguments
to a function but the function does not return back any value to the caller.
It is an one â way data communication between the calling portion of a
program and a called function block.
Values
of arguments
No return values
One â way data communication between functions
@2020 Presented By Y. N. D. Aravind 14
Functionâs with arguments and no return values
function1()
{
---------------------
---------------------
---------------------
function2(x); /* function call*/
---------------------
---------------------
}
function2(a)
{
---------------------
---------------------
---------------------
---------------------
---------------------
}
15. #include<stdio.h>
#include <conio.h>
void maximum(int, int, int);
void main()
{ int x,y,z;
clrscr();
printf( â Enter three Numbers nâ);
scanf(â%d%d%dâ, &x,&y,&z);
maximum(x,y,z);
getch();
}
void maximum(int a, int b, int c)
{ int max;
max = a;
if(b > max)
max = b;
if(c > max)
max = c;
printf(âMaximum = %d nâ,max);
}
@2020 Presented By Y. N. D. Aravind 15
Functionâs with arguments and no return values
Input- Output
Enter three Numbers
1 2 3
Maximum = 3
16. The third type of user defined function passes some formal arguments to
a function from a calling portion of the progra and the computed value, if
anyy, is transferred back to the caller. Data are communicated between
the calling portion of a program and a called function block.
Values
of arguments
Function results
Two â way data communication between functions
@2020 Presented By Y. N. D. Aravind 16
Functionâs with arguments and with return values
function1()
{
---------------------
---------------------
---------------------
function2(x); /* function call*/
---------------------
---------------------
}
function2(a)
{
---------------------
---------------------
---------------------
---------------------
---------------------
return(z);
}
17. #include<stdio.h>
#include <conio.h>
int maximum(int x, int y, int z);
void main()
{ int x,y,z,maxi;
clrscr();
printf( â Enter three Numbers nâ);
scanf(â%d%d%dâ, &x,&y,&z);
maxi = maximum(x,y,z);
printf(âMaximum = %d nâ,maxi);
getch();
}
int maximum(int x, int y, int z)
{ int max;
max = x;
if(y > max)
max = y;
if(z > max)
max = z;
return(max);
}
@2020 Presented By Y. N. D. Aravind 17
Functionâs with arguments and with return values
Input- Output
Enter three Numbers
1 2 3
Maximum = 3
18. The fourth type of user defined function, the function is invoked by a
calling environment by not passing any formal arguments to a function
but the computed value, if any, is transferred back to the caller. Data are
communicated between the calling portion of a program and a called
function block in one way.
No Input
Function results
One â way data communication between functions
@2020 Presented By Y. N. D. Aravind 18
Functionâs with no arguments and return values
function1()
{
---------------------
---------------------
---------------------
function2(); /* function call*/
---------------------
---------------------
}
function2()
{
---------------------
---------------------
---------------------
---------------------
---------------------
return(z);
}
19. #include<stdio.h>
#include <conio.h>
int maximum();
void main()
{ int max;
clrscr();
max = maximum();
printf(âMaximum = %d nâ,max);
getch();
}
int maximum()
{ int x,y,z,max;
printf( â Enter three Numbers nâ);
scanf(â%d%d%dâ, &x,&y,&z);
max = x;
if(y > max)
max = y;
if(z > max)
max = z;
return(max);
}
@2020 Presented By Y. N. D. Aravind 19
Functionâs with no arguments and return values
Input- Output
Enter three Numbers
1 2 3
Maximum = 3
20. Most programming languages have 2 strategies to pass parameters. They are
i. pass by value
ii. pass by reference
Pass by value (or) call by value :-
In this method calling function sends a copy of actual values to called function, but the
changes in called function does not reflect the original values of calling function.
Pass by reference (or) call by address :-
In this method calling function sends address of actual values as a parameter to called
function, called function performs its task and sends the result back to calling function.
Thus, the changes in called function reflect the original values of calling function. To
return multiple values from called to calling function we use pointer variables.
Calling function needs to pass ..â&â operator along with actual arguments and called
function need to use â*â operator along with formal arguments. Changing data through
an aaddress variable is known as indirect access and â*â is represented as indirection
operator.
@2020 Presented By Y. N. D. Aravind 20
Parameter Passing
21. Pass by value (or) call by value :-
Function call by value is the default way of calling a function in C
programming. Before we discuss function call by value, lets understand the
terminologies that we will use while explaining this:
Actual parameters: The parameters that appear in function calls.
Formal parameters: The parameters that appear in function declarations.
When we pass the actual parameters while calling a function then this is known
as function call by value. In this case the values of actual parameters are copied
to the formal parameters. Thus operations performed on the formal parameters
donât reflect in the actual parameters.
Call By Value: In this parameter passing method, values of actual parameters
are copied to functionâs formal parameters and the two types of parameters are
stored in different memory locations. So any changes made inside functions are
not reflected in actual parameters of caller.
@2020 Presented By Y. N. D. Aravind 21
Pass by Valur (or) Call By Value
22. Pass by reference (or) call by address :-
Before we discuss function call by reference, lets understand the terminologies
that we will use while explaining this:
Actual parameters: The parameters that appear in function calls.
Formal parameters: The parameters that appear in function declarations.
When we call a function by passing the addresses of actual parameters then this
way of calling the function is known as call by reference. In call by reference,
the operation performed on formal parameters, affects the value of actual
parameters because all the operations performed on the value stored in the
address of actual parameters. It may sound confusing first but the following
example would clear your doubts.
Call by Reference: Both the actual and formal parameters refer to same
locations, so any changes made inside the function are actually reflected in
actual parameters of caller.
@2020 Presented By Y. N. D. Aravind 22
Pass by Reference (or) Call by address
23. Thank You
@2020 Presented By Y. N. D. Aravind
Presented By
Y. N. D. ARAVIND
M.Tech, Dept of CSE
Newtonâs Group Of Institutions, Macherla
23