Function Fundamentals

Loop
4 min readNov 22, 2023

--

Function in C is defined as set of statements that perform some specific task on calling it. It provides modularity and code re-usability. { } braces are used to enclose the programming statements of a function. It is also the basic building block of a C program.

Source

Hence, the compiler is instructed by a statement (function call) in which function name and parameters are used.

Syntax of function is divided into 3 parts:-

  1. Function declaration
  2. Function definition
  3. Function calling
Source

How does C function work?

This working can be divided in the following steps-

  1. Declaring function: Here, we declare a function. Also, we define it’s return types and parameters.
  2. Defining function: Here, actual work or purpose of function is defined and it also includes logic of the program.
  3. Calling function: Here, we call that functions by passing arguments in it.
  4. Executing function: Here, we can run all the statements inside function to get final result.
  5. Returning value: Here, the calculated value is returned after execution of the function.
  6. Exiting function: It is the final step where all allocated memory, functions,etc is destroyed before the full control is given to main function.
Source

Note: C function returns only one value. We have to use pointers or structures to return multiple values.

· In C, functions may or might not return values to the calling functions
· Hence, there are 4 types of functions:

  1. Function that has no arguments and no return value
  2. Function that has no arguments but has return value.
  3. Function that has argument but has no return value.
  4. Function that has arguments and return value.
#include <stdio.h>

void average(int, int, int, int, int);

int main() {
int p, q, r, s, t;

printf("\nCalculating average of five numbers:");
printf("\nEnter 5 numbers: ");
scanf("%d %d %d %d %d", &p, &q, &r, &s, &t);

average(p, q, r, s, t);

return 0;
}

void average(int p, int q, int r, int s, int t) {
float avg;
avg = (p + q + r + s + t) / 5.0;
printf("Calculated average is: %f", avg);
}

/*
Output:
Calculating the average of five numbers:
Enter 5 numbers: 50 60 70 80 90
Calculated average is: 70.000000
*/
Source

User-defined Functions:-

These are also called as “tailor-made functions”. Functions that the programmer creates and are improved and modified according to the need of that programmer are called user defined functions.

Library Functions:-

These are also called “built-in functions”. These are the functions that are already present in a compiler package.
All C standard library functions are defined inside different header files and these header files are saved with the extension .h

Passing arguments to C function can be done in 2 ways:

  1. Pass by Value:
    Here, values are copied from actual parameters into formal function parameters
#include<stdio.h>

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

// Driver code
int main() {
int a = 4, b = 5;

printf("Before swapping, value of a and b is: %d, %d\n", a, b);
swap(a, b);
printf("After swapping, value of a and b is: %d, %d", a, b);

return 0;
}

/*
Output:
Before swapping, value of a and b is: 4, 5
After swapping, value of a and b is: 4, 5
*/

2. Pass by Reference:
Here, same locations are referred by the caller’s actual parameters and the function’s actual parameters.

#include<stdio.h>

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

// Driver code
int main() {
int a = 4, b = 5;

printf("Before swapping, value of a and b is: %d, %d\n", a, b);
swap(&a, &b);
printf("After swapping, value of a and b is: %d, %d", a, b);

return 0;
}

/*
Output:
Before swapping, value of a and b is: 4, 5
After swapping, value of a and b is: 5, 4
*/

Summary:-

  1. Function can be reused as many times as we want inside a program.
  2. We need to call a function to use it.
  3. function_name, return type and parameters are included in function declaration.
  4. Body of the function is included in function definition.
  5. Function has 2 types- User-defined and library functions.
  6. According to the values passed, two types are- call by value and call by reference.

--

--

Loop
Loop

Written by Loop

Competitive coding club at Cummins College of Engineering, Pune

No responses yet