Monday, 20 July 2015

Concept of function in C - Programmer section!

A functions can be defined as :
It is the set of information that can be grouped into smaller sub-program called a function.
OR
It is set of instructions that performs a particular task and we need several times in our program so it can be grouped into a smaller subprogram called a function.

Function is very important concept from hacker's perspective as well as programmers. Before the processor jumps on to the function call, the address of next instruction to the call instruction is saved on the stack as return address, which will be loaded in EIP register at when  the called function finishes its job. Remember the ret instruction will make the processor to land on an address saved in place of saved return address. In buffer overflow attacks this situation is exploited to control the execution  of the processor by overwriting the saved return address.

First let's start  understand the concept of function from programmers perspective.

Here are some examples of functions:
int sum(int a, int b)
 {
    int answer = 0;   
    answer = a + b;  
    return answer
 }
The first line is called Function header. In this line, sum is our function and return value type is int.
In the second line, the body of function starts with { and ends with  }  in the sixth line .
In the third line, answer is variable, that holds the result which will be returned by the function.
The forth line calculates the sum.
And finally the fifth line will return the answer.

The function declaration  to be used in program is as follows:
return_type function_name (parameter_declarations)
{
    declarations
    statements
}

where return_type and parameter_declarations are optional.

Return Type: A function may return a value. The return_type is the data type of the value the function returns. If the return_type have returned no value, use the keyword void.

parameter_declarations :When a function is invoked, you pass a value to the parameter. This value is referred to actual parameter or argument.

Function Prototype : It is a declaration of the function that tells the program about the type of the value returned by function and type of each argument.

General form of Function Prototype :
 return_type function_name(type variable1, type variable2, ...., type variableN);

A prototype can be recognized by the ; right after the arguments. It tells the compiler that there is a function called argument defined somewhere else in our code.In larger compilations, the function prototypes are put in header files. Header files have an .h extension.

Difference between function prototype and function definition:
The difference between function prototype and function definition is that function prototype has no body and terminating by semicolon.
Function prototype :
int sum(int n1, int n2);

Function definition:
int sum(int a, int b)
{
    int s;
    sum = a+b;
    return sum;
}

main() Function:
int main();
Main is a function like any other.The main() function is the starting point for all  of the codes in your program. When the window runs program in C, it passes control of the PC over to that program. Operating system needs to know where inside your program the control needs to be passed. In the case of a C language program, it's the main() function that the operating system is looking for.So It is the function called by the Operating System when your code is executed.

                                             




Basically The function main() can be defined with any of the following forms:
int main (void)
int main ( )
int main(int argc, char *argv[])
int main (int argc, char ** argv)

But All C programs contain a main() structure that follows this format:
return_value_type main(optional argument)
{
    body_of_main;
}
where both the return value type and arguments are optional. If you use command-line arguments for main(), use the format:
return_value_type main(int argc, char * argv[])
where the argc integer holds the number of arguments and the argv array holds the input arguments (strings).

Called and Calling function :
int main()
{
    int x =3, y=4,z;
    z = sum(x,y);
    printf("The sum is : %d", z);
}
int sum(int a, int b)
{
    int c;
    c = a+b;
    return c;
}
Here main is called function and sum is calling function .


Returning from a Function:
The return statement is the mechanism for returning a value from the function. Generally, a return statement is used to terminate a function weather or not it returns a value.

The return statement :
The return statement is useful in two ways. First, an immediate exit from the function is caused as soon as a return statement is encountered. second use of return statement is that it is used to return a value to the calling function.

return expression ;
OR
return value;
The expression will converted to the return type of function if necessary.Parentheses are often used around the expression, but they are optional.
The calling function is free to ignore the returned value. Furthermore, if there is no expression after expression, no value is returned to the caller function.In any case, if a function fails to return a value, its 'value' is certain to be garbage. All function except those of type void, return a value.If in a function, a return statement is missing, then the return value of the function technically undefined. Only the function declared as void do not return a value and hence can not be used in expression.

Function Arguments:
Two most important ways of passing arguments to a functions are : call by value and call by reference.
Call  by value:The call by value method copies the values of actual parameters into formal parameters, that is, the function creates its own copy of arguments values and then use them.
Call by reference : When a function is called by reference, the formal parameter become reference to the actual parameter in the calling function. This means that in call by reference, the called function does not create its own copy of original values, rather, it refers to the original vales.

Important Note: In C,There is no call by reference in C. You use pointers to emulate that, but pointers are passed by value.
For more detail on call by reference and call by value, Google.

I think for programmer, This is enough. But for hackers, this does not end. Hackers are still proceed. In the next tutorial i will consider how function calls are represented in memory and show the stack organization when functions are called.This is important concept to a hacker. By understanding how function calls are represented in memory, You will be able to understand buffer-overflow concept and how to exploit it.

If you like this post or have any question, please feel free to comment !

No comments:

Post a Comment

Blogger Widget