Functions in 'C'

Functions in 'C' : 

  • Function can be defined as a piece or block of code used to define particular task.
  • Function has a name and return type. Return type specifies type of a value that function returns.
  • Function can be either standard library function or user defined function.
  • Standard library functions are predefined by language itself we just use them in our program by calling them like printf, scanf, sqrt and so on.
  • Function defined by users or developers in their program to perform particular task is called user defined function.
  • User defined function has two parts : function definition and function calling.
1) Defining function :
  • Syntax :
     return_type function_name([arg1,arg2,...])
{
   //body of function 
   ________
 }

  • Here return type tells data type of value that function returns. 
  • Name can be any identifier but it must specify or tell information about what the function does. 
  • Function can have zero or any number of arguments(parameters) passed to it after function name inside the parenthesis.
  • Body of a function should always enclosed between {}.


2) Function Calling :
  • Function name is used to call a function. 
  • Whenever function gets called code written inside function body is execute.
  • If function is returning some value then it must be called into some variable having same data type as return type of a function.
  • Syntax : 
      function_name([arg if any]);
                   Or
      variable = function_name([arg if any]);
  • The value returned by a function will get stored into a variable at where we calling it.
Example : Program to print "Welcome" message using function. ( This is function without returning value)

#include<stdio.h>
int main()
{
   void disp_msg()
   {
       printf("Welcome");
       return;
     }
  disp_msg();
return 0;
}

Output :

Welcome


Note : The return type void meaning is empty indicating that function does not return any value.


Example 2 : Program to calculate addition of two numbers using function

#include<stdio.h>
int main()
{
   int a,b,c;
   printf("Enter two numbers : ");
   scanf("%d%d",&a,&b);
   int add(int n, int m)
   {
       int r;
        r = n + m;
        return r;
     }
   c = add(a,b);
printf ("\nAddition = %d",c);
return 0;
}

Output :

Enter two numbers : 6

7

Addition = 13

Note : The parameters defined in function definition are formal parameters(n,m) and parameters passed to function while calling are actual parameters(a,b). It is not necessary to have same names to formal and actual parameters but it can be. 
When we call function value of actual parameters get copied into its formal parameters so value of a,b gets copied into n,m respectively. Value returned by function get assigned to variable c. Thus variable c contains addition of a,b.

                           (Continue in part 2......)

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.