Function In 'C'

  • Till now we have seen many library  function that perform some operations or some calculation.
  • But 'C' language also allows programmer to define their own function to solve certain task.
  • Programmer define functions are self contained block which perform certain task.
  • Advantage of programmer define function is that large program can break into small and clean self-contained blocks which performs certain task.
  • Using programmer define function in a larger program keeps program clean and more readable ,and  also it reduce the complexity of the program.

Concept of Functions

  • As functions are programmer define function ,which is a self-contained block which perform some well defined tasks.
  • Functions  in 'C' language are define outside the main() and they are used in main().
  • For defining function, you have to first declare it on the top.
  • As the function has completed it's task the control goes at that point where it was called or Used.
  • Generally the function is defined such a manner that it may or may not take some arguments and return a single value or not.
  • Taking argument and returning value ,totally  depends upon the programmer or according to the need whether the certain  program needs an argument or not and whether it  will return a value or not.
  • For example: printf() this a function which take an argument and returns nothing.
    getchar() this function does not take any argument, but it returns a value which is a character. 
  • Function can return only single Value.

Types of functions

  • As you know functions can take arguments and may return a value ,so according this there are four types of functions.
  • Functions which takes nothing and returns Something:

  • This type of function takes nothing as argument but returns Something.
    getchar() this function does not take any argument,but it returns a value which is a character.

  • Functions which takes something and returns Nothing:

    This type of function takes something as argument but returns Nothing.
    For example: printf() this a function which take an argument and returns nothing.

  • Functions which takes something and returns Something:

    This type of function takes something as argument and returns Something.
    For example: power(a,n) this a function which take an argument and also returns some value.

  • Functions which takes Nothing and returns Nothing:

    This type of function does not takes an argument and also returns Nothing.

Declaration of Function

  • Declaration of function is also called as function prototype
  • Before defining a function ,you have to declare it on the top
  • As you know function can take an argument an may or may not return a value, this think we have to specify at the declaration of function.
  • let's see ,how we can declare a function.
Syntax:

return_datatype   function_name(datatype arg1_name, datatype agr2_name,....,);

//return_datatype :it means which type of data function will return.

//arg :no of arguments  depends upon the programmer, 
//that how much data he wants to complete the task. 

Note: Arguments are nothing just an mode of communication between the function and the calling function.
Argument are enclosed in parenthesis('()') contain variable names seperated by commas .
Lets declare a function whose name is 'Add' which will take two integer values as an argument and it will return an integer value

Solution:
int Add(int a,int b);
//this decleration means,
//function will take 2 arguments,
//and return an integer value.



Q:declare a function whose name is 'temp' which will take One float type value as an argument and it will return an float type value

Solution:
float temp(int a);
//this declaration means,
//function will take 1 argument,
//and return an float value.



Q:declare a function whose name is 'give_ascii' which will take An character as an argument and it will return an integer value

Solution:
int give_ascii(char a);
//this declaration means,
//function will take 1 argument,
//and return an integer value.




Defining a Function

  • Defining a function is the process, where  we write a actual code which will perform
    certain task
  • As you have a declare function at the top, the same thing you have to write while defining the function.
  • First line contain the function prototype.
  • There is no semicolon at the end of prototype, while definition of function.
  • s
  • Remember function can return only single value.
  • let's see ,how we can define a function.
  • We will write a function for adding two numbers and returning the result.

    int add(int a ,int b)
    {//defination of function.
    
        int result;
    
        result = a + b;
        return result;  //returning value of result.
    }
    

  • Defining the Function for checking number is even or odd.

    int check(int a)
    {
        int result;
        if(a % 2==0)
           result = 1;
        else
          result = 0;
    //if result is 1 then number is even.
    //if result is 0 then number is odd.
      return result;   //returning the value of result.
    }
    






Further Concepts:



Further Topics:




People Also Searched: