Saturday, 31 January 2015

C Programming Functions

C Programming

C Programming Functions

In programming, a function is a segment that groups code to perform a specific task.
A C program has at least one function main( ). Without main() function, there is technically no C program.

Types of C functions

There are two types of functions in C programming:
  • 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 functions in C programming language.

User defined function

C allows programmer to define their own function according to their requirement. These types of functions are 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.
Working of functions in C programming
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 programmer 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. Programmer working on large project can divide the workload by making different functions.

No comments:

Post a Comment