c
C Programming Tutorial : Introduction

Function Arguments in C, and Types Of Functions

There are two types of function arguments in C Programming language.

  1. Actual arguments
  2. Formal arguments
When a calling function sends some values to the called function for communication, then these values are called arguments or parameters. These arguments serve as input data to the function to carry out the specified task.

Actual arguments:

The arguments which are mentioned in the function call are known as actual arguments. Since these are the values that are sent to the called function. Actual arguments can be written in the form of variables, constants or expressions, or any function call that returns a value.

For example-

Syntax-

#include<stdio.h>
return-type func-name(arguments); /*formal arguments*/
{
……………………………
statements;
…………………………….
}
int main()
{
……………………………..
func-name(arguments_value); /*actual arguments*/
……………………………..
return 0;
}
 

Formal arguments:

The name of the arguments, which are mentioned in the function definition are called formal or dummy arguments. Since they are used just to hold the values that are sent by the calling function.

These formal arguments are simply like other local variables of the function which are created when the function call starts and are destroyed when the function ends. However, there are two differences.

  1. First is that formal arguments are declared inside parentheses while other local variables are declared at the beginning of the function block.
  2. The second difference is that the formal arguments are automatically initialized with the values of the actual arguments passed. Variables are assigned values through the statements written inside the function body.

The order, number, and type of actual arguments in the function call should match with the order, number, and type of formal arguments in the function definition,

/* Program to understand formal and actual arguments */
#include<stdio.h>
void main( )
{
int m=6, n=3;
    printf(“%d\t", multiply(m,n));
    printf("%d\t”, mnltlply(15,4);
    printf("%d\t", multiply(m+n,m-n));
    printf("%d\n", multiply(6,sum(m,n));
}
multiply (int x, int y)
{
    Int p;
    P=x*y;
    return p;
}
sum (int x, int y)
{
    Return x+y;
}

Output:
18	60	27	 54
 

In this program, function multiply( ) is called 4 times. The variables x and y are the formal arguments of multiply( ).

  1. The first time when the function is called, actual arguments are variables m and n, so the formal arguments x and y are initialized with the values of m and n, and p=18 is returned.
  2. The second time function is called with actual arguments 15 and 4, so this time x and y are initialized with values 15 and 4 respectively, hence p=60 is returned.
  3. Similarly, third-time x and y are initialized with values of m+n=9 and m-n=3 respectively, hence p=27 is returned.
  4. In the fourth call, the first argument is a constant value (6), while the second argument is a function call. So this time, x is initialized by 6, and y is initialized by the value returned by the function sum( ) i.e. 9. Hence this time p=54 is returned.

The names of formal and actual arguments may be the same or different because they are written in separate functions.

Example-

/*Program to understand formal and actual arguments */
#include<stdio.h>
main ( )
{
    int a=6.b=3;
    func(a, b); 
    func (15, 4);
    func(a+b.a-b);
}
func (int a. int b)

{
    printf ("a = %d b =%d\n",a,b);
}

output:
a=6	    b=3
a=15	b=4
a=9 	b=3
 

In the above program, we can see that the values of a and b inside main( ) are 6 and 3, while a and b inside func( ) are initialized with values of actual arguments sent on each call. Any changes made to the formal arguments inside the function do not affect the actual arguments.

Types of Functions

The functions can be classified into four categories on the basis of the arguments and return value.

  1. Functions with no arguments and no return value.
  2. Functions with no arguments and a return value.
  3. Functions with arguments and no return value.
  4. Functions with arguments and a return value.

Functions with no arguments and no return value.

Functions that have no arguments and no return value are written as­

void func (void) ;
main()
{
……………………..
func ();
…………
}
void func (void)
{
……………………….
Statements
………………………..
}
 

In the above example, the function func( ) is called by main () and the function definition is written after the main() function. As the function func( ) has no arguments, main() cannot send any data to func( ) and since it has no return statement, hence function cannot return any value to main(). There is no communication between the calling and the called function. since there is no return value these types of functions cannot be, used as operands in expressions.

Let us see one example program-

/*Program that uses a function with no arguments and no return values*/
#include<stdio.h>
void dispmenu(void);
main()
{
int choice;
dispmenu();
printf("Enter your choice :");
scanf("%d",&choice);
}
void dispmenu (void)
{
printf("1. Create database\n");
printf("2. Insert new record\n");
printf("3. Modifya record\n");
printf("4. Delete a record\n");
printf("5. Display all records\n");
printf("6. Exit\n");
}
 

Function with No Arguments but A Return Value:

These types of functions do not receive any arguments, but they can return a value.

Syntax-

int func (void);
 main()

{
Int r;
……………………………….
r=func ( );
………………………………….
}
int func (void)
{
…………………….
…………………….
return (expression);
}
 

The next program uses a function of this type

Example-

/* Program that returns the - sum of squares of all odd numbers from 1 to 25*/
#include<stdio.h>
Int func (void);
Main()
{
printf("%d\n", func());
}
int func (void)
{
int num, s=0;
for(num=1; num<=25; num++)
{
if(num%2 !=0)
s+=num*num;

}
return s;
}

Output:
2925
 

Function With Arguments But No Return Value:

These types of functions have arguments, hence the calling function can send data to the called function but the called function does not return any value, These functions can be written as-

Syntax-

void func(int, int);
main ()
{
………………………
Func(a,b);
……………………….
}
Void func(int c, int d)
{
…………………………
Statements;
…………………………
}
 

Here a and b are actual arguments which are used for sending the value, c and d are the formal arguments, which accept values’ from the actual arguments.

Example-

/* Program to find the type and area of a triangle.*/
#include<stdio.h>
#include<math.h>
void type (float a, float b, float c);
void area (float a, float b, float c);
main()
{
    float a,b,c;
    printf("Enter the sides of triangle ");
    scanf("%f%f%f",&a, &b, &c);
    if(a<b+c && b<c+a && c<a+b)
        {
            type(a, b, c);
            area(a,b,c);
        }
    Else
            printf("No triangle possible with these sides\n");
}
void type(float a, float b, float c)
{
    If((a*a)+(b*1)==(c*c) II (b*b)+(c*c)==(a*a) II (c*c)+{a*a)==(b*b))
        printf("The triangle is right angled triangle\n");
    if(a==b && b==c)
        printf("The triangle is equilateral \n");
    else if {a==b II b==c ||c==a)
        printf("The triangle is isosceles\n");
    else
        printf("The triangle is scalene\n");
}
Void area(float a, float b, float c)
{
    Float s; area;
    S=(a+b+c)/2;
    Area=sqrt(s*(s-a)*(s-b)*(s-c));
    Printf("the area of tringle = %f\Nn",area);
}
}
 

Function With Arguments And Return Value

These types of functions have arguments, so the calling function can send data to the called function, it can also return any value to the calling function using return statement. This function can be written as-

Syntax-

int func (int, int);
main ( )
{
int r;
…………………………
r=func (a, b);
………………………….
func(c,d) ;
}
int func (int a, int b)
{
…………………..
…………………..
Return (expression);
}
 

Here return statement returns the value of the expression to the calling function. The functions sum(), multiply(), max( ) that we had written earlier’ are examples of these types of functions. Let us take one more example program-

Example-

/*Program to find the sum of digits of any number*/
#include<stdio.h>
int sum (int n);
main()
{
int num;
printf("Enter the number ");
scanf("%d”,&nu);
printf("Sum of digits of %d is %d\n", num, sum(n,um) );
}
int sum (int n)
{
int i, sum=O, rem;
while(n>0)
{
rem=n%10;	/*rem takes the value of last digit*/
sum+=rem;
n/=10;		/* skips the last, digit of number*/
}
Return (sum);
}