C Programming Tutorial – Functions

The C programming tutorial – Functions are another important feature in C language which helps in achieve a particular task. There is at least one function in a C program and it’s called the main () function. Without main there is no C program. You cannot delete the main function, but you can delete other user-defined functions.

There are two reasons why function exists

  1. Code reuse
  2. Better program management because programming task can be broken and distributed to many functions.

You can repeat the same function by calling instead of writing the whole code again as many times as required in your C programs.

In structured programming practice, the program is broken in to manageable tasks and later each task becomes a function with own codes. This is very easy to manage compared to writing whole code into the main () function itself.

Important parts of a C function

Three things are important about a C function

  1. Function declaration
  2. Function definition
  3. Function call

Function Declaration

You can declare function prototype using formal argument in two location in a C program.

  • Global declaration
  • Local declaration

Some declaration is done outside of main () function and those are called the globally declared variables or functions.

There is nothing special about them except that you can call them anywhere in you program or another file in the same program.

The scope of local variable is inside of main or inside of a user-defined function. It means you cannot call the function outside or main () or another user-defined function.

Syntax:

data_type function_name (
data_type variable1, 
data_type variable2, 
.
.
data_type variable n);

Example:

int addition (int a, int b);

Function definition

The function definition is written outside of the main () function and it has all the statements to perform a specific task assigned to that function. You can declare local variables other than function parameters in a function definition.

Syntax:

data_type> function_name (data_type variable1…)

{

    Statement 1;

    Statement 2;

    .

    .

    Statement n;

}

Example:

int addition (int x, int y)

{

    int result = x + y;

    return result;

}

Function Call

You can call a function anywhere you want to use it depending on the scope of the function. You can call function in three places

  • Inside another function
  • In main() program
  • Another file in the same program

Syntax:

function_name (variable_list);

Example:

addition (a, b);

Function Types

Earlier you learned about the function components inside a C program. There are different types of functions in C language. They are as follows

  1. Function without parameters
  2. Function with parameters
  3. Function with return
  4. Function without return

We will discuss each one of them in more details.

Functions without parameters

These type of functions do not take parameters to perform computation.

Syntax:

data type function_name> ( );

Example:

int addition ( );

Example Program:

#include <stdio.h>

int main ()

{

    int result = 0;

    int addition (); /* function declaration */

    result = addition (); /* function call */

    printf ("Total= %d\n", result);

    getch ();

    return 0;

}

/* function definition */

int addition ()

{

    int a = 10;

    int b = 20;

    int total = 0;

    total = a + b;

    return output;

}

Output:

Total = 30

Function with parameters

A function with parameter has two important parameter values depending on where it appears.

  1. Formal parameters
  2. Actual parameters

Formal Parameters

The formal parameter appears at the function declaration, when you tell the kind of argument is used, especially the data type.

For example

void sum (int x, int y);

and

void sum (int, int);

They are the same type of declarations.

Actual Parameters

The actual parameters are only used in function calls. The things you must know about functions with parameters are

What kind of values they take?

  1. Integer
  2. Float or double
  3. Array
  4. String of characters
  5. Pointers
  6. Memory address

Syntax:

data_type function_name ( variable1 …);

Example:

void multiply (int a, int b);

How to supply parameters for these types of functions during function call?

There is two way to do this

  1. Call-by-Value
  2. Call-by-Reference

If you want to learn the how this works – check examples of both

Examples of each input

In this section, we will discuss different calls to functions.

Integer value
Example:

int a, b;

a = b = 10;

/* function declaration */

int addition (int a, int b);

/* function call */

addition (a, b);

/* function definition */

int addition (int a, int b)

{

    do something;

}

Float or Double

Example:

float a, b;

a = b = 10.45;

/* function declaration */

float addition (int a, int b);

/* function call */

addition (a, b);

/* function definition */

float addition (float a, float b)

{

    do something;

}

Character value

Example:

char vote;

vote = ‘Y’;

/* function declaration */

void opinion (char vote);

/* function call */

opinion (vote);

/* function definition */

void opinion (char vote)

{

    do something;

}

Array value as parameter

An array is contagious memory location that hold same type of values and a subscript helps in identifying the correct location of an array element. There are four notation to access the value from an array.

  1. Array [0] – refers to value of first element of array.
  2. Array – refers to memory address of first element of an array.
  3. *(array + 1) or *(1 + array) – refers to value of second element of an array.
  4. (array + 1) or (1 + array) – refers to memory address of second element of an array.

Note: The notation array [0] is same as i [array] because it means *(array + 1) or *(i + array) which called the commutative property.

When you use array as parameter to function.

  • The name of the array ‘array’ is pointer to the first location in an array. It contains the memory address of the first element in the array.
  • The index or subscript is added to the memory address to find all elements of the array which is much more efficient that copying and passing entire array and slowing the program.

Since it is not possible to send the entire array as function argument by call-by-value method, passing a pointer to first element or passing address to first element is easy. Later any element of the array can be accessed using subscript or index value.

Syntax:

/* Function declaration */

    int sum (int arr []);

/*Function call where arr points to first element of the arr [] */

    sum (arr)

/* Function definition */

int sum (int arr []) {

    do something;

}

Pointer value as parameter to function

Pointers do not hold any value, but points to the memory address of a variable that has a value.

There are some rules regarding pointer.

  1. Pointer contains memory address of variable
  2. Pointer must point to something before you use them.
  3. Pointer is passed as parameter to a function using call-by-reference method.

Here is an example of pointer as function parameter.

Syntax:

/* Function declaration */

int sum (int *p);

int a = 10;

p = &a;

/*Function call where ‘p’ points memory address of variable ‘a’ */

sum (p);


/* Definition of the function*/

int sum (int *p);

{

    Do something;

}

Function with return

After completing a specific task some function return a single value to the main function or to a function that called it.

A function with return requires two things.

  1. A return statement using keyword – ‘return’
  2. The return type should match with the return type of function.

Syntax:

data_type function_name ( )

{

    do something;

    return variable;

}

Example

int sum (int a, int b)

{

    int sum = 0;

    sum = a + b;

    return sum;

}

Notice that the sum is an integer and returns an integer value which match with the int sum (int a, int b) function data type. If there is a mismatch you will get an error.

The main function always returns an integer by default, unless it’s specified not to return anything.

Function without return

There is a different class of functions that does not return any value. There are two requirement to define such a function.

  1. No return keyword required because function does not return any value.
  2. The function has no data type declared and instead the keyword ‘void’ is used to denote that the function does not return anything.
void sum (int a, int b)

{

int sum = 0;

sum = a + b;

printf (“The sum is %d\n”, sum);

}

Example Programs

C++ Program to Solve Quadratic Equations

C++ Program to do Interest Calculations


Bibliography

Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications.

Linden, Peter Van der. 1994. Expert C Programming: Deep C Secrets. Upper Saddle River, NJ, USA: Prentice-Hall, Inc. .

Advertisements