Functions in C

Last Updated Nov 20, 2015, 07:00:14 PM





A function is a self-contained block of code which accomplishes a specific task. Functions can be used or called repeatedly over time

Functions take data as input and return the result as output after processing it. Functions usually combines many lines of code into a single function to perform a task

When a function is called in the program, the execution stops there and begins the function execution which generally starts from the first line inside the function

How does A Function Work?

1. When the function is called, the program execution starts from the line of code where the function contains

2. The program begins the execution by entering inside the function body

3. In the function body, every instruction is executed from top to bottom

4. Once it is done, the program goes back where it started from

5. All the computed data is returned

How to Define a Function

Evrery function is programming contains four different parts like we see in the above syntax( return type, function name, parameters) and body

Return Type:

The return type indicates what type of value should the function return?. The return type must be explicitly specified when declaring a function

Function Name:

The name of the function we want to call in the program. For example, a function name for adding two numbers can be addition_of_two_numbers etc

Parameters:

When we define a function, we can pass different set of arguments or parameters. The parameters the data that we supply to the function

Body:

The body of the function contains the actual logic and statements to perform various tasks within the function

Types of Functions

1. Predefined Functions:Predefined functions are already defined functions by various libraries such as printf(), scanf(), main() etc.. We can simply use these function just by calling them whenever we need

2. User Defined Function: These functions are created by programmers to implement or perform different tasks.



Example

In the above example, we have function named bigger which has two parameters int a, int b

Try It Now

Why we use functions?

There are many advantages of using functions in programming, some of these are

  • Reusability of the code
  • Easy debugging of software
  • Eliminates the duplication or redundancy of code
  • Finally, functions improve the readability of code

How to use functions in C Programming

Function Declaration:

Before any function is used in the program, it must be declared. It applies only to the user-defined functions, as we already know the predefined functions need not be declared as they were already declared in the libraries.

The declaration of the function helps the compiler to understand what is the name of the function, and how to use or call the function within the program. It is just similar to having a name for a person to call him

Syntax

In the above syntax the return type indicates what type of value it should return. Ex (int, void, double etc.), and parameters are the data that we pass to the function

How to call a function in C

Calling a function in programming is like, calling your friend when you need him with his name. You define a function before you use it, and you'll call in the program when the function needs to perform a task or do something with the data. This is what a function calling means. Let us see an example on how this process works.

Try It Now

In the above example, total(x, y); is a function call and x,y,z; are parameters that we passed to the function

Functions with Arguments

Every function accepts arguments or parameters as a data but these parameters do not return any value back to the calling program. There are two different ways to pass arguments or parameters to a function. Let us see try to understand them.

Two Ways of Passing Argument to Function in C Language

  • Call by Value
  • Call by Reference
Call by Value

In call by value mechanism, the called function creates a new set of variables in stack and copies the values of the arguments into them

Call by Reference

Call by Reference mechanism copies the actual address of argument into the formal parameter. In this case, address location can be used to call the argument. Unlike, call by value any changes made to the formal parameters does effect the actual parameter



Here are some interesting threads online about this topic, it might help you learn more

Difference Between Call By Reference And Call By Pointer? - StackOverFlow

What is the difference between call by value and call by reference in C? - Quora