Home » C and C++, Tech Corner

Functions

4 August 2009 No Comments Posted By:Dileep
  • The process of splitting the lengthy and comlex programs into a number of smaller units(called modules or sub-programs) is called modularization.
  • Programming in such an approach is called modular programming.
  • Modularization offers several advantages – Reusability, Debugging is easier, Build libraray.
  • Functions – A function is a set of instructions to carryout a particular task.
  • The function, after it’s executions, returns a single value.
  • Classified into standard functions and user-defined functions.
  • Function Definition – Defining a function means writing an actual code for the function which performs a specific and identifiable task.
  • Two ways – Non-ANSI style and ANSI style.
  • Non-ANSI function definition -

type name_of_function(parameter_list)
parameter definition;
{
variable declaration; /*within the function*/
statement1;
statement2;
.
.
.
return(value_computed);
}

  • ANSI style -

type name_of_the_function(parameter definition)
{
variable declaration; /*within the function*/
statement1;
statement2;
.
.
.
return(value_computed);
}

  • return‘ – It is a means of communication from the called function to the calling function.
  • There may be one or more return statements.
  • When a return statement is encountered, the control is transferred to the calling function.
  • The point where the name of the function appears in any function is called function reference. This is also called as a function call.
  • When a function is called in any function or a program the execution of the calling function temporarily suspends and the control transfers to the called function. After the required computations are made in the function, the control returns to the calling function along with the computed value.
  • Arguements and Parameters – are the variables used in a program and a function respectively.
  • Variables used in the function call are called arguements.
  • These are written within the parantheses followed by the name of the function.
  • They are also called actual parameters, as they are accepted in the main program(or a calling function).
  • Variables used in the function definition are called parameters.
  • They are also referred to as formal parameters, because they are not the accepted values.
  • They receive the values from the calling function. The parameters must be written within the parantheses followed by the name of the function in the function definition.
  • The number of arguements should be equal to the number of parameters.
  • There must be 1 to 1 mapping between arguements and parameters, i.e they should be in the same order and should have the same data type.
  • Same variables can be used as arguements and parameters.

Category of functions -

  • arguements with return value
  • no arguements, no return value
  • arguements but no return value
  • no arguements but return value
  • An array name in C represents the address of the first element in that array. So, arrays are passed to functions as pointers.
  • We can pass either an entire array as an arguement or it’s individual elements.
  • Both the calling function and the called function are using their own variables. The existence of these variables is restricted to the calling function or to the called functions.This is known as scope of the variables.
  • Based on this scope, the variables can be classified into – local variables and global variables.
  • Variables whose existence is known to only to the main program or functions are called local variables.
  • Variables whose existence is known to both the main() function as well as other functions are called global variables.
  • Local variables are declared within the main program or a function. But the global variables are declared outside the main() and the other functions.
  • Storage classes are used to define the scope(visibility) and the life-time of the variables and/or functions.
  • Four storage classes in C – auto, static, extern, register.
  • static Variables – These variables hold their values throughout the execution of a program.
  • Two types – Internal static variables, External static variables.
  • Internal static variables are declared inside a function.
  • They are identical to automatic variables in the function scope except that they retain their values throughout the program.
  • External static variables are declared outside of all functions including the mian() function.
  • They are global variables but are declared with the keyword static.
  • The external static variables cannot be used across multi-file program.
  • Static variables are initialised only once during the compile-time.
  • The initial values to be assigned must be always constants but not expressions.
  • If nothing is initialized to a static variable, by default, it is assigned a zero.
  • register Variables – Variables whose values are stored in the CPU registers.
  • Since the values are stored in the CPU registers, they are accessed quickly and hence programs are executed faster.
  • Like automatic variables, the register variables are also local variables.
  • Declaring with the keyword register, it does not guarantee that they are actually register variables. It is valid only when the requested CPU register is available.
  • Function declaration – means specifying the function as a variable depending on the return value.
  • Functions may be written before or after the main(). If the functions are written before the main() then function declaration is not required. But, the functions which are written after the main() function would require function declaration.
  • Two styles – Non-ANSI style and ANSI style.
  • Non-ANSI style – type_of_return_value name_of_function();
  • ANSI style – type_of_return_value name_of_function(dt1 p1,dt2 p2,…,dtn pn);
  • The process of transmitting the values from one function to other is known as parameter passing.
  • Two methods – call by value, call by reference.
  • Call by value – When the values of the arguements are passed from a calling function to a called function, the values are copied into the called function.
  • If any changes are made to the values in the called function, there will not be any change in the original values within the calling function.
  • Call by reference – In this method, the actual values are not passed, instead their addresses are passed to a calling function. Here, no values are copied as the memory locations themselves are referenced.
  • If any modification is made to the values in the called function, then the original values will get changed within the calling function.
  • Recursion – is a technique that defines a function in terms of itself.
  • A function which calls itself.
  • Factorial calcualtion – coomon use of recursion.

<<Previous Post

Next Post>>

Related posts:

  1. Pointers
  2. Constants and Variables
  3. Managing Input and Output Operations
  4. Data Files
  5. Operators & Expressions

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>