2. Contents
• Introduction to function
• Definition of functions
• Return value and their types
• Function calls
• Function declaration
• Category of function
• Nesting of function
• Recursion
• Passing array to functions
• Passing 2 D array to the function
• Passing string to functions
3. Functions in C programming
• Depending on whether a function is defined
by the user or already included in C compilers,
there are two types of functions in C
programming
• There are two types of functions in C
programming:
• Standard library functions
• User defined functions
•
4. Standard library functions
• The standard library functions are built-in functions in
C programming to handle tasks such as mathematical
computations, I/O processing, string handling etc.
• These functions are defined in the header file. When
you include the header file, these functions are
available for use. For example:
• The printf() is a standard library function to send
formatted output to the screen (display output on the
screen). This function is defined in "stdio.h" header file.
• There are other numerous library functions defined
under "stdio.h", such as scanf(), fprintf(), getchar() etc.
Once you include "stdio.h" in your program, all these
functions are available for use.
5. User defined function
• C allow programmers to define functions. Such
functions created by the user are called user-
defined functions
• Advantages of user-defined function
• The program will be easier to understand,
maintain and debug.
• Reusable codes that can be used in other
programs
• A large program can be divided into smaller
modules. Hence, a large project can be divided
among many programmers
6. Introduction of user defined Functions
• A function is self contained block of codes that programs
some specific well defined tasks
Example:
void message()
{
printf (“nThis is inside function message”);
}
void main()
{
printf(“This is in main before function call”);
message();
printf(“nthis is after a function call”);
}
7. Function declaration/ prototype
• It provides:
1. The name of the function
2. Specifies the type of value to be returned
3. Provides the number and type of argument
that must be supplied to the function
4. Facilate error checking between calls to a
function and the corresponding function
definition
8. Function definition
• The function definition is similar to the
declaration but there is no semicolon
• The first line of the function definition is
known as function declarator that is followed
by function body
• The declarator and the prototype must be
same function name number of arguments,
argument types and return type
9. Syntax:
return_type function name(type1 arg1,type2
arg2……….typen argn)
{
body of function;
}
WHERE,
return_type= data type being returned
function name= name of the function
type1,type2,……typen represents the data types of the
arguments arg1,arg2,………argn
10. • Suppose, you need to create a circle and color
it depending upon the radius and color. You
can create two functions to solve this
problem:
• createCircle() function
• color() function
12. Return statement
• One of the main feature of function is that it can
return a value to the calling function
• In order for a function to return a value to the
calling function, the function should have return
statement
• This statement comprises of the keyword called
return,followed by the value to be returned
• This value may be a variable constant or even
another function call
13. Execution of return statement
• When a return statement is executed,it
immediately transfers the control back to the
calling program
• A function call can return only a single value at a
time
• As soon as the return statement is executed the
function terminates if the function doesn’t return
any value
• In this condition it is not necessary to include the
return statement in the function
15. Function with no arguments and no
return value
• When a function has no arguments, it doesnot
receive any data from the calling function
• When it doesnot return any value, the calling
function `doesnot receive any data from the
called function
• So there is no data transfer between calling
function and called function
16. No arguments passed but a return
value
• It may be the occasion where we may need to
design function that may not take any
arguments
• However it returns a value to the calling
function
• Typically we have getchar function declared in
header file<stdio.h>
• Getchar function has no parameters but it
returns an integer type data that represents a
character
17. Argument passed and no return
values
• Here function value receives data from the
calling function through arguments
• However it does not send back any value
• Rather it displays the results of calculations at
the terminal
18. Nesting of functions
• If we are calling any function inside another
function call is known as nesting function call.
• Sometime it converts a difficult program in
easy one.
• Main can call function1 which calls function2
which calls function3…………. and So on
• Nesting of function can be done deeply to a
large limit
19. • Find the maximum number among five
different integers using nested function call:
• int max(int x,int y){return x>y?x:y;}
• void main(){
• int m;
• m=max(max(4,max(11,6)),max(10,5));
• printf("%d",m);
• getch();
• }
20. Recursion
• Recursion is a programming technique that allows
the programmer to express operations in terms
of themselves
• This takes the form of a function that calls itself
• A useful way to think of recursive functions is to
imagine them as a process being performed
where one of the instructions is to "repeat the
process"
• This makes it sound very similar to a loop
because it repeats the same code, and in some
ways it is similar to looping
21. Conclusion
• A return statement is required if the return
type is anything other than void
• If the function doesnot return any value the
return type must be declared void
• Function returns integer value by default
• A function definition can be placed either
after or before the main function