Functions
Functions
Harapriya Mohanta
Function:
A function is a group of statements(instructions) that performs a particular and
predefined task.
The idea behind the function is to put the instructions(performs a specific task)
and make a function so that instead of writing the same code again and again,
we can call the function.
Advantages:
▪ Code reusability
▪ Easier to debug
▪ Divide the large program into several sub-programs(using divide-and-conquer
principle) and provide better memory utilization as well.
▪ It reduces the size of the program due to code reusability.
Harapriya Mohanta
Types of Function:
Function
Pre-defined User-defined
scanf() my_fun()
printf() add()
square(), etc.
Harapriya Mohanta
Pre-defined(Library) Function:
These functions are already dfined on the C library and we can simply use
them by including the appropriate header files in our program. Some of
the common used pre-defined functions are scanf(), printf(). gets(), etc.
User-defined functions:
Functions that are defined ny user according to their needs to perform
some specific task are known as User-defined functions.
Harapriya Mohanta
Is the main() function is predefined or use-defined?
As the program’s execution starts from the main() function, there is a predefined
standard or format that is present so that the compiler knows about it.
Harapriya Mohanta
How to make our own function?
Harapriya Mohanta
Function Declaration(prototype) & Definition:
Declaration
return_type function_name(arg1, arg2,-----);
Definition
Harapriya Mohanta
How to use function?
Harapriya Mohanta
1. Define first, then use 2. Declare first, then use, then Define
#include<stdio.h> #include<stdio.h>
void fun() void fun();
{ int main()
printf(“i am in fun() function”); {
} ------
int main() fun();
{ ------
------ }
fun(); void fun()
------ {
} printf(“i am in fun() function”);
}
Harapriya Mohanta
What is the purpose of function declaration?
Harapriya Mohanta
Be Reminded of:
If we do not provide a prototype declaration of the function in a situation
where it is defined before it gets called, then th eprogram works fine.
However, on the other hand, if the prototype declaration is not present and
we call the function at a point before it is defined, then we get an error.
Harapriya Mohanta
Function calling
• In order to use a function, we have to call it, i.e., function calling. In a program, when we call
a function, the program’s control is transferred to that function and execute the code(body)
of the function.
• To call a function, we simply need to pass the required arguments along with the function name.
• for ex:
add(10,20); #include<stdio.h>
add(x,y); void fun(); //function prototype
int main()
{ o/p:
printf(“i am from main function\n”); i am from main function
fun(); //function calling i am from userdefined function
return 0;
}
// function definition
void fun()
{
printf(“i am from userdefined function\n”);
}
Different ways (types) to define a function:
Harapriya Mohanta
Formal parameters & Actual Parameters
• The aparametrs used in function prototype and definition are knwon as the formal
arguments or parameters.
• The parameters that are used in the function calling are knwon as the actual arguments.
Formal arguments
void add(int a, int b)
{
printf(“sum:%d”,(a+b));
}
int main()
{
int x =10, y =20;
add(x,y);
Actual arguments
return 0;
}
Harapriya Mohanta
Argument/parameter passing to the function
Harapriya Mohanta
Call by Value Call by address