C langugae : Functions

All the instructions of a C program are contained in functions . Each function performs a certain task. A special function name is main( ): the function with this name is the first one to run when the program starts. All other functions are subroutines of the main( ) function (or otherwise dependent procedures, such as call-back functions), and can have any names you wish.
A function in C plays the same role that functions, subroutines, and procedures play in other languages, although the details might differ. Some functions cause an action to take place. For example, printf() causes data to be printed on your screen. Some functions find a value for a program to use. For instance, strlen() tells a program how long a certain string is. In general, a function can both produce actions and provide values.
The definition of a function consists of a function head (or the declarator), and a function block . The function head specifies the name of the function, the type of its return value, and the types and names of its parameters, if any. The statements in the function block specify what the function does. The general form of a function definition is as follows:
In the function head, name is the function's name, while type consists of at least one type specifier, which defines the type of the function's return value. The return type may be void or any object type, except array types. Furthermore, type may include the function specifier inline, and/or one of the storage class specifiers extern and static.
A function cannot return a function or an array. However, you can define a function that returns a pointer to a function or a pointer to an array.
The parameter declarations are contained in a comma-separated list of declarations of the function's parameters. If the function has no parameters, this list is either empty or contains merely the word void.
Scope of Function Variables
Only a limited amount of information is available within each function. Variables declared within the calling function can't be accessed unless they are passed to the called function as arguments. The only other contact a function might have with the outside world is through global variables.
Local variables are declared within a function. They are created anew each time the function is called, and destroyed on return from the function. Values passed to the function as arguments can also be treated like local variables.
Static variables are slightly different, they don't die on return from the function. Instead their last value is retained, and it becomes available when the function is called again.
Global variables don't die on return from a function. Their value is retained, and is available to any other function which accesses them.

Function types
There are three types of functions: special functions, standard (built-in, predefined) functions, and user-defined functions. Special Functions
They have predefined names: init(), start(), and deinit(). These may not be used as the names of any other functions. A detailed consideration of special functions is given in Special functions. We only say here that the basic code of a program is located inside these functions.
The special feature of special functions is the fact that they are called for execution by the client terminal. Although special functions have all the properties of functions in general, they are not usually called from the program if this program is coded properly.
Standard (built-in, predefined) Functions
These functions are also called as 'library functions'.Some compilers (for example, GCC[7]) provide built-in versions of many of the functions in the C standard library; that is, the implementations of the functions are written into the compiled object file, and the program calls the built-in versions instead of the functions in the C library shared object file. This reduces function call overhead, especially if function calls are replaced with inline variants, and allows other forms of optimization (as the compiler knows the control-flow characteristics of the built-in variants), but may cause confusion when debugging (for example, the built-in versions cannot be replaced with instrumented variants).
However, the built-in functions must behave like ordinary functions in accordance with ISO C. The main implication is that the program must be able to create a pointer to these functions by taking their address, and invoke the function by means of that pointer. If two pointers to the same function are derived in two different translation unit in the program, these two pointers must compare equal; that is, the address comes by resolving the name of the function, which has external (program-wide) linkage.
These functions are provided by system. These functions are stored in library files. e.g. : - scanf(), printf(), strcpy, strlwr, strcmp, strlen, strcat
User-Defined Functions
The functions which are created by user for program are known as 'User defined functions'. These are those functions which are defined by the user at the time of writing program. Functions are made for code reusability and for saving time and space.It provides modularity to the program. Easy code Reuseabillty. In case of large programs with thousands of code lines, debugging and editing becomes easier if you use functions.User-defined functions are utilized in programs with both function descriptions and function calls.

Function Type

Function description

Function call

Special

Applicable

Not applicable (*)

Standard

Not applicable

Applicable

User-defined

Applicable

Applicable

PARAMETERS
There are two other categories that are also referred to as "parameters". They are called "parameters" because they define information that is passed to a function.
*Actual parameters are parameters as they appear in function calls.
*Formal parameters are parameters as they appear in function declarations.
Actual Parameters
The actual value that is passed into the method by a caller. All actual arguments (those supplied by the caller) are evaluated. There is no implied order in which these arguments are evaluated, but all arguments are evaluated and all side effects completed prior to entry to the function.
Formal Parameters
The identifier used in a method to stand for the value that is passed into the method by a caller.Each formal argument is initialized with its corresponding actual argument in the expression list. (A formal argument is an argument that is declared in the function header and used in the body of a function.) Conversions are done as if by initialization — both standard and user-defined conversions are performed in converting an actual argument to the correct type. The initialization performed is illustrated conceptually.
// expre_Formal_and_Actual_Arguments.cpp
void func( long param1, double param2 );
int main()
{
long i = 1;
double j = 2;
// Call func with actual arguments i and j.
func( i, j );
}
// Define func with formal parameters param1 and param2
void func( long param1, double param2 )
{
}
Types of User-defined Functions in C Programming
User-defined functions can be categorised as:-
*Function with no arguments and no return value
*Function with no arguments and return value
*Function with arguments but no return value
*Function with arguments and return value.
Function with no arguments and no return value
*Function Accepts argument but it does not return a value back to the calling program.
*It is Single (One-way) Type communication
* Generally Output is printed in the called function
*Here prime is called function and main is calling function.
Program: -
/*C program to check whether a number entered by user is prime or not using function with no arguments and no return value*/
#include< stdio.h >
void main()
{
prime();      //No argument is passed to prime()
return 0;
}
void prime()
{
/* There is no return value to calling function main(). Hence, return type of prime() is void */
int num,i,flag=0;
printf("Enter positive integer enter to check:\n");
scanf("%d",&num);
for(i=2;i<=num/2;++i)
{
if(num%i==0)
{
flag=1;
} }
if (flag==1)
printf("%d is not prime",num);
else
printf("%d is prime",num);
}
Function with no arguments and return value
The called Function does not recieve any data from the calling Function. It is also a one-way data communication between the calling Function and the called Function. So let us understand this with the help of Program:
Program: -
/*C program to check whether a number entered by user is prime or not using function with no arguments but having return 2value */
#include < stdio.h >
int main()
{
int num,i,flag; num=input();     /* No argument is passed to input() */
for(i=2,flag=i;i<=num/2;++i,flag=i)
{
if(num%i==0)
{
printf("%d is not prime",num); ++flag; break; }}
if(flag==i)
printf("%d is prime",num);
return 0;
}
int input()
{
/* Integer value is returned from input() to calling function */
int n;
printf("Enter positive enter to check:\n");
scanf("%d",&n);
return n;
}
Function with arguments and no return value
*Function accepts argument but it does not return a value back to the calling program.
* It is Single (One-way) Type Communcation
*Generally Output is printed in the called function
* Here check_display is called function and main is calling function.
Program: -
/*Program to check whether a number entered by user is prime or not using function with arguments and no return value */
#include < stdio.h >
void check_display(int n);
int main()
{
int num;
printf("Enter positive enter to check:\n");
scanf("%d",&num);
check_display(num);      /* Argument num is passed to function. */
return 0;
}
void check_display(int n)
{
/* There is no return value to calling function. Hence, return type of function is void. */
int i,flag;
for(i=2,flag=i;i<=n/2;++i,flag=i)
{
if(n%i==0)
{
printf("%d is not prime",n);
++flag;
break;
} }
if(flag==i)
printf("%d is prime",n);
}
Function with arguments and return value
/* Program to check whether a number entered by user is prime or not using function with argument and return value */
#include < stdio.h >
int check(int n);
int main()
{
int num,num_check=0;
printf("Enter positive enter to check:\n");
scanf("%d",&num); num_check=check(num);
/* Argument num is passed to check() function. */
if(num_check==1)
printf("%d in not prime",num);
else printf("%d is prime",num);
return 0;
}
int check(int n)
{
/* Integer value is returned from function check() */
int i;
for(i=2;i<=n/2;++i)
{
if(n%i==0)
return 1;
}
return 0;
}


Free Web Hosting