Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Parameterized Functions

Till now we were using functions that do not accept anything from caller function and do the fixed operation. But what if we want to pass values to the called function from caller function? Does C provide facility to achieve this type of condition? Yes! C has provided us with the parameterized functions to achieve this type of condition.

Parameterized function is the function that can accept values sent from the caller function.

Suppose we want to create two functions that can perform addition & subtraction on different values sent from caller function. Then we should create two parameterized functions that can accept values sent from caller function.

Note

The values that are sent from caller function are known as arguments, and the variables that are used in function header to receive values are known as parameters.

Example of parameterized functions

void addition(int a, int b, int c)
{
    int result = a+b+c;
    printf("\n Addition :: %d",result);
}

Above addition() function can receive any three int values from caller function and operate on it.

void subtraction(double a, double b)
{
    double result = a-b;
    printf("\n Subtraction :: %lf",result);
}

Above subtraction() function can receive any two double values from caller function and operate on it.

Let’s see how parameterized function works

#include <stdio.h>
void addition(int a, int b, int c);
void subtraction(double a, double b);

int main() 
{
    addition(10,20,30);
    subtraction(50.54,15.11);
    return 0;
}

void addition(int a, int b, int c) 
{
    int result = a+b+c;
    printf("\n Addition :: %d",result);		
}

void subtraction(double a, double b) 
{
    double result = a-b;
    printf("\n Subtraction :: %lf",result);		
}

Output

Addition :: 60

Subtraction :: 35.430000

For the better understanding of the above program look at the diagram shown below:

C Programming Language Parameterized Function Working

Arguments can be of any type. Like constant, expression, variable or function call.

Note

To understand the program given below, you need to understand topic Returning a value from a function.

Example

#include <stdio.h>
int addition(int a, int b, int c);
float subtraction(float a, float b);

int main()
{
    int x = 5;
    int y = 6;
    int add;
    float sub;
    
    add = addition(x,3,x*y);
    printf("\n Addition :: %d",add);
	
    sub = subtraction(subtraction(15.5,5.5),5.5);
    printf("\n Subtraction :: %lf",sub);
	
    return 0;
}

int addition(int a, int b, int c) 
{
    int result = a+b+c;
    return result;	
}

float subtraction(float a, float b) 
{
    float result = a-b;
    return result;		
}

Output

Addition :: 38

Subtraction :: 4.500000

Note

A function cannot be defined inside another function. That is, functions cannot be nested.

 

Leave a reply