Categories: C

Functions in C

A computer program cannot handle all the tasks by itself, instead, it requests other programs like entities called, functions in C, to get its task done. A function is a self-contained block of statements that perform a coherent task of some kind. Every C program can be thought of as a collection of these functions. A function can also be referred to as a method or a sub-routine or a procedure, etc.

Why use functions:
  • Writing a function avoids rewriting the same code over and over, i.e., code reusability increases.
  • By using the function, it becomes easier to write programs and keep track of what they are doing.
  • If the operation of a program can be divided into separate activities, and each activity is placed in a different function, then each could be written and checked more or less independently.
  • Separating the code into modular functions also makes the program easier to design and understand.
Types of Functions

There are two types of functions in C programming:

  1. Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
  2. User-defined functions: are the functions that are created by the user (programmer), so that he/she can use them as per their requirement. It reduces the complexity of a big program and optimizes the code.
The general syntax of a function:
return-type functionName( parameters)//function definition
{
  //Body of function
  return statement;
}
Return statement:

A function in C may or may not return a value, it depends on the function declaration that whether a return type is mentioned or not. for instance, void type does not return any value, whereas, int, float, etc returns some value on function call.

Example: here we are only printing a value but there is no return type.

void hello()
{
  printf("hello coderzpy");
}

Example: here we are calculating sum of two integers and returning the sum.

int sum_of_integers()
{
  int a=9,b=1;
   sum=a+b;
  return sum;
}
Key facts :
  • A c program is a collection of one or more functions.
  • If a c program contains only one function, it is main().
  • There is no limit on the number of function present in a c program.
  • Program execution always begins from the main() function.
  • A function can be called any number of times.
  • A function can be called from another function, but it cannot be defined by another function.
Example of a simple function:
int max(int x, int y)
{
    if (x > y)
      return x;
    else
      return y;
}
  
// main function that doesn't receive any parameter and
// returns integer.
int main()
{
    int a = 10, b = 20;
  
    // Calling above function to find max of 'a' and 'b'
    int m = max(a, b);
  
    printf("m is %d", m);
    return 0;
}
output:
m is 20

In the above code, we have created a function to calculate the max value among two integers and return it. The max function is called inside the main function and the returned value gets printed.

Note: also read about the Jump statements in C.

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago