C Functions

Mostly we see that every C program has at least one function such as main(). We can use more than two C Functions in a single C Program.
A function is a group of statements that together performs a task. All C programs are written using functions to improve re-usability and understandability.


Advantages of C Functions:

Here are some main advantages of Functions in C:

  • Code Re-usability:

We can call the C Functions many times. So there will be no need to write the same code again and again.

  • Code optimization:

C Functions help in the optimization of the C Program because we don’t need to write much code.


Function Declaration/Function Prototype

Declaration of C Function, tells the compiler about a function’s name, it’s the return type and the parameters. We can define the actual body of the function separately.

Important points for the Function Declaration:

  • Function declaration in C always ends with a semicolon.
  • In C Language, by default, the return type of a function is an integer(int) data type.
  • A Function declaration is also known as a function prototype.
  • In function declaration name of parameters are not compulsory, but we must define their datatype. Hence the following declaration is also valid.
    int getSum(int, int);
  • If function definition is written before the main function then function declaration is not required whereas, if function definition is written after the main function then we must write function declaration before the main function.

Syntax of Declaration of Function

return_type function_name(data_type parameter, data_type parameter)
{
// code to be executed
}

Example of Function Declaration/Prototype

#include
void display(int x); // function decalration or prototype
int main()
{    
int a=1;
 display(a); // calling a C Function
return 0;      
}      
void display(int x)
{
 printf("x=%d",x);
}

Definition of C Functions:

When we define a function, we provide the actual body of the function. A function definition provides the following parts of the function.

  • The return type is a data type of the value(return by the function).
  • Within curly braces the function body is present.
  • A function may have 0 or more parameters. When a function is invoked, you pass a value to the parameter. You can have any type of parameter in C program such as int, float, char, etc.

Conclusion: A function definition specifies the name of the function, the types and number of parameters it expects to receive, and its return type. A function definition also includes a function body with the declarations of its local variables, and the statements that determine what the function does.

Syntax:

type functionName(int a, int b)
{ statement; }


Calling C Functions

Once when we have declared and defined a function, we can call it anywhere in the program as many times. The function will return the value based on our parameters. A function can be called many times.

Simple Example of C Function :
Here is the simple example of C function to print a message without any return statement.

#include
void display(int x ) // function decalration or prototype
{
 printf("x=%d",x);
 
} 
int main()
{    
int a=1;
 display(a); 
return 0;      
}

Output:

Tutorials Class - Output Window

x=1

Another Example of C function:
Here is another example of C Function to print a variable using the return statement.

#include
int display(int x ) // function decalration or prototype
{
 printf("x=%d",x);
 return(x);
} 
int main()
{    
int a=1;
 display(a); 
return 0;      
}

Output:

Tutorials Class - Output Window

x=1


While calling a function, we can pass the parameters by the following two ways:

  • call by value
  • call by reference

Now, we are going to learn these things one by one.


Call by Value

When the copy of actual parameters is passed to respective formal parameters, then it is defined as call by value. If we will change the formal parameter it will not reflect the actual parameter.
Example of Call by Value:

#include   
 void show(int variable ) 
{  
    printf("Before adding value inside function num=%d \n",num);  
    variable=variable+200;  
    printf("After adding value inside function num=%d \n", num);  
}  
  
int main() 
{  
    int num=100;  
    printf("Before the function call number=%d \n", num);  
    change(x);//passing copy of actual arguments(int num) in function  
    printf("After the function call number=%d \n", num);  
    return 0;  
}

Output:

Tutorials Class - Output Window

Before function call number=100
Before adding value inside function variable=100
After adding value inside function variable=300
After function call number=100

In above example, int num is actual parameter and in function change(), int variable is formal parameter.


Call by Reference

In call by reference, the address of actual arguments is passed to the formal arguments, hence any change made to formal arguments will also reflect in actual arguments.

Example of Call by Reference:

#include   
void show(int *num) 
{  
    printf("Before adding value inside function num=%d \n",*num);  
    (*num) +=300;  
    printf("After adding value inside function num=%d \n", *num);  
}  
  
int main() 
{  
    int x=100;  
    printf("Before function call x=%d \n", x);  
    change(&x);//passing reference/address of actual argument(int x) in function  
    printf("After function call x=%d \n", x);  
    return 0;  
}

Output:

Tutorials Class - Output Window

Before function call x=100
Before adding value inside function num=100
After adding value inside function num=400
After function call x=400

In call by reference, we pass the address of actual arguments in formal parameter
Note: In call by reference, formal arguments should be pointer type, because address type value can hold only pointer-type variable.


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.