< Prev
Next >

# C - Functions with Arguments

In our last tutorial, we have explained how to create simple functions that take no arguments i.e. function with empty parenthesis() and hence they were not passed any values when they were called.

In this tutorial, we are going to explain how to make functions with arguments i.e functions that take arguments and pass values to them when they are called.

For doing this, when declaring the prototype of a function, we will have to define the type of each value that will be passed to a function(when it is called), within its parenthesis(). All such values declared in the prototype of the function are also called the formal arguments or formal parameters and when such function is called with values, the passed values are called actual arguments or actual parameters. Let's see an example of how it is done.

## Function with argument example

In the upcoming example, we are going to create a function named square which calculates the square of each variable of int type passed to it, when this function is called.

``````/* Function in C */

#include<stdio.h>
int main()
{
int square(int);       /*function prototype declaration*/

int a= 5;
int result;

result = square(a);	 /* square() function is called */
printf("Square of a is = %d \n", result);

return 0;
}

int square(int num)		   /*function is defined*/
{
int result = num*num;
return result;
}``````

## Output-

````Square of a is = 25`
```

## Program Analysis

• Declaring function prototype - In the above mentioned example, we have created a function named square and have declared its prototype as -

• ``int square(int);    /*function prototype declaration*/``

The prototype of this function is declared with a int return type, hence, this method will return an int value when it is called. This function will be passed an int value when it is called, as we have specified int type within the parenthesis().

• Calling the function - We have called the square method by passing the value of an int variable named a as an argument. The int variable a has a value 5 and it can also be referred as "actual argument" of the square method, when it is called.

``result = square(a);	 /* square function is called */``

• Function definition - When the square function is called, it receives a copy of the value of the variable(a) passed to it and this value is copied into int variable named num declared in the definition of square function. In the function definition, the num variable can also be referred as a "formal argument" of square function. Eventually, the square function calculates the square of an int value contained in num and returns the result, also an int value.

``````int square(int num)		   /*function is defined*/
{
int result = num*num;
return result;
}``````

## Multiple functions with arguments of different type

• We can even create a program with multiple functions having arguments of different type. Let's see how.

``````/* Function in C */

#include<stdio.h>
int main()
{
int square_int(int);		 /*function prototype declaration*/
float square_float(float);	 /*function prototype declaration*/

int a =10;
float f = 10.5;
int result;

/* square_int() function is called */
printf("Square of 10 is = %d \n", square_int(10));

/* square_float() function is called */
printf("Square of 10.5 is = %f \n", square_float(10.5));

return 0;
}

int square_int(int num)		       /* square_int() function is defined*/
{
int square_result = num*num;
return square_result;
}

float square_float(float num)		/* square_float() function is defined*/
{
float square_result = num*num;
return square_result;
}``````

## Output-

``````Square of 10 is = 100
Square of 10.5 is = 110.250000```
```

In the above mentioned coding example, we have create two functions square_int which calculates square of an int variable, and square_float, which calculates square of a variable of type float.

## Functions with multiple arguments

We could even create functions with multiple arguments. Let's see how we do this.

``````/* Passing values to functions in C */

#include<stdio.h>
int main()
{
int addition(int, int);    /*function prototype declaration*/
int subtraction(int, int); /*function prototype declaration*/

int a =10, b=5;
int result;

printf("Result of 10 + 5 = %d \n", result);

result = subtraction(a,b); /* subtraction() function is called */
printf("Result of 10 - 5 = %d \n", result);

return 0;
}

{
}

int subtraction(int a, int b)      /* subtraction function is defined */
{
int subtract = a-b;
return subtract;
}``````

## Output-

``````Result of 10 + 5 = 15
Result of 10 + 5 = 5``````

## Functions with mixed arguments

We could even create functions with arguments of mixed or different types Let's see how we do this.

``````/* Function in C */

#include<stdio.h>
int main()
{
int misc(int,char,float);    /*function prototype declaration*/

int i= 5;
char c ='a';
float f= 9.9;
misc(i,c,f);	   /* misc() function is called */
return 0;
}

int misc(int a, char ch, float fl)		/*function is defined*/
{
printf("int value : %d \n",a);
printf("char value : %c \n", ch);
printf("float value : %f \n", fl);
}``````

## Output-

``````int value : 5
char value : a
float value : 9.900000``````

Note : In the above mentioned code we are going to use format specifiers to print value of different types of variables, such as -
• %d format specifier for printing an int type variable.
• %c format specifier for printing a char type variable.
• %f format specifier for printing an float type variable.