Quick Learnology

Functions

Introduction:

A function in C is a block of code that performs a specific task and can be called from other parts of the program. Functions allow for modular and organized code, making it easier to debug, maintain, and reuse code.

Passing Values between Functions:

Values can be passed between functions in C using function arguments. Arguments are specified in the function declaration and passed to the function when it is called.

For example:

#include <stdio.h>

void add(int a, int b) {
   int sum = a + b;
   printf("The sum is: %d", sum);
}

int main() {
   int x = 10, y = 20;
   add(x, y);
   return 0;
}

Here, the values of x and y are passed to the function add as arguments.

Using Library Functions:

C has a rich standard library that provides various functions for tasks such as input/output, string manipulation, and mathematical operations. These functions can be used in your program by including the relevant header files and calling the functions. For example, to use the printf function, the stdio.h header file must be included.

Return Type of Functions:

A function can return a value back to the calling function using the return statement. The return type of the function is specified in the function declaration and specifies the type of value that the function will return.

For example :

#include <stdio.h>

int sum(int a, int b) {
   return a + b;
}

int main() {
   int x = 10, y = 20, result;
   result = sum(x, y);
   printf("The sum is: %d", result);
   return 0;
}

Storage Classes:

C has four storage classes: auto, register, static, and extern. The storage class of a variable determines its scope, life, and visibility in the program.

  • auto is the default storage class and is used for variables declared within a function.
  • register is used for variables that are used frequently and need to be stored in a register for quick access.
  • static is used for variables that retain their value between function calls.
  • extern is used for variables that are declared in one file and used in another file.

Leave a Comment

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