Functions in C/C++ | User-defined Functions in C/C++ Programming with examples

In computers, each task that it performs is known as Function. Like whether we cut/copy or paste, each is termed as a function. These are one of the very useful features provided to the user by any programming technology. In this tutorial, we are going to see how to declare functions, how to call them(ask them to operate now) or how they work and all.

What are Functions in C/C++?


In every programming language, a function is a block of code that performs a particular task. It is the most re-usable and most modular aspect of a programming language. These are of 2 types : Predefined and User-defined . Some of the predefined functions are already being used by us, including printf() and scanf() and more will come as we proceed further. Even the main() is also a function and a very essential component of a program.

 But for this tutorial, our main topic of concern is User defined functions. These are created by the user to reduce the complexity of large programs, by dividing the program into components or block of codes, which will get executed whenever that block is called for execution. Every program has at least one function, that is the main function. 

Function prototype in C

Function prototyping is simply the other term used for declaring a function. It tells us about the returnType of the function, its name, and the parameters & their type it is using. Also the order in which the parameters are written in the prototype, the same order is followed by arguments are passed to the function, as shown in the below figure.


returnType funcName (parameter_1, parameter_2, ...., parameter_n);

* When we declare a function, it is required to return us a value after performing the necessary tasks. So this returnType signifies what type of value the function is going to return to the main(). This value can be of int data-type, float, double, char etc. If the function returns nothing, then the returnType is void.

* funcName is an identifier that is used to uniquely identify the function in the program. It can be any valid name, but not a keyword. 

* The parameter list declares the type and number of parameters that the function accepts when it is called. In some cases, the function we declared has to operate on some values that are not of its own, but passed to it, through the main() funtion as arguments during function call, so this parameter list holds those values.

* The function body is denoted by a pair of curly braces {}. All the function code or the body of the function is contained in it.

Valid function declarations/proto-typing:

int sum(int a,int b);

int sum(int ,int); 

char function_1(int a, char ch);

float function_2(float c, long l);

double d(char c);

void my_funct_12();

Invalid function declarations:

char 23foo(int a);

void my.function();

int max-num();

int #myf();

*************************************

USUALLY FUNCTIONS ARE DECLARED ON THE TOP OF THE PROGRAM, I.E , ABOVE MAIN(). BUT IT IS NOT NECESSORY TO DO SOYOU CAN DECLARE IT WHERE-EVER YOU LIKE . YOU NEED TO GIVE THE FUNCTION PROTOTYPE IN THIS CASE .

*************************************

Example:

#include<stdio.h>
int function(int , int );   //function prototype

int main()
{
function(a,b);   //function calling
}

/*full function 
declaration
*/
int function(int a,int b) 
{
//statements
}

* GIVE MEANINGFUL NAMES TO THE FUNCTIONS , SO THAT YOU CAN EASILY IDENTIFY WHAT THE FUNCTION DO, ONLY BY READING ITS NAME.

*************************************

Programs :

Q. Write a function in C to add two numbers ?

#include<stdio.h>

int add(int a, int b);     // function prototype

int main() 
{
    int i, j, result;
    printf("Please enter 2 numbers you want to add...");
    scanf("%d%d", &i, &j);
    
    result = add(i, j);        // function call
    printf("The result of addition is: %d", result);
    
    return 0;
}

int multiply(int a, int b)
{
    return (a+b);       // function defintion                                                //this can be done in                                                //  one line
}

Q. Print all the prime numbers between A and B , where A and B are any 2 integer values?

#include <stdio.h>
void print_prime(int , int);
int main()
{
  int num1, num2;
  printf("Enter two range(input integer numbers only):");
  scanf("%d %d", &num1, &num2);
  print_prime(num1,num2);
  return 0;
}
void print_prime(int num1,int num2);
{ int flag_var, i, j;
 for(i=num1+1; i<num2; ++i)
   {
      flag_var=0;
      for(j=2; j<=i/2; ++j)
      {
         if(i%j==0)
         {
            flag_var=1;
            break;
         }
      }
      if(flag_var==0)
         printf("%d\n",i);
}

********************************

ANOTHER IMPORTANT POINT HERE IS, THAT IN main() THE VARIABLES num1 AND num2 ARE COMPLETELY DIFFERENT FROM THE VARIABLES DECLARED IN void print_prime(int num1,int num2) , THIS IS DUE TO THE CONCEPT OF LOCAL VARIABLESTHE num1 AND num2 VARIABLES ARE LOCAL TO THE main() FUNCTION AND OUTSIDE IT THEY HAVE NO EXISTENCE AND THE ONES DECLARED IN PRINT_PRIME FUNCTION ARE FUNCTION DECLARED TO STORE THE VALUE COMMING FROM THE main() FUNCTION.

********************************

Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post