• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Types of Function in C Programming Example | Call by Value vs Call by Reference

Tarshal Nimawat/11169/2
C / C++Code

Do you know,

  • What are the different types of functions in C?
  • How to declare, define and call the function in C?
  • What is the difference between call by value and call by reference?

In this tutorial, I will explain all these points one-by-one with programming examples.

C language is a procedure-oriented programming language.

The program is divided into small blocks known as functions, which enhances the reusability and efficiency of the program.

Table of Contents

  • What is the function in C?
    • main() Function in C
    • Built-in Functions in C
    • User Defined Functions in C
    • User Define Function Example:
  • Function calls and pointer:
  • Difference between call by value and call by reference
    • Call by value
    • Call by reference
  • Conclusion

What is the function in C?

A function is a block that contains a well-organized set of instructions that are performed whenever the function is called.

Example:

The best example of a function is the ‘main()’ function.

main() Function in C

What is special about the main function?

  • It is a special function because the execution of a C program starts from this function.
  • When all the instructions and function calls present in main() are executed, the C program ends.
  • Every C program must contain one and only one main() function.

Built-in Functions in C

Other examples of functions would be printf(), scanf(), getch(), etc.

These are the functions which are already defined to the compiler and are stored in libraries like stdio.h and conio.h.

User Defined Functions in C

However, we can always write user-defined functions as per our requirements.

There are two steps for establishing a function in C.

  • function declaration
  • function definition

Function Declaration in C

Function declaration means declaring the function by stating its return type, function name, and parameters.

What is the return type in function?

The return type specifies the nature of value returned by the function. The function might return anything like integers, characters, decimals, etc.

The return type void is used whenever the function returns nothing.

The parameters are the list of arguments which are fed as input to the program.

On the other hand, a function declaration means writing the body of the function.

Function declaration syntax-

return_type function_name(parameters);

Function definition syntax-

return_type function_name(parameters)
{
//body of function;
}

User Define Function Example:

For example, let’s consider a simple program that uses a user-defined function called ‘sum’ which add two integers and prints the sum.

#include<stdio.h>

void sum(int x, int y);

int main()
{
  int a1,b1,a2,b2,a3,b3;
  printf ("enter three pairs of inetegers");
  scanf(“%d%d%d%d%d%d”,&a1,&b1,&a2,&b2,&a3,&b3);
  printf("sum of the first pair is:");
  sum(a1,b1);
  printf("sum of the second pair is:");
  sum(a2,b2);
  printf("sum of the third pair is:");
  sum(a3,b3);
  return 0;
}

void sum(int x, int y)
{
  int s;
  s=x=y;
  printf(“%d”,&s);
}

Further, you can also classify the types of functions based on the number and types of input parameter or return type of the function.

Function calls and pointer:

In C programming, to execute a function, you need to call it.

In the case of a parameterized function, arguments need to pass while function call.

Now, there are two types of function calls–

  • call by value
  • call by reference

Difference between call by value and call by reference

Call by value

In call by value, a copied set of value of arguments is passed to the function.

Hence, all the operations performed on variables does not affect the original set of arguments.

For example: Write a C program to swap the values of variables using Call by Value.

#include<stdio.h>

void swap(int a, int b)

int main()
{
  int x=2,y=4;
  printf("Before swap:");
  printf("x=%d, y=%d",x,y);
  swap(x,y)
  printf("After swap:");
  printf("x=%d, y=%d", x,y);
  return 0;
}

void swap(int a, int b)
{
  int temp;
  temp=a;
  a=b;
  b=temp;
}

The above program passes the arguments x and y to the function called swap by a call by value method. The values of x and y are copied to a and b. Later the values of a and b are interchanged with the help of another integer variable called temp. First, the value of a is stored in temp, then the value of b is stored in a and at last, the value of temp is stored in b.

Call by reference

In call by reference, instead of the copied set, the address of the variables is passed to the function. Hence, the operations performed to change the values in the original set.

Now, whenever a variable is declared or assigned some value, the C compiler allocates a location for it in the memory. The address of this memory cell is a number. So, the data type which can store this address is known as a pointer. While declaring a pointer, the special character ‘*’ is used.

For example:

int *p;

The asterisk means ‘value at the address’.

Hence, *p means values at the address stored in p. Whereas, the & operator gives the address of a variable.

For example: Write a C program to swap the values of variables using Call by Reference or pointer.

Let’s see the swapping program but with the help of pointers this time. In this program, instead of passing the values of x and y, we pass the pointers which store the addresses of x and y.

#include<stdio.h>

void swap(int *p, int *q);

int main()
{
  int x=2, y=4;
  printf("Before swap:");
  printf("x=%d, y=%d",x,y);
  swap(&x,&y);
  printf("After swap:");
  prinytf("x=%d, y=%d",x,y);
}

void swap(int *a, int *b)
{
  int temp;
  temp=*a;
  *a = *b;
  *b = temp;
}

Conclusion

There are many advantages of writing your program with functions. They are very useful as it makes your program more modular.

If you are working on any project, create a separate function for similar logical operations. This will reduce the complexity of your program and it will also be easy for debugging.

This is all about different types of functions in C and the difference between call by value and call by reference. I have also explained it with programming examples.

If you have any specific questions related to the functions in C, write in the comment.

cpp
Tarshal Nimawat
Tarshal is a tech-head CS undergrad, who is always on the lookout for the sharpest cutting edge techs in the business, be it Blockchain, hashgraphs or AI/ML. With a knack for business development, negotiation and tech, she is often found educating those around her.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Dale Moore
    May 21, 2019 at 9:14 pm

    Minor error…
    Change
    Built in Fucntions in C
    to
    Built in Functions in C

    • Reply
      Aniruddha Chaudhari
      May 22, 2019 at 9:51 am

      Thanks, Dale for the correction!

      Updated.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard