C - Functions


FUNCTIONS

A group of statements that together performs a task is known as ‘Function’. Every C program has at least one function, which is main(), and additional functions can be defined by all the most trivial programs.
We can divide our code in separate functions and how we divide our code is up to us, but logically division is such that each function performs a specific task.
Declaration of a function tells the compiler about the function name, parameters and return type. A function definition provides the actual body of the function.
Numerous built in functions that our program can call are present in the C standard library. For ex:-
strcat() to concatenate two strings, memcpy()  to copy one memory location to another location, and many more funtions.

A function can also be referred as a sub-routine or a method or a procedure, etc.

Defining a Function

In C programming language, the general form of a function definition is as follows:-
return_type function_name( parameter list ) {
   body of the function
}
In C programming, a function definition consists of a function header and a function body. Here are all the parts of a function −
·         Return Type – A value may be returned by a function. Return Type is a data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return type is the keyword void.
·         Function name – It is the actual name of the function. The parameters list and the function name together constitute the function signature.
·         Parameters – It is like a placeholder. We pass a value to the parameter when a function is invoked. This value is referred as actual parameter or argument. The parameters list refers to the order, type and number of parameters of a function. A function may contain no parameters that is, parameters are optional.
·         Function Body – It contains a collection of statements that define what the function does.

Example

The source code for the function called max() is given below. This function takes two parameters num1 and num2 and it returns the maximum value between these two -
/* function returning the max between two numbers */
int max(int num1, int num2) {
 
   /* local variable declaration */
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}

 

Function Declarations

A declaration of a function tells the compiler about a function name and how to call the function. The actual function body can be defined separately.
A function declaration has the following parts −
return_type function_name( parameter list );
The function declaration is as follows for the above defined function max():-
int max(int num1, int num2);
In function declaration parameter name are not required only their type is required, so given below is also a valid declaration:-
int max(int, int);
When we define a function in one source file, function declaration is required and we call that function in another file. In such case we should declare the function at the top of the file calling the function.

Calling a Function

We give a definition of what the function has to do while create a C function. To use a function, we will have to call that function to perform the defined task.
The program control is transferred to the called function, after a program calls a function. A called function, after performing the defined task and after the execution of the return statement and reaching of its function-ending closing brace, it returns the program control to the main program.
We simply need to pass the required parameters along with the function name, to call a function, and if the function returns the value, then we can store the returned value.
For ex:-
#include <stdio.h>
 
/* function declaration */
int max(int num1, int num2);
 
int main () {
 
   /* local variable definition */
   int a = 100;
   int b = 200;
   int ret;
 
   /* calling a function to get max value */
   ret = max(a, b);
 
   printf( "Max value is : %d\n", ret );
 
   return 0;
}
 
/* function returning the max between two numbers */
int max(int num1, int num2) {
 
   /* local variable declaration */
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}
We have kept max() along with main() and compiled the source code. While running the final executable, it would produce the following result −
Max value is : 200

Function Arguments
If a function is to use arguments, it must declare variables that accepts the value of the arguments. These variables are called as the formal parameters of the function.
The formal parameters behave like other local variables inside the function and they are created upon entry into the function and destroyed upon exit.
While giving a call to a function, there are two ways in which arguments can be passed to a function −
Sr. No.
Call Type & Description
1
The actual value of argument is copied into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
2
The address of the argument is copied into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that the changes made to the parameter affect the argument.
C uses call by value to pass arguments (by default). In general, this means the code within the function cannot alter the arguments used to call the function.




No comments:

Post a Comment