Chapter 4 Functions Ece128
Chapter 4 Functions Ece128
CHAPTER 4
FUNCTIONS
Lesson Outcomes
■ To declare function if the function definition is written below the main function.
■ Otherwise, no need function prototype. (function definition above the main function)
■ Almost similar with function definition header, but must be written before the main
function with semicolon at the end of statement.
Anatomy of a program with a function
#include<stdio.h>
Function prototype (without
void function_name(); parameter)
int main() {
statements; Function call (no argument)
function_name();
return 0;
}
Function definition
void function_name() {
statements;
}
Anatomy of a program with a function
(con’td)
#include<stdio.h>
Function prototype (with
parameters pm1 and pm2)
double function_name(int pm1,double pm2);
int main() {
int p1, double p2; Function call (with arguments
statements;
p1 and p2)
function_name(p1,p2);
return 0;
}
Function definition
return_value_type function_name(parameter_list);
return_value_type function_name(parameter_list)
{
statements;
}
Specifies the type of the value returned by the function
Data types of return value : int (default), char, float, double
If function does not has any return value : void
o List of variable names received by the function from the caller –
arguments (data input)
o parameter_list : (parameter_type parameter_name)
o parameter_type: int , char, float, double
o If function does not has any parameter_list : () or (void)
Function Call
■ Local variables are variables declared within a block and only accessible within that
block only.
■ Variables declared in function main() are known only within function main() – not
accessible outside of the block
■ But variables defined in the outer block are accessible in the inner block
Local variable
Example 4:
#include <stdio.h>
int main() • Variable a is declared in main()
{ • a is the outer block variable therefore
int a=0; it is accessible within inner block
while (a <10) {
int b=1; (main block and while loop)
printf( “ %d %d \n”, a,b); • Variable b is declared within while
++a;
++b;
loop block (inner block)
} • b is accessible only inside while loop,
} not accessible in the main block
Global variable
■ Function that calls itself either directly or indirectly through another function
Recursive Function
Example 5:
#include <stdio.h>
void func(); ■ In the main() function, func() is
int main() being called once.
{
■ The function func(), in its
func();
definition calls itself. So, func()
return 0; becomes a recursive function.
}
void func() ■ Then func() would continue
{ calling itself forever.
printf("\n This is a recursive function \n"); ■ This will forever printing “This is
func(); a recursive function”
}
Exercise
■ Write a program that utilizes a function with parameters. The program reads 3 integer
numbers from the user and sends the 3 input to the function. The function finds the
smallest value between the 3 integer numbers and then return to the main function and
display the result.
Smallest value: 20