Function is basically set of statement that takes inputs, perform some computations and produces output. The function contains the set of programming statements enclosed by {} is knows as C programming Language.

Syntax

  • return_type function_name(data_type parameter…)
  • {  
  • //code to be execute 
  • }  

Types of Functions

There are two type of function in C programming Language.

  • Library Functions
  • User-defined functions
Function Types
Function Types

Library Functions in C Language

Library Functions are the functions which are declare in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() , etc is knows as Library function.

User-defined functions in C language

User-defined functions are the functions which are creates by the C programmer, so that he can use it many times. It reduces the complexity of a big program and optimizes the code is knows as User-defined functions.

Aspects of function calling in different a ways

  • function without arguments and with return value.
  • function with arguments and without return value.
  • function without arguments and without return value.
  • function with arguments and with return value.

function without arguments and with return value

Example

Input value

#include<stdio.h>  
int sum();  
void main()  
{  
    int result;   
    printf("\nthe sum of three numbers:");  
    result = sum();  
    printf("%d",result);  
}  
int sum()  
{  
    int x,y,z;   
    printf("\nEnter three numbers");  
    scanf("%d %d %d",&x,&y,&z);  
    return x+y+z;   
}  

Output

Output value

Function with argument and without return value

Example

Input value

#include<stdio.h>  
void sum(int, int, int);  
void main()  
{  
    int x,y,z,result;   
    printf("\nthe sum of three numbers:");  
    printf("\nEnter three numbers:");  
    scanf("%d %d %d" ,&x,&y, &z);  
    sum(x,y,z);  
}  
void sum(int x, int y, int z)  
{  
    printf("\nThe sum is %d",x+y+z);      
}  

Output value

Output value

Function without argument and without return value

Example

Input

#include<stdio.h>  
void printName();  
void main ()  
{  
    printf("Good Student ");  
    printName();  
}  
void printName()  
{  
    printf("How are you ?");  
}  

Output

Output value

Function with argument and with return value

Example

Input value

#include<stdio.h>  
int sum(int, int, int);  
void main()  
{  
    int x,y,z,result;   
    printf("\nThe sum of two numbers:");  
    printf("\nEnter two numbers:");  
    scanf("%d %d %d",&x,&y,&z);  
    result = sum(x,y,z);  
    printf("\nThe sum is : %d",result);  
}  
int sum(int x, int y, int z)  
{  
    return x+y+z;  
}  

Output value

Output value

C Library Functions

Library functions are inbuilt function in C that are groups and places at a common place is knows the library.

There are some Header files Functions

Header files Description
stdio.hThis is a standard input/output header file in which i/o function are declare.
conio.hThis is a console i/o header file.
string.hAll string relate library functions like gets(), puts(),etc.
stdlib.hThis file contains all the common library functions like malloc(), calloc(), exit(), etc
math.hThis file contains all the math operations relate functions.
time.hAll the time-relate functions.
ctype.hAll character handling relate functions
locale.hThis file contains locale functions.

Function Arguments

There are two methods to pass the data into the function in C language.

  • Call by value.
  • Call by reference.

Call by Value

In call by value means, the value of the actual parameters is copied into the formal parameters is knows as Call by Value. we can not modify the value of the actual parameter by the formal parameter.

Normal Example in call by value

Input value

#include<stdio.h>  
void change(int call) {    
    printf("Before adding value inside function call=%d \n",call);    
    call=call+82;    
    printf("After adding value inside function call=%d \n", call);    
}    
int main() {    
    int x=82;    
    printf("Before function call x=%d \n", x);    
    change(x);   
    printf("After function call x=%d \n", x);    
return 0;  
}    

Output value

Output value

Swap () Example of call by value

Input value

#include <stdio.h>
void swap(int x, int y);
 
int main () {

   int x = 542;
   int y = 854;
 
   printf("Before swap, value of a : %d\n", x );
   printf("Before swap, value of b : %d\n", y );

   swap(x, y);
 
   printf("After swap, value of x : %d\n", x );
   printf("After swap, value of y : %d\n", y );
 
   return 0;
}
void swap(int p, int q) {

   int temp;

   temp = p; 
   p = q;    
   q = temp;
  
   return;
}

Output value

Output value

Call by reference

The address of the variable is passed into the function call as the actual parameter is knows as Call by reference. The actual parameters can be modified by changing the formal parameters.

Normal Example of call by reference

Input value

#include<stdio.h>  
void change(int *call) {    
    printf("Before adding value inside function call=%d \n",*call);    
    (*call) += 65;    
    printf("After adding value inside function call=%d \n", *call);    
}      
int main() {    
    int by=65;    
    printf("Before function call by=%d \n", by);    
    change(&by);    
    printf("After function call by=%d \n", by);    
return 0;  
}   

Output value

Output value

Swap() Example of call by reference

Input value

#include <stdio.h>

int main () {

   int p = 45;
   int q = 96;
 
   printf("Before swap, value of p : %d\n", p );
   printf("Before swap, value of q : %d\n", q );

   swap(&p, &q);
 
   printf("After swap, value of p : %d\n", p );
   printf("After swap, value of q : %d\n", q );
 
   return 0;
}
void swap(int *s, int *t) {

   int temp;

   temp = *s; 
   *s = *t;    
   *t = temp;
  
   return;
}

Output value

Output Value

Recursion in C

Recursion is the process of repeating items in a self-similar way is knows as Recusion in C.

Syntax

void recurse()

{

……

recurse(); /* calling of the function by itself */

……

}

int main()

{

……

recurse();

……

}

1.Example

Input value

#include <stdio.h>

unsigned long long int factorial(unsigned int rom) {

   if(rom <= 1) {
      return 1;
   }
   return rom * factorial(rom - 1);
}

int  main() {
   int rom = 9;
   printf("Factorial of %d is %d\n", rom, factorial(rom));
   return 0;
}

Output value

Output value

2.Example

Input value

include

int fibonacci(int a) {

if(a == 0) {
return 0;
}

if(a == 1) {
return 1;
}
return fibonacci(a-1) + fibonacci(a-3);
}

int main() {

int a;

for (a = 0; a < 27; a++) {
printf(“%d\t\n”, fibonacci(a));
}

return 0;
}

Output Value

Output value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about C Programming language please Wikipedia click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *