13 Function
13 Function
13 Function
A function is a subroutine that contains one or more statements and performs a single well-defined task.
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.
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.
Write a program that accepts two numbers and computes and prints their sum.
int main(void){
//input
scanf("%d%d",&num1,&num2);
//process
//output
printf("%d",sum);
Elements of 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:
Example:
Note:
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:
Example:
int sum;
return sum;
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){
printf("%d",sum);
Note:
Argument – is the value that is passed to the function at the time that it is called.
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.
#include<stdio.h>
int sum;
return sum;
int main(void){
sum = computeSum(num1,num2);
printf("%d",sum);
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 main(void){
int num1,num2,ans=0;
char op;
op=getchar();
scanf("%d%d",&num1,&num2);
switch(op){
printf("%d",ans);
}
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>
return km * 0.62137;
int main(){
printf("%.2f",toMiles(2));
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>
int main(){
scanf("%d",&ft);
scanf("%d",&inch);
printf("%.2f",toCm(ft,inch));