Function Calling in C Language

Categories: C language

A function call is an important part of the C programming language. It is called inside a program whenever it is required to call a function. It is only called by its name in the main() function of a program. We can pass the parameters to a function calling in the main() function.


Syntax:

Add(a, b) // a and b are the parameters  

Let's consider a program to call a function in C programming languages.


Add.c

#include <stdio.h>  

int add(int a, int b);   

void main()  

{  

  

int sum;  

int a, b;  

printf(" Enter the first and second number \n");  

scanf("%d %d", &a, &b);  

sum = add(a, b); // call add() function  

printf( "The sum of the two number is %d", sum);  

}  

int add(int n1, int n2) // pass n1 and n2 parameter  

{  

int c;  

c = n1 + n2;  

return c;  

}  

Output:

Enter the first and second number

5

6

The sum of the two number is 11


Call by Value:

When single or multiple values of an actual argument are copied into the formal parameter of a function, the method is called the Call by Value. Hence, it does not alter the function's actual parameter using the formal parameter.


Consider a program to demonstrate the Call by Value in C programming.


Call_Value.c


#include <stdio.h>  

int main()  

{  

int x = 10, y = 20;  

printf (" x = %d, y = %d from main before calling the function", x, y);  

CallValue(x, y);  

printf( "\n x = %d, y = %d from main after calling the function", x, y);  

}  

int CallValue( int x, int y)  

{  

x = x + 5;  

y = y + 5;  

printf (" \nx = %d, y = %d from modular function", x, y);  

}   

Output:

x = 10, y = 20 from main before calling the function

x = 15, y = 25 from modular function

 x = 10, y = 20 from main after calling the function


Call by Reference:

In this method, the address of the actual argument is copied into the function call's formal parameter; the method is known as Call by Reference. If we make some changes in the formal parameters, it shows the effect in the value of the actual parameter.


Consider a program to demonstrate the Call by Reference in C programming.


Call_Ref.c

#include <stdio.h>  

int main()  

{  

int x = 10, y = 20;  

printf (" x = %d, y = %d from main before calling the function", x, y);  

CallValue (&x, &y);  

printf( "\n x = %d, y = %d from main after calling the function", x, y);  

}  

int CallRef( int *a, int *b)  

{  

*a = *a + 5;  

*b = *b + 5;  

printf (" \nx = %d, y = %d from modular function", *a, *b);  

}  

Output:

x = 10, y = 20 from main before calling the function

x = 15, y = 25 from modular function

 x = 15, y = 25 from main after calling the function

Top Blogs
C Language Interview Question and Answers Published at:- Benefits of C language over other programming languages Published at:- History of C Language : Introduction to C Programming Language Published at:- How does C Programming Language Work Published at:- Importance of C Programming Language Published at:- C Character Set Published at:- Input and Output Functions in C Published at:- Introduction to Implementation of Queue using Linked List Published at:- Definition of C Language Published at:- History of C Language Published at:- Features of C Language Published at:- How to install C Language Published at:- Compilation process in c Published at:- printf() and scanf() in C Published at:- Variables in C Language Published at:- Types of Variables in C Language Published at:- Data Types in C Published at:- C Identifiers Published at:- C Operators in c Language Published at:- C Format Specifier in C Language Published at:- Escape Sequence in C Published at:- What is ASCII code? Published at:- Constants in C Published at:- Tokens in C language Published at:- Operators in C Language Published at:- C Boolean in C language Published at:- Boolean with Logical Operators in C language Published at:- Static in C Language Published at:- Difference in C language Term Published at:- Programming Errors in C Language Published at:- Compile time vs Runtime In C language Published at:- Differences Between Compile-Time and Runtime In C Language Published at:- Conditional Operator in C Language Published at:- Meaning of Bitwise Operator in C Language Published at:- What is the 2s complement in C Published at:- The C Language in the C Control Statement Published at:- C Switch Statement In C Language Published at:- Difference Between if-else and switch Published at:- C Loops of C Language Published at:- do while loop in C Published at:- while loop in C Language Published at:- Properties of while loop in C Language Published at:- for loop in C Language Published at:- Nested Loops in C Language Published at:- Nested Loops in C Language Published at:- C break statement in C Language Published at:- C continue statement in C Language Published at:- C goto statement in C Language Published at:- C Functions Published at:- Types of Functions in C Language Published at:- Call by value and Call by reference in C Language Published at:- Recursion in C Language Published at:- Recursive Function In C Language Published at:- Storage Classes in C Published at:- C Array in C Language Published at:- Two Dimensional Array in C Language Published at:- What is an Array in C Language Published at:- Passing Array to Function in C Published at:- C Pointers in C Language Published at:- C Double Pointer (Pointer to Pointer) Published at:- Pointer Arithmetic in C Language Published at:- C Double Pointer (Pointer to Pointer) in C Language Published at:- Pointer Arithmetic in C Language Published at:- Pointer to function in C Language Published at:- Dangling Pointers in C Language Published at:- sizeof() operator in C Language Published at:- const Pointer in C Language Published at:- Pointer to Constant In C Language Published at:- void pointer in C Language Published at:- Advantages of void pointer in C Language Published at:- C dereference pointer in C Language Published at:- What is a Null Pointer in C Language Published at:- C Function Pointer in C Language Published at:- Function pointer as argument in C Language Published at:- Dynamic memory allocation in C Language Published at:- C Strings In C Language Published at:- Traversing String in C Language Published at:- Accepting string as the input in C Language Published at:- Pointers with strings in C Language Published at:- C gets() and puts() functions in C Language Published at:- C String Functions in C Language Published at:- C Math in C Language Published at:- C Structure in C Language Published at:- What is Structure in C Language Published at:- typedef in C Language Published at:- C Array of Structures Published at:- Nested Structure in C Language Published at:- Types of Nested Structure in C Language Published at:- Passing structure to function in C Language Published at:- Structure Padding in C Language Published at:- Why structure padding in C Language Published at:- Changing order of the variables In C Language Published at:- Union in C Language Published at:- Deciding the size of the union in C Language Published at:- File Handling in C Language Published at:- C fprintf() and fscanf() in C Language Published at:- C fputc() and fgetc() in C Language Published at:- C fputs() and fgets() in C Language Published at:- C fseek() function in C Language Published at:- C rewind() function in Language Published at:- C ftell() function in C Language Published at:- C Preprocessor Directives in C Language Published at:- C Predefined Macros in C Language Published at:- C #include in C Language Published at:- C #define in C Language Published at:- C #undef in C Language Published at:- C #ifdef in C Language Published at:- C #ifndef in C Language Published at:- C #if in C Language Published at:- C #else in C Language Published at:- C #error in C Language Published at:- C #pragma in C Language Published at:- Command Line Arguments in C Language Published at:- C Expressions in C Language Published at:- Inception Of C Language Tutorial for Beginners Published at:- The C Compiler work in C language and its important Published at:- Program Structure with “Hello World” Example Published at:- Data Segments in C Language Published at:- Flow of C Program in C Language Published at:- What is a programming language in C Language Published at:- Differences between Machine-Level language and Assembly language Published at:- Differences between Low-Level language and High-Level language Published at:- Enum in C Language Published at:- What is getch() in C Language Published at:- What is the function call in C Language Published at:- Function Calling in C Language Published at:- Difference between typedef and define in C Published at:- Use of typedef keyword in Structure C Language Published at:- Program in C Language with Practical Published at:- Difference between the typedef and the #define in C Published at:-
R4R.co.in Team
The content on R4R is created by expert teams.