Skip to main content

Function

src/c/basics/05-circle-area.c
#include <stdio.h>

#define PI 3.14159
// preprocessor statement: it tells the compiler to replace instances of PI with
// its defined value

int main()
{
double area = 0.0;
double radius = 0.0;
printf("Enter radius: ");
scanf("%lf", &radius);

area = PI * radius * radius;
printf("Area of circle = %lf\n", area);
}

/*=============================================================================
Test 1:
radius = 1
area = 3.14159
=============================================================================*/

Function prototype

Prototype declaration can help with code organization. We can declare function prototype in the top of the file followed by main function, and finally place the body of prototyped functions in the bottom.

src/c/basics/05-function-proto.c
#include <stdio.h>

// prototype declaration
float add(float, float);
// float add(float input1, float input2);

int main()
{
float input1, input2, sum;
printf("Enter input 1: ");
scanf("%f", &input1);
printf("Enter input 2: ");
scanf("%f", &input2);

sum = add(input1, input2);

printf("Total = %f\n", sum);

return 0;
}

// function definition can be placed after main()
float add(float input1, float input2)
{
return (input1 + input2);
}

Call by reference

We can call by reference in order to update the values in the calling environment instead of returning function values. Here is an example in C++:

src/cpp/basics/05-func-call-by-ref.cpp
// this program takes an integer input and prints number of digits in the given
// input and sum of the digits. For example, an input of 1235 would print 4 and
// 11. If we have to implement a function, we need to return two results. One of
// the ways we can achieve this by implementing a function with call by
// reference
#include <iostream>
using namespace std;

void analyze_digits(int, int &, int &);

int main()
{
int num, num_digits, sum_digits;
cout << "Enter integer input: ";
cin >> num;

analyze_digits(num, num_digits, sum_digits);

cout << "Number of digits: " << num_digits << endl;
cout << "Sum of digits: " << sum_digits << endl;

return 0;
}

void analyze_digits(int num, int &num_digits, int &sum_digits)
{
int digits = 0;
int sum = 0;

while (num > 0)
{
digits += 1;
sum += num % 10;
num = num / 10;
}

num_digits = digits;
sum_digits = sum;
}

We can achieve the same using pointer as well, however syntax could be little difficult (I think):

src/c/basics/05-using-swap-pointer.c
#include <stdio.h>

void swap(int *i, int *j)
{
int temp = *i;
*i = *j;
*j = temp;
}

int main()
{
int a = 3, b = 5;

printf("Before swap: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}

Inline function

Inline function blocks are copied to the calling environment, instead of calling environment handling over control to the function. This reduces function call overheads, suitable for functions with minimal computation.

src/cpp/basics/05-func-inline.cpp
// inline functions are just copied to the where they are called
// useful for small functions, function calls have some overhead due to stack
// calls
#include <iostream>
using namespace std;

inline int make_double(int);

int main()
{
int size = 3;
cout << make_double(size) << endl;

return 0;
}

int make_double(int size)
{
return (2 * size);
}

Default arguments

src/cpp/basics/05-func-default-args.cpp
#include <iostream>
using namespace std;

// int vol(int, int width=1, int height=1);
int vol(int, int = 1, int = 1);

int main()
{
cout << vol(1) << endl;
cout << vol(3, 2, 2) << endl;
return 0;
}

int vol(int length, int width, int height)
{
return (length * width * height);
}

Function overloading

src/cpp/basics/05-func-overloading.cpp
// multiple functions can have same name provided they have different numbers of
// arguments or different argument type. function signature not only on their
// names but also on arguments
#include <iostream>
using namespace std;

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

int add(int a, int b, int c)
{
return (a + b + c);
}

float add(float a, float b)
{
return (a + b);
}

int main()
{
int a = 2, b = 4, c = 5;
float d = 3.2, e = 4.5;

cout << a << " + " << b << " = " << add(a, b) << endl;
cout << a << " + " << b << " + " << c << " = " << add(a, b, c) << endl;
cout << d << " + " << e << " = " << add(d, e) << endl;

return 0;
}