13 Function

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

FUNCTION

A function is a subroutine that contains one or more statements and performs a single well-defined task.

Two Types of Function:

1. Pre-defined – a function written by for us (by some other programmers).

Examples: printf(), pow(), sqrt()

2. User-defined – the function that we are going to write/implement by ourselves.

Before we start writing user-defined functions, let’s have the difference first between the main program
and the function. Note that the main program is actually the main function. The main function is where
program execution begins, that’s why we call it the main program.

A function is a subprogram therefore the structure of a function is similar to the main program.

Main Program Function

Input Output Input Output

scanf printf parameter return

A program has the life cycle input->process->output. Since a function is a subprogram, it has the same life
cycle with a program. Both have the same process. The main program uses pre-defined input and output
functions, while the function has parameter as input and a return statement for output.

Here is a sample problem for a program:

Write a program that accepts two numbers and computes and prints their sum.

Here is the implementation:

int main(void){

int num1, num2, sum;

//input

scanf("%d%d",&num1,&num2);

//process

sum = num1 + num2;

//output
printf("%d",sum);

Elements of a Function

Here is the problem for a function:

Write a function that accepts two numbers and computes and returns their sum.

Before we write the function, here are the three elements we need to define:

1. Function declaration/prototype
2. Function definition/implementation
3. Function call

The following are the syntaxes of the elements with the code segments to answer the revised problem.

Function Declaration/Prototype

Function declaration is first read by the compiler. When a function is called, it is checked whether the
name exists and whether the number of parameters and data types are correct based on how it is
declared.

Syntax:

ReturnType FunctionName(parameter list);

Example:

int computeSum(int num1, int num2);

Note:

FunctionName – is an identifier. It names the function.

ReturnType – is any standard data type in C of the value to be returned by the function.

parameter list – consists of variables that receive the value of the arguments used in the function
call. The variable name is optional.

Function Definition/Implementation

Function definition is composed of statements that perform the task asked for the function. The
parameter serves as input to the function. It means that when a function is called, the value of the
parameter passed becomes the value of the parameter of the called function. Parameter passing is done
by position. If there is a return type, the function is expected to return a value.

Syntax:

ReturnType FunctionName(parameter list){


Statement/s

Example:

int computeSum(int num1, int num2){

int sum;

sum = num1 + num2;

return sum;

The return keyword has two important uses:

1. It can be used to return a value.

2. It can be used to cause an immediate exit from the function it is in. That is, the return will cause
program execution to return to the calling as soon as it is encountered.

Note: The return statement can also be used without any value associated with it.

Function Call

Function call invokes a function. A function only exists when it is called. As mentioned above, parameters
are passed by position. When a function returns a value, the calling function either receives the value by
assigning it to a variable or passes it to another function.

int main(void){

int num1=5, num2=10, sum;

sum = computeSum(num1,num2); //received by a variable

printf("%d",sum);

printf(“%d”, computeSum(num1,num2)); //passed to another function

Note:

Argument – is the value that is passed to the function at the time that it is called.

Additional Things to Remember in Using Functions

Here are additional things to remember in using functions:

1. If there are parameters, there should be no pre-defined input functions inside the function.
2. If there is a return type, the computed value is expected to be returned, not to be displayed. It
could destroy the format defined in main.
3. If the function does not need an input, use void or just leave the parameter empty. This can only
be the time to have a pre-defined input function, and only when it is an input function.
4. If the function does not return a value, use void. This can only be the time to have a pre-defined
output function, and only when it is an output function.

Sample Program Using a Function

#include<stdio.h>

int computeSum(int num1, int num2);

int computeSum(int num1, int num2){

int sum;

sum = num1 + num2;

return sum;

int main(void){

int num1=5, num2=10, sum;

sum = computeSum(num1,num2);

printf("%d",sum);

Sample Program with Multiple Functions

Problem: Write a program that performs the four mathematical operations, addition, subtraction,
multiplication, division (one function each operation). The main program lets you choose what operation
to perform using the switch statement. The following are the case labels (+,-,*,/). If the operation chosen
is subtraction, the minuend should always be greater than the subtrahend to avoid a negative answer. If
the operation chosen is division, the dividend should be greater than the divisor.

#include<stdio.h>

int computeSum(int num1, int num2);

int computeDifference(int num1, int num2);

int computeProduct(int num1, int num2);

int computeQuotient(int num1, int num2);


int computeSum(int num1, int num2){

return num1 + num2;

int computeDifference(int num1, int num2){

return num1 - num2;

int computeProduct(int num1, int num2){

return num1 * num2;

int computeQuotient(int num1, int num2){

return num1 / num2;

int main(void){

int num1,num2,ans=0;

char op;

op=getchar();

scanf("%d%d",&num1,&num2);

switch(op){

case '+': ans=computeSum(num1,num2); break;

case '-': ans=computeDifference(num1,num2); break;

case '*': ans=computeProduct(num1,num2); break;

case '/': ans=computeQuotient(num1,num2); break;

default: printf("Invalid operator!");

printf("%d",ans);
}

Sample 1 for Function:

Write a function accepts a distance expressed in kilometers and returns the distance expressed in miles,
where 1 kilometer equals 0.62137 miles. Test the function inside main().

#include<stdio.h>

float toMiles(float km);

float toMiles(float km){

return km * 0.62137;

int main(){

printf("%.2f",toMiles(2));

Sample 2 for Function:

There are 12 inches in a foot and an inch is 2.54 centimeters long. Write a function that accepts distance
expressed in feet and inches and output the equivalent distance in centimeters. Test the function inside
main().

#include<stdio.h>

float toCm(int ft, int inch);

float toCm(int ft, int inch){

return ((ft*12) + inch) * 2.54;

int main(){

int ft, inch;

scanf("%d",&ft);

scanf("%d",&inch);

printf("%.2f",toCm(ft,inch));

You might also like