Search Your Topic

config
Monday, 12 June 2017 11:43

functions in C

Written by
Rate this item
(0 votes)

The function in C language is also known as procedure or subroutine in other programming languages.

To perform any task, we can create function. A function can be called many times. It provides modularity and code reusability.


Advantage of functions in C

There are many advantages of functions.

1) Code Reusability

By creating functions in C, you can call it many times. So we don't need to write the same code again and again.

2) Code optimization

It makes the code optimized, we don't need to write much code.

Suppose, you have to check 3 numbers (781, 883 and 531) whether it is prime number or not. Without using function, you need to write the prime number logic 3 times. So, there is repetition of code.

But if you use functions, you need to write the logic only once and you can reuse it several times.

Types of Functions

There are two types of functions in C programming:

  1. Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
  2. User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. It reduces complexity of a big program and optimizes the code.

C Function


Declaration of a function

The syntax of creating function in c language is given below:

 
  1. return_type function_name(data_type parameter...){  
  2. //code to be executed  
  3. }  

Return Value

A C function may or may not return a value from the function. If you don't have to return any value from the function, use void for the return type.

Let's see a simple example of C function that doesn't return any value from the function.

Example without return value:

 
  1. void hello(){  
  2. printf("hello c");  
  3. }  

If you want to return any value from the function, you need to use any data type such as int, long, char etc. The return type depends on the value to be returned from the function.

Let's see a simple example of C function that returns int value from the function.

Example with return value:

 
  1. int get(){  
  2. return 10;  
  3. }  

In the above example, we have to return 10 as a value, so the return type is int. If you want to return floating-point value (e.g. 10.2, 3.1, 54.5 etc), you need to use float as the return type of the method.

 
  1. float get(){  
  2. return 10.2;  
  3. }  

Now, you need to call the function, to get the value of the function.


Parameters in C Function

A c function may have 0 or more parameters. You can have any type of parameter in C program such as int, float, char etc. The parameters are also known as formal arguments.

Example of a function that has 0 parameter:

 
  1. void hello(){  
  2. printf("hello c");  
  3. }  

Example of a function that has 1 parameter:

 
  1. int cube(int n){  
  2. return n*n*n;  
  3. }  

Example of a function that has 2 parameters:

 
  1. int add(int a, int b){  
  2. return a+b;  
  3. }  

Calling a function in C

If a function returns any value, you need to call function to get the value returned from the function. The syntax of calling a function in c programming is given below:

 
  1. variable=function_name(arguments...);  

1) variable: The variable is not mandatory. If function return type is void, you must not provide the variable because void functions doesn't return any value.

2) function_name: The function_name is name of the function to be called.

3) arguments: You need to provide arguments while calling the C function. It is also known as actual arguments.

Example to call a function:

 
  1. hello();//calls function that doesn't return a value  
  2. int value=get();//calls function that returns value  
  3. int value2=add(10,20);//calls parameterized function by passing 2 values  

Example of C function with no return statement

Let's see the simple program of C function that doesn't return any value from the function.

 
  1. #include <stdio.h>      
  2. #include <conio.h>    
  3. //defining function    
  4. void hello(){  
  5. printf("hello c programming");  
  6. }  
  7. void main(){      
  8. clrscr();      
  9.   
  10. hello();//calling a function  
  11. hello();  
  12. hello();  
  13.   
  14. getch();      
  15. }      

Output

hello c programming
hello c programming
hello c programming

Example of C function with return statement

Let's see the simple program of function in c language.

 
  1. #include <stdio.h>      
  2. #include <conio.h>    
  3. //defining function    
  4. int cube(int n){  
  5. return n*n*n;  
  6. }  
  7. void main(){      
  8. int result1=0,result2=0;    
  9. clrscr();      
  10.   
  11. result1=cube(2);//calling function  
  12. result2=cube(3);    
  13.       
  14. printf("%d \n",result1);  
  15. printf("%d \n",result2);  
  16.   
  17. getch();      
  18. }      

Output

8
27
Read 275 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.