c
C Programming Tutorial : Introduction

C Functions: Function Definition and Function Declaration and Function Call

Function Definition

The function definition consists of the whole description and code of a function. It tells what the function is doing and what are its inputs and outputs. A function definition consists of two parts – a Function header and a function body. The general syntax of a function definition is-

Syntax-

return_typr func_name ( typel argl, type2 arg2,)
{
local variables declarations;
statement;
…………………………….
return(expression);
}
 
  1. The first line in the function definition is known as the function header and after this the body of the function is written enclosed in curly braces. The return_type denotes the type of the value that will be returned by the function.
  2. func_name specifies the name of the function and it can be any valid C identifier. After function name the argument declarations are given in parentheses. It is mentioning the type and name of the arguments. These are known as formal arguments and used to accept values.
  3. The body of function is a compound statement (or a block). It is consisting of declarations of variables and C statements followed ‘by an optional return statement. The variables declared inside the function are known as local variables.

The function definition can be placed anywhere in the program. But generally, all definitions are placed after the main() function. Note that a function definition cannot be placed inside another function definition. Function definitions can also be placed in different files.

Example-

/*the function definition is-*/
void drawline (void)
{
int i;
for(i=1;i<80;i++)
printf ("-");
}
 

Here the function is not returning any value so void is written at the place of return_type, and since it does not accept any arguments, so void is written inside parentheses.

int sum (int x, int y) /*Function definition*/
{
int s;
S=X+Y;
re'turn s;
}
 

This function returns a value of type int, because int is written at the place of return_type. This function takes two formal arguments x and y, both of type int. The variable s is declared inside the function, so it is a local variable. The formal arguments x and y are also used as local variables inside this function.

Function Call

The function definition describes what a function can do. But actual use of it to call a function somewhere in the program. A function can be called by simply writing, its name followed by the argument list inside the parentheses.

func_name(arg1, arg2, arg3 ...) 

These arguments arg1, arg2, , …are called actual arguments.

Here func_name is known as the called function. The function in which this function call is placed is known as the calling function.

For example in the below program main() is the calling function, sum() is the called function and a, b are actual arguments. The function call is written on the right-hand side of the assignment operator as-

s =sum(a, b);

Example-

/*Program to . find the sum of two numbers*/
#include<stdio.h>
int sum(int x, inty); /*Function declaration*/
main ( )
{
    int a,b, s;
    printf ("Enter values for a and b”);
    scanf ("%d %d", &a, &b);
    s=sum (a, b); 	/*Function call*/
    printf ("Sum of %d and %d is % d \n" ,a,b,s) ;
}
int sum (int x, int y) /*Function definition*/
{
    int s;
    S=X+Y;
    return s;
}
 

Even if there are no actual arguments, the function call should have the empty parentheses, For example, In the below program, the function call is written as-.

drawline( );

Example-

/* Program to draw a line*/
#include<stdio.h>
#include<conio.h>

void drawline (void); /*Function Declaration*/
main()
{
draw1ine();  /*Function Call*/
getch();
}
void drawline (void) /*Function Definition*/ 
{
int i;
for(i=1;i<=80;i++)
printf(" _ " ) ;
}
 

When a function is called, the control transferred to the called function. The called function is executed and then the control is transferred to the statement by following the function call in the calling function. The following figure shows the transfer of control when two functions func1() and func2() are called from main().

If the function returns a value, then this function call can be used like an operand in any expression anywhere in the program. The type and value of this operand will be the same type and value of the return value of the function. For example-

s=sum(a"b);	/*Assigning the return value of sum() to variable s*/

b=max(x, y)*10;	/*return value of max() is multiplied by 10 and assigned to variable b*/

if (isprime(x)==1)	/*return value of isprime() is used in if condition*/

printf(“Number is prime”);

printf("%d\n", sum(a+b) );	/*Return value of sum( ) is printed*/
 

If the function is declared as void() then it cannot be used in this way in any expression. For example, it would be meaningless and invalid to write an expression like this-

s=drawline( );

A function call can be used as a statement if a semicolon is placed after it. In this case, if the function returns any value, it is just discarded. For example-

draw(x, y);

display( a, b, c);

printprimes( );

A function call, cannot occur on the left hand of an assignment statement.

func(x, y)=a;	/* Invalid */
 

The code of a function is executed only when it is called by some other function. If the function defined and not called even once, then it is code will never be executed. A function can be called more than once, so the code is, executed each time it is called. The execution of a function finishes either when the closing braces of the function body are reached or if the return statement is encountered.

Function Declaration

The calling function needs information about the called function. If definition of the called function placed before the calling function, then declaration is not needed. For example- if in the below program we write the definition of sum( ) before main( ), then the declaration is not needed.

Example-

/*Program to  find the sum of two numbers*/
#include<stdio.h>
#include<conio.h>
int sum(int x, inty); /*Function declaration*/
main ( )
{
    int a,b, s;
    printf("Enter values for a and b");
    scanf("%d %d", &a, &b);
    s=sum (a, b); 	/*Function call*/
    printf("Sum of %d and %d is % d \n" ,a,b,s) ;
}
int sum (int x, int y) /*Function definition*/
{
    int s;
    S=X+Y;
    re'turn s;
}
 

See the below program. Here the definition of sum( ) is written before maine(). So maine() knows everything about the function sum().

Example-

/*Program to find the sum of tow numbers*/
#include<stdio.h>
#include<conio.h>

int sum(int x, int y)		/*function definition*/
{
    int s;
    i=x+y;
    return s;
}
void main()
{
    int a,b,s;
    pr'intf ("enter values for a and b ");
    scanf("%d %d",&a,&b);
    s=sum(a,b)	/*function call*/
    printf("sum of %d and %d is%d\n",a,b,s);
    getch();
}
 

But generally, the function main() is placed at the top and all other functions are placed after it. In this case, a function declaration is needed. The function declaration is also known as the function prototype, and it informs the compiler about the following three things-

  1. Name of the function
  2. Number and type the arguments received by the function.
  3. Type of value returned by the function.

A function declaration tells the compiler that a function with these features will be defined and used later in the program. The general syntax of function declaration’s is-

return_type func_name(typel arg1, type2 arg3, .. , .....); 

This just looks like the header of a function definition, except that there is a semicolon at the end. The names of the arguments while declaring a function are optional. These are only used for descriptive purposes. So we can write the declaration in this way also-

return_type func_name(type1, type2, ........); 

Now we will write two more example programs that use functions.

Example-1

/*Program that finds whether a number is even or odd*/
#include<stdio.h>
#include<conio.h>
void find(int);
main()
{
    int num;
    printf ("enter a number ");
    scanf("%d",&num);
    find(num);
}
void find (int n)
{
    if(n%2==0)
    printf ("%d is even\n",n);
    else
    printf ("%d is odd\n", n);
}
 

Example-2

/*Program that finds the larger of two numbers*/
#include<stdio.h>
#include<conio.h>
int max (int x, int y);
main( )
{
    int a, b;.
    printf ("Enter two numbers:”);
    scanf("%d%d",&a,&b);
    printf ("Maximum of %d and %d is %d\n", a, b, max(a,b);
}
max ( int x, int y)
{
    if(x>y)
    return x;
    else
    return y;
}