You are on page 1of 16

User Defined Functions

❑Function : -
A function is a self contained block of
statements that perform a coherent (સુસગ ં ત)
task of some kind in other words , Function in
programming is a segment that groups a
number of program statements to perform
specific task.
User Defined Functions
➢A C program has at least one function main().
➢Without main() function there is technically
no C program.
➢Every C program can be thought of as a
collection of functions.
User Defined Functions
❑Types of C functions : -
➢ Basically , there are two types of functions in
C on basis of whether it is defined by user or
not
1.Library function
2. user defined function
User Defined Functions
Library functions are the in built functions in C programming
system.
❑ For example : -
➢ main() the execution of every C program starts from this main()
function.
➢ printf() which is used for displaying output in C ( is a library
function)
➢ scanf() which is used for taking input in C ( is a library function)
➢ Moreover clrscr() , getch() , sqrt() ,abs() , . . . etc are some examples
of library functions.
User Defined Functions
❑ User defined function : -
➢ C provides programmer to define their own function
according to their requirement known as user defined
functions.
➢ Suppose , a programmer wants to find factorial of a
number and check whether it is prime or not in same
program then he / she can create two separate user
defined functions in that program :
One for finding factorial and other for checking whether it
is prime or not.
User Defined Functions
➢ Every C program starts executing the codes inside main() function
➢ When the control of program jumps to function_name() inside
main()function
➢ The control of program jumps to function_name and executes the codes
inside it.
➢ When , all the codes inside that user - defined function are executed.
➢ Then control of the program jumps to the statement just after
function_name() from where it is called.
➢ The function_name is an identifier and should be unique.
User Defined Functions
❑ Advantages of user defined functions
➢ User defined function helps to decompose(ઘટાડવુ)ં the
large program into small segments which makes program
easy to understand , maintain and debug (identify and
remove error from ......)
➢ If repeated codes occurs in a program , function can be
used to include those codes and execute when needed by
calling that function.
➢ Programmer working on large project can divide the
workload by making different functions.
User Defined Functions
❑Function prototype(Declaration) : -
➢ Every function in C programming should be
declared before they are used.
➢ This type of declaration are called function
prototype.
➢ Function prototype gives compiler information
about function name type of arguments to be
passed and return type of the function.
User Defined Functions
❑Return statement : -
➢Return statement is used for returning a value
from function definition to calling function
❑Syntax of Return statement : -
return;
return(expression);
User Defined Functions
❑For example
return;
return a;
return (a+b);
➢ The data type of expression in return statement
should also match the return type of function.
User Defined Functions
❑Void data type : -
➢ void is an empty data type that has no value.
➢ Using void data type we can specify the type of a
function.
➢ UDF with void data type is function which does
not return any value.
➢ It is good practice to avoid functions that does
not return any value to the calling function.
User Defined Functions
❑Types of user defined functions : -
➢ For better understanding of arguments and return in functions , user
defined functions can be categorized as
1. User defined function without arguments and without return value
2. User defined function with arguments and without return value
3. User defined function without arguments and with return value
4. User defined function with arguments and with return value
User Defined Functions
❑User defined function without arguments and
without return value : -
➢ When a function has no argument it does not receive a data from the
calling function.
➢ Similarly when it does not return a value the calling function does not
receive any data from called function.
➢ In fact there is no data transfer between the calling function and the called
function.
User Defined Functions
❑User defined function with arguments and without
return value : -
➢ The function includes arguments but does not return anything
➢ One way communication takes place between the calling
function and the called function.
➢ The data is passed from the calling function to the called
function but does not receive any value from the called
function.
User Defined Functions
❑User defined function without arguments and with
return value : -
➢ This function does not receive any data from calling function
but does send some value to calling function.
User Defined Functions
❑User defined function with arguments and with
return value : -
➢ Here the user defined function includes arguments and return
a value.
➢ Two way communications takes place between the calling
function and the called function.
➢ The data is passed from the calling function to the called
function and receive a return value from the called function.

You might also like