coderz.py

Keep Coding Keep Cheering!

Functions in C

Matrix multiplication

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement