Thursday, January 20, 2011

14. Introduction to Functions


Define function

Function is a block of source code which does one or some tasks with specified purpose.

Benefit of using function

  • C programming language supports function to provide modularity to the software.
  • One of major advantage of function is it can be executed as many time as necessary from different points in your program so it helps you avoid duplication of work.
  • By using function, you can divide complex tasks into smaller manageable tasks and test them independently before using them together.
  • In software development, function allows sharing responsibility between team members in software development team. The whole team can define a set of standard function interface and implement it effectively.

Structure of function

Function header

The general form of function header is:
1return_type function_name(parameter_list)
Function header consists of three parts:
  • Data type of return value of function; it can be any legal data type such as int, char, pointer… if the function does not return a value, the return type has to be specified by keyword void.
  • Function name; function name is a sequence of letters and digits, the first of which is a letter, and where an underscore counts as a letter. The name of a function should meaningful and express what it does, for example bubble_sort,
  • And a parameter list; parameter passed to function have to be separated by a semicolon, and each parameter has to indicate it data type and parameter name. Function does not require formal parameter so if you don’t pass any parameter to function you can use keyword void.
Here are some examples of function header:
1/* sort the array of integer a with the
2specified size and return nothing */
3void sort(int a[], int size);
4
5/* authenticate user by username and password and return true
6if user is authenticated */
7bool authenticate(char*username,char*password)

Function body

Function body is the place you write your own source code. All variables declare in function body and parameters in parameter list are local to function or in other word have function scope.

Return statement

Return statement returns a value to where it was called. A copy of return value being return is made automatically. A function can have multiple return statements. If the void keyword used, the function don’t need a return statement or using return; the syntax of return statement is return expression;

Using function

How to use the function

Before using a function we should declare the function so the compiler has information of function to check and use it correctly. The function implementation has to match the function declaration for all part return data type, function name and parameter list. When you pass the parameter to function, they have to match data type, the order of parameter.

Source code example

Here is an example of using function.
01#include<stdio.h>
02  
03/* functions declaration */
04  
05void swap(int *x, int *y);
06  
07  
08  
09void main()
10{
11    int x = 10;
12    int y = 20;
13  
14    printf("x,y before swapping\n");
15    printf("x = %d\n",x);
16    printf("y = %d\n",y);
17     
18    // using function swap
19    swap(&x,&y);
20  
21    printf("x,y after swapping\n");
22    printf("x = %d\n",x);
23    printf("y = %d\n",y);
24  
25}
26  
27  
28/* functions implementation */
29  
30void swap(int *x, int *y){
31    int temp = *x;
32        *x = *y;
33        *y = temp;
34}
Here is the output
x,y before swapping
x = 10
y = 20
ping
x = 20
y = 10
x,y after swa
p
ess any key to continue
P
r

Dont Miss Another Post Connect With Us !

Enter your email address:

0 comments: