C - Functions

A Function play an import role in a program development. A function is a block of statements that performs a task. Instead of writing a block of code again and again, we can create a function, write the code inside it and call the function as many times as required. When the function is call, the controller is passed to that function, execute the function statements and return back, from where it is called.

Features of the function:
  • Function reduces the size of a program as we avoid writing the same code again and again.
  • A function can return only one value at a time.
  • Any function can be called from any other function.
  • Function provide modularity and code reusability.
A function consists of 3 parts:

1. Prototype or declaration: A function declaration tells the compiler about the program by giving details such as function name, the number and type of arguments, and the return data type in advance. Syntax:

return_datatype function_name(datatype1, datatype2, …);

2. Calling: Calling a function means a controller is passed to a function and return back after executing the function, while calling a function mention the function name with its parameter. Syntax:

variable_name = function_name(variable_name1, variable_name2, …);

3. Body or definition: The function body contains a collection of statements that defines the logic to solve a particular problem. Syntax:

return_datatype function_name(datatype variable1, datatype variable2, …) 
{
	statement 1; 
	statement 2; 
}

Return data type: A function may or may not return a value, but it cannot return more than one value at a time. The return data type, returns the result back from where a function is called. If the function is not returning any value, in that case you must prefix void keyword before function name. If no data type is specified, the default return data type is integer.

Function name: It is a identifier name, defined by the user, to name a function.

Parameter: A parameter is like a placeholder. When a function is called, you can pass a value to that function via parameter. Data type in parameter is also referred to as the actual parameter or argument. The parameter list refers to the data type, argument order, and number of the arguments in a function. Parameters are optional, you can provide zero or any number of arguments, there is no maximum limit.

Function can be written as:
  1. Function with no argument, no return type
  2. Function with argument, no return type
  3. Function with no argument, with return type
  4. Function with argument, with return type
Main is also a function, like other function, Main can return value, pass arguments, and can be called from other function.

Function Example

Example 1: WAF to print a message "Hello" using a function with no argument and no return type.

#include<stdio.h>
void fun(); // function prototype or function declaration
void main()
{
    printf("I am in main() function\n");
    fun();
    fun();
    printf("I am back in main() function\n");
}
void fun()
{
    printf ("Hello! I am in user-defined function\n");
}
I am in main() function
Hello! I am in user-defined function
Hello! I am in user-defined function
I am back in main() function

Example 2: WAF to find sum of 2 numbers using function with argument and no return type.

#include<stdio.h>
void sun(int, int);
void main()
{
    int a, b;	
    printf("Enter 2 numbers: ");
    scanf("%d",&a);
    scanf("%d",&b);
    sun(a, b);
}
void sun(int a, int b)
{
    int c;
    c=a+b;
    printf("Sum = %d",c);
}
Enter 2 numbers: 10
20
Sum = 30

Example 3: WAF to find sum of 2 numbers using function with argument and with return type.

#include<stdio.h>
int sun(int, int);
void main()
{
    int a, b, c;	
    printf("Enter 2 numbers: ");
    scanf("%d",&a);
    scanf("%d",&b);
    c=sun(a, b);
    printf("Sum = %d",c);
}
int sun(int a, int b)
{
    int c;
    c=a+b;
    return c;
}
Enter 2 numbers: 10
20
Sum = 30
Advertisement