Wednesday

Funtions


Funtions

What is functions?

C Programming Functions
Function in programming is a segment that groups a number of program statements to perform specific task.
A C program has at least one function main( ). Without main() function, there is technically no C program.
Types of C functions

Basically, there are two types of functions in C on basis of whether it is defined by user or not.

·    Library function
·    User defined function

Library function

Library functions are the in-built function in C programming system. For example:
main()
- The execution of every C program starts from this main() function.
printf()
- prinf() is used for displaying output in C.
scanf()
- scanf() is used for taking input in C.
Visit this page to learn more about library funin C programming language.

User defined function

C provides programmer to define their own function according to their requirement known as user defined functions. Suppose, a programmer wants to find factorial of a number and check whether it is prime or not in same program. Then, he/she can create two separate user-defined functions in that program: one for finding factorial and other for checking whether it is prime or not.

How user-defined function works in C Programming?

#include <stdio.h>
void function_name(){
................
................
}
int main(){
...........
...........
function_name();
...........
...........
}
As mentioned earlier, every C program begins from main() and program starts executing the codes inside main() function. When the control of program reaches to function_name() inside main() function. The control of program jumps to void function_name() and executes the codes inside it. When, all the codes inside that user-defined function are executed, control of the program jumps to the statement just after function_name() from where it is called. Analyze the figure below for understanding the concept of function in C programming. Visit this page to learn in detail about user-defined functions.
 

Remember, the function name is an identifier and should be unique.

Advantages of user defined functions

1.    User defined functions helps to decompose the large program into small segments which makes programmar easy to understand, maintain and debug.
2.    If repeated code occurs in a program. Function can be used to include those codes and execute when needed by calling that function.
3.    Programmar working on large project can divide the workload by making different functions.
4.    This chapter is the continuation to the function Introduction chapter.
5.    Example of user-defined function
6.    Write a C program to add two integers. Make a function add to add integers and display sum in main() function.
7.    /*Program to demonstrate the working of user defined function*/
8.    #include <stdio.h>
9.    int add(int a, int b);           //function prototype(declaration)
10.    int main(){
11.         int num1,num2,sum;
12.         printf("Enters two number to add\n");
13.         scanf("%d %d",&num1,&num2);
14.         sum=add(num1,num2);         //function call
15.         printf("sum=%d",sum);
16.         return 0;
17.    }
18.    int add(int a,int b)            //function declarator
19.    {            
20.    /* Start of function definition. */
21.         int add;
22.         add=a+b;
23.         return add;                  //return statement of function
24.    /* End of function definition. */  
25.    }                                 

Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter
YOUR ADSENSE CODE GOES HERE

0 comments:

Post a Comment

Dont Forget for Commets

 

| C programing tutorials © 2009. All Rights Reserved | Template Style by My Blogger Tricks .com | Design by Brian Gardner | Back To Top |