C Functions

C function

Functions is a group of statements and instruction. this group are instruction are executed when function are call. they are type of procedure or routine. In this post we are learn everything about c function.

Phase of functions

C function phase

[A] Function Declaration

Function declaration is a prototype of functions.

Syntax of function declaration

return_type function_name(Parameter_lists);  

Iteam Overview
return_type return type of a function are indicate which type are value are send by function. if function are return any value then should be defined its data type ( like int,float,pointer so on). otherwise provide void.
function_name name of function. is must be unique name to other function. and also should not an keyword.
Parameter_lists list of data type. if function are accept any parameters
Scope Entire the program

when declare a function, compiler are understand the behaviour of function. like number of parameter of function, return type of function an so on. Note that most of c compiler are not need to declare function. when defined a functions then compiler are understand its behaviours. but best practices before defined functions is must be declared. In functions declaration are not need to given name of parameter list. But data type are compulsory.

Example
void print_data(int,int);  

/*Return type: (void) void are indicating is not return any value by function.
Function name: print_data,
Parameter_lists : accepting two integer value*/

float salary(int);

/*Return value : a float value,
Function name : salary,
Parameter list: one integer value
*/

int * data();

/*Return type: return one integer pointer,
function name : data,
parameter list : none
*/

Explanation

Return type : Specified which type of value are returned by function. the following values are return by function.
a) values
b) pointer and
c) user defined data type

Function Name : function name is user defined. function name also called an identifiers.

Parameter Lists:Parameter list is collection of data types. there are indicating. which type of values are should be accept in function.

[B] Define a function

Define of function that means defined the actual code or group of statements. this is called body of the function. Note that there are identical of function declaration.

Define a function in c language as follows.

return_type  function_name (data_type variable_name...){
  //function body
}

Example
  void sum( int value1,int value2){
  //body of function
  }

[C] Calling of function

Function calling is important port of function. because they are start execution to function. if function are exist parameter then passing those parameter in same sequence by calling the function.

Example:

/*
  Example function declaration,definition and calling of function.
*/
/*Header file*/
#include <stdio.h>

/*Declaration of functions*/
void sum(int,int);
/*
  Sum function Definition 
  return type : void (not return any value)
  function name: sum
  Parameter list: num1,num2 (int data type)
  */
void sum(int num1,int num2){
  /*Print addition of num1+num2*/
  printf("\n Sum : %d",(num1+num2));
}
int main(){
  /*Sum Function call*/
  sum(1,2); //First time
  sum(5,5); //Second time
  sum(3,8); //Third time
  return 0;
}

Program execution : step 1: function Call sum(1,2)

Function Execution

Step 2: function call sum(5,5).

Function Execution

Step 3: function call(3,8).

Function Execution

Output:

 Sum : 3
 Sum : 10
 Sum : 11

Types of Functions in C

C Function are categories in two type
1) predefined
2) user defined

C function types

Predefined

Predefined function are include in c header files. use header file and access those function. For example < stdio.h > have two most important function like printf() and scanf(). before access those function append those header file in our program.

User Defined

function which are defined by programmer this is called user defined functions. user defined function are real logic to solve complex program but predefined function also providing important role.

Function have 4 properties
1) A function accept parameter and not return any value. example

void sub(int no1,int no2){
  //code
}

2) A function are not accept parameter and not return any value.

void display(void){
//code here
}
//or
void show(){
  //code
}

3) A function are accept parameter and return any value

int sum(int no1,int no2){
  result=no1+no2;
  return result;
}

4) A function are not accept any parameter but return the value

int sum(){
  int no1=1,no2=2;
  return no1+no2;
} 

Why use function in c

There are two main reason.

1) Reusability

Main reason function are provide reusability of codes. we are invoke multiple time in same function. this process are save lot of time and work.

2) Recursion usability

C function is provide a function calling facility. recursion is one of the example of this process. recursion is provide a best way to utilize of function. look at view this example.

/*
  calculate factorial using recursion.
*/

#include <stdio.h>
/*function declaration*/
int factorial(int);

/*Calculate factorial of given number*/
int factorial(int value){
  if(value==0){
    //base condition to terminate recursion
    return 1;
  }else{
    return factorial(value-1) *value; //recursive call itself
  }
}

int main(){
  //start execution of program
  
  int number=3;

  printf("Factorial of %d is %d\n",number,factorial(number)); //call factorial

  number=5;
  
  printf("Factorial of %d is %d\n",number,factorial(number)); //reuse factorial function
  return 0; 
}

code execution process

First time factorial(3) function call

C function types

Second time factorial(5).

C function types

Function with argument

Function accept arguments as parameter and this parameter is also called formal parameter. passing of argument in a function there are two possibility.

1) Call by value

When calling of function. given the parameter as values. this process are called call by value. note that there are not change actual parameter value. look at the difference between actual and formal parameters

actual formal parameters
 /*
  example of call by value
*/

#include <stdio.h>
/*function declaration*/
void exchange(int,int);


void exchange(int no1,int no2){
  int auxiliary=no1;
  /*swap values*/
  no1=no2;
  no2=auxiliary;
}

int main(){
  //start execution of program
  
  int no1=10,no2=20;
  printf("\nBefore function calling are value is\n");
  printf("no1 : %d  \nno2 : %d",no1,no2);

  exchange(no1,no2); //call function

  printf("\nAfter function calling are value is\n");
  printf("no1 : %d  \nno2 : %d",no1,no2);
  return 0; 
}
Example call by value

Output

Before function calling are value is
no1 : 10  
no2 : 20
After function calling are value is
no1 : 10  
no2 : 20

2) Call by Reference

/*
  example of call by reference
*/

#include <stdio.h>
/*function declaration*/
void exchange(int*,int *);


void exchange(int *no1,int *no2){
  int auxiliary=*no1;
  /*swap values*/
  *no1=*no2;
  *no2=auxiliary;
}

int main(){
  //start execution of program
  
  int no1=10,no2=20;
  printf("\nBefore function calling are value is\n");
  printf("no1 : %d  \nno2 : %d",no1,no2);

  //pass argument as reference
  exchange(&no1,&no2); // function calling

  printf("\nAfter function calling are value is\n");
  printf("no1 : %d  \nno2 : %d",no1,no2);
  return 0; 
}
Example call by reference

Output

Before function calling are value is
no1 : 10  
no2 : 20
After function calling are value is
no1 : 20  
no2 : 10

Scope of function

Scope of function are start when its call.

Example:

/*
  Scope of functions
*/
/*Header file*/
#include <stdio.h>

/*Declaration of functions*/
void add();
void subtract();
void multiple();
void divide();


/*Defination of functions*/
void add(){
  printf("\nStart execution of add function");
  int var1=5,var2=3;
  subtract();//function call
  printf("\nAdd: %d\n",(var1+var2));
  printf("\nEnd execution of add function");
}
void subtract(){
  printf("\nStart execution of subtract function");
  int sub1=5,sub2=3;
  multiple();//function call
    printf("\nSubtract: %d\n",(sub1-sub2));
  printf("\nEnd execution of subtract function");
}
void multiple(){
  printf("\nStart execution of multiple function");
  int mul1=5,mul2=3;
  divide();//function call
    printf("\nmultiple: %d\n",(mul1*mul2));
  printf("\nEnd execution of multiple function");
}
void divide(){
  printf("\nStart execution of divide function");
  int div1=15,div2=3;
  printf("\nDivide: %d\n",(div1/div2));
  printf("\nEnd execution of divide function");
}

int main(){
  printf("\nStart Main function");
  //Entry point of program
  add(); //function call

  printf("\nEnd of main function\n");
  return 0;
}

Output

Start Main function
Start execution of add function
Start execution of subtract function
Start execution of multiple function
Start execution of divide function
Divide: 5

End execution of divide function
multiple: 15

End execution of multiple function
Subtract: 2

End execution of subtract function
Add: 8

End execution of add function
End of main function

Execution process:
step1: Program execution will start in main function.

Main Function Scope

Step 2: inside of main function have two functions printf() and add(). but printf is predefined so it will execute. after this executing another function add().

Function Scope

step 3: add() function are calling subtract().

Function Scope

step 4: subtract() function are calling multiple().

Function Scope

step 5: multiple() function are calling divide().

Function Scope

bottom up function return back.

Function scope of this program:
1) main(): Entire the program.
2) add(): Execution of both Multiple and Divide function then end of execution of Add().
3) muliple(): end after execute divide().
4) divide(): execute inside statement and back to calling function.

Conclusion : Scope of functions is start when its call. by default main function is calling first. Execution and scope of function are start when its call. and execute all statements and instruction. Then end of this function. User defined function are call in stack area.

Variable Length Parameter Lists

variable length that means we are not defined how many number of parameter are it will accepted. and also not defined there parameter list (data type and variable name). look at view this example.

/* 
  Example of Variable Length Argument in C
*/
/*Header file*/
#include <stdio.h>
#include <stdarg.h>
/*Function declaration*/
int func(int , ...);

/*Function defination of func*/
int func(int arg, ...){
  printf("\nVariable Argument : [%d] \n",arg);
  /*Hold info in variable arguments*/
  va_list list;

  va_start(list,arg);

  for (int i = 0; i < arg; i++){
    /*Print parameter list*/
    printf(" %d \n",va_arg(list, int)); 
  }  
  va_end(list); 

}
int main(){
  /*Function call*/ 
  func(3,4,5,33);
  func(2,90,100);
  return 0;

}

Output:

Variable Argument : [3] 
 4 
 5 
 33 

Variable Argument : [2] 
 90 
 100

Advantage of function

1) allowing to call same function many time. this process are provide reusability.
2) Code are easier to read and modified.
3) Dividing a complex problem into sub task using function.

© 2019, regularcodes.com, All rights reserved