C106: Functions

Functions is a subprogram that contains a group of related statements that performs certain task when called. Each function has its own name. In a c program, function is is followed by parentheses (). Without the parentheses, one might think it is a variable, because they follow same set of rules.

A function may take parameters as an input. these inputs are processed by the statements and returns an output. Lets look at general syntax for a function.

returnType functionName(parameter_list){
  //code block including the return statements.
}

First we write returnType. the return type can be int, float, double, char or void. Void means the function don't return value.

Next we write functionName. Function name is written lowercase letter, cannot have space, cannot start with digit, must be unique meaning you cannot existing function name. If the name consist of more than one word, first letter subsequent words are capitalised (example displayTotalMarks).

parameter_list looks similar to variable declaration but its not. its just a list of parameters that the function can take. list might be empty, or void. It may contain only one parameter. Or contains any number of parameters. Lets minimise the parameter to 5 so that we dont make mistakes.

following example shows function named displayTotalMarks. As the name suggest, the function display the total up mark on screen when called.

void displayTotalMarks(float total){
  printf("Total marks : %f\n", total);
}

this function does not return value, because it is written void as return type. it take one parameter called total. the parameter type is float because the total value might contain decimal point. in the function body, there is only one statement which is to display an output on the screen.

Now let's look at second example:

float addTwoValues(float num1, float num2){
  float total;
  total = num1 + num2;
  return total;
}

this function does return a value, because it is written float as return type. it take two float parameter called num1 and num2.

In the body of the function first line is variable declaration, this variable is used temporarily to store calculated value. Next, an arithmetic operation that performs addition operation on num1 and num2 and the result is assigned to total. Finally, the stored value in total is returned using the return statement.

Calling these function in main function

int main(){
  float total;
  total = addTwoValues(12.4, 14.1);
  displayTotalMarks(total);
  return 0;
}

now in the main function, use the function name to call the function. When a function is called, parameters are also passed to the function.

first line is variable declaration, where we declare a float-point variable called total. This variable is used to store a value returned from addTwoValues() function. Remember that, in this function there is also variable called total. Both the variable are local within within each function. we pass two decimal point values to the function, after it performs its task the function return the value and stored in total.

next we call displayTotalMarks() function and pass the total as its parameter. the function takes this parameter do its task and return back to the main function. Notice that when we call a function that return void we don't use assignment operation to store its return value.

Normally, functions need to be written before the main function. This is to make sure compiler recognise them and know their return type and parameters before being called in main function.


int a()
  // function a body
}
int b()
  // function a body
}
int main(){
  // function main body
}


Some times, for more readable program, we write the main function first and then followed by the other functions. In this case prototypes is a compulsory. Prototype consist the information we get from function declaration, the first line of the function.

// function prototypes
int a();
int b();

int main(){
  // function main body
}

int a()
  // function a body
}
int b()
  // function a body
}

so our program is written as:

#include  <stdio.h>

// function prototypes
float addTwoValues(float num1, float num2);
void displayTotalMarks(float total);

int main(){
  float total;
  total = addTwoValues(12.4, 14.1);
  displayTotalMarks(total);
  return 0;
}

// add two numbers
float addTwoValues(float num1, float num2){
  float total;
  total = num1 + num2;
  return total;
}

// display string with total
void displayTotalMarks(float total){
  printf("Total marks : %f\n", total);
}

Output of this program is:

Total marks : 26.5

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net