Introduction to Basics of Functions in C language

Functions are the major building blocks of a C program. Of course, every C program execution starts from main function. We have coded many programs even without a basic knowledge of a function. Let us define and understand the term function.
What is a function in C language?
A function is a group/block of statements that is used to perform a given task.
return_datatype function_name(list_of_arguments);  
A function is an identifier and every identifier in C language should be declared and/or defined.
There are three constraints for every function in C language – Function declaration, Function definition, and Function call.
Function declaration gives the structural details of a function i.e., name of the function, type of arguments it accepts (if any) and the datatype of return data from the function (if any). The following is the syntax of a function declaration. The function call initiates the function to perform the task given task.
Function definition includes the task for which the function was designed to perform. This is the core part of a function. The following is how a function definition looks.
return_datatype function_name(list_of_arguments)  
{  
 ...  
 ...  
 statements  
 ...  
 ...  
}  
Based upon the definition of functions, there are two types of functions – User-defined functions and library functions. The names of both these functions types define them.
User-defined functions are the functions, whose functionality is defined by the user/programmer. Whereas the library functions are defined by the developers.
The user-defined functions allow the programmer to define the functionality based upon the specifications of the application. The library functions help the user to reduce the repeated definition of certain most commonly used constructs. For example, when we have string, fetching the length of a string is one of the most commonly used string operations. Instead of defining it again in the program we code, the library function strlen can be used directly.
The added and advanced advantage of library functions is that the library functions provide access to manage the system resources in system-level programming.
Based upon the function arguments and return type, there are four types of functions.
  1. Functions with arguments and return type
  2. Functions with arguments and without return type
  3. Functions without arguments and with return type
  4. Functions without arguments and without return type
The following are the examples that demonstrate these four function types.
int getSum(int a,int b)//function with arguments and return type   
{  
 return a+b;  
}  
void getSum(int a,int b)//function with arguments and without return type  
{  
 printf("Sum is %d\n",a+b);  
}  
int getSum(void)//function without arguments and with return type  
{  
 int a=10,b=20;  
 return a+b;  
}  
void getSum(void)//function without arguments and return type  
{  
 int a=10,b=20;  
 printf("Sum is %d\n",a+b);  
}  
Out of all the user-defined and library functions, main is the special kind of function all in terms of declaration, definition and the number of arguments. However, in gcc, the main function always returns an integer, which returns the termination status of main function to the _start function, which in turn returns to the kernel. The main function returning int datatype is implicit i.e., even if the programmer does not do that explicitly, the compiler return an int value.
Always remember that main is a user-defined function because, the functionality of the function is defined by the programmer based upon the specifications. But, it needs no declaration because the declaration of main function is done in the library files those are included during execution of the program. In terms of number of arguments, main can have zero arguments, two arguments or three arguments as shown below.
int main(void) //int main() - no arguments  
int main(int argc,char* argv[]) //int main(int argc,char argv[][]) - two arguments  
int main(int argc, char* argv[],char* envp[]) //int main(int argc, char* argv[],char envp[][]) - three arguments  
In the above prototypes of main function, there are three arguments.
  • The argument argc gives the number of arguments supplied in command line. This includes the executable file that is provided for execution.
  • The argument char* argv[] or char argv[][] stores the arguments those are provided during the command line.
  • The argument char* envp[] or char envp[][] stores the environment variables for the executable file that is being executed, after compiling the program. The environment variables include the parameters like default header path, default path for libraries, default time for execution etc.
The concept of command line arguments is very early at this stage for discussion hence, they will be discussed in detail in further demonstrations.
However, a sample program for calculating factorial of a given number is shown below.
#include<stdio.h>  
int getFactorial(int);//function prototype or declaration  
main()  
{  
 int num;  
 printf("Enter number to find factorial:");  
 scanf("%d",&num);  
 printf("Factorial is:%d\n",getFactorial(num));//function call for get factorial  
}  
int getFactorial(int n)//function definition  
{  
 int i,fact=1;  
 for(i=1;i<=n;i++)  
 fact*=i;  
 return fact;  
}  
Here, the getFactorial function is designed as the function with arguments and return type.  In the function call getFactorial(num), num is called actual parameter and in the function definition int n is called formal parameter.
Another important and most useful concept of functions, especially for data structures, is recursion. This can also be discussed in coming sessions. To give a rough idea, about recursion, remember that recursive function is the function that calls itself.
The following are limits of parameter/argument passing about functions accodring to C99 standard.

  • 127 parameters in one function deļ¬nition
  • 127 arguments in one function call

Share:

0 comments:

Post a Comment