Functions:
A function is a self contained sub-program which is designed for a particular task.
Example:-
if we want to find circumference of different circles in a project many times, then instead of writing same code every where we could use functions.
/*****************************************************************************/
/* Author section */
/* we can write about the file, author and its functionality etc., */
/* Author:- EMBEDDED ARCHITECTS */
/* This is a simple example to explain about the functions in C */
/* */
/****************************************************************************/
/*
* This section is the header file section
* where the header files required for this program are included
* here
*/
#include <stdio.h>
#include <conio.h>
/*
* This section is the MACRO section
* where the MACRO required for this program are included
* here
*/
#define PI 14
/*
* In this section we can declare the functions
* or we can declare functions in a header file and
* include the header file above in header file section
*/
int calc_circum(int );
/*
* This is where the actual code starts
*
*/
int main()
{
int cir_1_rad = 10, circumference = 0;
circumference = calc_circum(circle_1); // function calling
printf("\n %d ", circumference);
return 0;
}
/*
* Function definition for calc_circum
* @in: radius - which is of data type integer
* @out: cicumference - which is of data type integer
*
* @operation: This function calculates the circumference of the circle using the radius which passed as an * argument to this function
*/
int calc_circum(int radius)
{
return (PI*radius*radius);
}
-> we have to declare a function before the calling, doing so tells the compiler that function is defined with the return data type and arguments data type/number of arguments.
-> main() is an user-defined function with predefined signature for linker
-> main() is an identifier in the program which indicates startup point of an application
syntax:
return-type function_name(parameters)
{
body of function;
-------------;
-------------;
return statement;
}
pass-by value:
This is one of the technique to send arguments to a function.
When we are calling a function the copy of the variables is created on stack which are local to that function.
The changes made inside the function not reflect outside.
Pass-by reference:-
This is another technique of sending arguments to a function.
In this case we are sending address of variables. The changes made inside a function will also reflect outside of that function. This is also used to return more than one value from a function.
If we are passing more actual arguments than formal arguments, extra arguments are dropped.
If we are passing less actual arguments than formal arguments, then garbage value is passed.
If actual argument is of one type and formal argument is of other type then compiler automatically converts it, if legal.
Recrusion:-
0 comments:
Post a Comment