Function
Function
Module-3
Function
• A function is a self contained block of codes or sub programs with a
set of statements that perform some specific task or coherent task
when it is called.
• Suppose you have a task that is always performed exactly in the same
way-say a bimonthly servicing of your motorbike. When you want it
to be done, you go to the service station and say, “ it’s time, do it
now”. You don’t need to be told how the job is done. You assume the
bike would be serviced in the usual way, the mechanic does it.
• That is function is just like hiring a person to do a specific job for you.
Types of function
Any ‘C’ program contain at least one function i.e main().
There are basically two types of function those are
1. Library function
2. User defined function
1. Library function or System defined function
• System defined function can’t be modified, it can only read and can
be used. These function are supplied with every C compiler. Source
of these library function are pre complied and only object code get
used by the user by linking to the code by linker
System defined function description:
• Function definition : predefined, precompiled, stored in the library
• Function declaration : In header file with or function prototype.
• Function call : By the programmer
Steps to convert source code into executable code
2. User defined function
message();
}
void message()
{
printf(“have a Nice Day!\n");
main();
}
• A function can be called any number of times.
• For example
#include<stdio.h>
void message();
void main()
{
}
• A function can call itself called as recursion.
• A function can be called from another function but a function can not be defined in another function.
#include<stdio.h>
void message();
void main()
{
}
}
Why Use functions
• Writing functions avoids rewriting the same code over and over.
Suppose you have a section of code in your program that
calculates area of a triangle. If later in the program you want to
calculate the area of a different triangle, you won’t like it if you
are required to write the same instructions all over again. Instead,
you would prefer to jump to a ‘section of code’ that calculates
area and then jump back to the place from where you left off. This
section of code is nothing but a function.
Why Use functions
• Using functions it becomes easier to write programs 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.
Notes
• Don’t try to cram the entire logic in one function. It is a very
bad style of programming. Instead, break a program into small
units and write functions for each of these isolated
subdivisions. Don’t hesitate to write functions that are called
only once. What is important is that these functions perform
some logically isolated task.
#include<stdio.h>
Control flow in function example
void a();
void b();
void c();
void main()
{
printf("\n I am in main");
a();
printf("\n I am finally back in main");
}
void a()
{
printf("\n I am in a");
b();
printf("\n I am back in a");
}
void b()
{
printf("\n I am in b");
c();
printf("\n I am in back b");
}
void c()
{
printf("\n I am in c");
}
Question
1. A function called itself is known as………..
Question
Main() function can be called by other function. True/False
Question
A function may be called more than once from any other
function. True/False
Question
It is necessary for a function to return some value
Passing Values between Functions
• The mechanism used to convey information to the function is
the ‘argument’ or parameter.
• In the printf( ) and scanf( ) functions; the format string and the
list of variables used inside the parentheses in these functions
are arguments.
Passing Values between Functions
1. In this program, from the function
main( ) the values of a, b are
passed on to the function swap( ),
by making a call to the function swap( )
and mentioning a and b in the parentheses:
swap( a, b) ; In the swap( )
function these values get collected in two variables
x and y: void swap( x, y ) int x, y ;
Passing Values between Functions
2. The variables a and b are called ‘actual arguments’, whereas
the variables x and y are called ‘formal arguments’. Any number
of arguments can be passed to a function being called. However,
the type, order and number of the actual and formal arguments
must always be same.
Instead of using different variable names x, y and z, we
could have used the same variable names a, b and c. But the
compiler would still treat them as different variables since they
are in different functions.
Passing Values between Functions