0% found this document useful (0 votes)
22 views

Chapter 4 Functions Ece128

Uploaded by

zafransyakirin11
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Chapter 4 Functions Ece128

Uploaded by

zafransyakirin11
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

ECE128

CHAPTER 4
FUNCTIONS
Lesson Outcomes

At the end of the lesson, students should be able to


describe and explain:
 Function Prototype
 Function Definition
 Calling Function
 Local and Global Variables
 Recursive Function
Introduction

■ Functions are Mini-programs


■ Break a large program into smaller sets of statements according to their tasks.
■ Functions can be categorized into:
1) Built-in Functions ( example: printf( ), scanf( ) )
2) User-defined functions (designed by user based on user needs)
Execution of Functions
3 Elements of Function

1. Function prototype / declaration


2. Function definition
3. Calling function
Function prototype/declaration

■ 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

double function_name(int pm1,double pm2) {


statements;
}
Example 1
#include <stdio.h>
int sum( int x, int y);
int main()
{ Function prototype
int x,y,result;
printf("Enter two integers: \n");
scanf("%d %d",&x,&y);
result=sum(x,y);
printf(" Sum = %d\n",result); Function call
return 0;
}
int sum( int x, int y)
{
int sum1;
sum1= x+y;
return sum; Function definition
}
1. Function Prototype/declaration

return_value_type function_name(parameter_list);

 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)
o parameter_type: int , char, float, double
o If function does not has any parameter_list : () or (void)
2. Function Definition

■ Defines the operation to be performed when a function is called.


Function Definition

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

■ Two ways to invoke/call a function


– Call-by-value
– Call-by-reference.
Pass/Call-by-value VS Pass/Call-by-
reference
Pass/Call-by-value Pass/Call-by-reference
• Copies of the arguments’ value • References of the arguments’
are made and passed to the called value are passed to the called
function function
• Changes to the copy do not • Called function can modify the
affect the original variable’s original variable’s value
value
This method copies the actual value of an argument into This method copies the address of an argument into the
the formal parameter of the function. In this case, formal parameter. Inside the function, the address is used
changes made to the parameter inside the function have no to access the actual argument used in the call. This means
effect on the argument. that changes made to the parameter affect the argument.
#include<stdio.h>
void func_1( int num1);
main()
Example 2:
{ Function passed/called by value
int num1=5;
printf(“Call-by-value:\n”);
printf(“Before func_1 is called\
n”); Output:
printf(“num1 = %d\n\n”, num1); Call-by-value:
func_1(num1); Before func_1 is From main function
printf(“After func_1 is called\n”); called
printf(“num1 = %d\n\n”, num1); num1 = 5
In func_1: From function func_1
return 0;
} num1=10
After func_1 is called From main function
void func_1(int num1)
num1=5
{
num1=10;
printf(“In func_1:\n”);
printf(“num1 = %d\n\n”, num1);
}
#include<stdio.h>
int power(int base,int n);
int main() Example 3:
{
int base,n,res;
Function passed/called by value
printf(“Enter base and power: “);
scanf(“%d %d, &base, &n);
res=power(b,n); Output:
printf(“The result is %d.\n”,res);
return 0; Enter base and power: 5 4
} The result is 625
int power(int base, int n)
{ base n
int i,p=1; Execution inside function power:
for(i=0; i<n; i++)
p=p*base; i=0 p=1x5 =5
return p; i=1 p= 5x5 = 25
} i=2 p= 25 x 5=125
i=3 p=125 x 5 = 625
#include <stdio.h> int main () {
/* local variable definition */ Example 4: Function passed/called
int a = 100; int b = 200; by reference
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
/* calling a function to swap the values */
swap(&a, &b);
printf("After swap, value of a : %d\n", a ); Output
printf("After swap, value of b : %d\n", b ); Before swap, value of a : 100
return 0; Before swap, value of b :
} 200 After swap, value of a :
void swap(int *x, int *y) 200 After swap, value of b :
{ 100
int temp;
temp = *x; /* save the value of x */
*x = *y; /* put y into x */
*y = temp; /* put temp into y */
return;
}
Local Variable & Global Variable
Local Variable

■ 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

■ Variables declared outside any function is called global variables


■ Global variables are variables that can be accessed in any function
■ Normally they are declared before function main()
Global variable
Example 4:
#include<stdio.h>
int func(int n);
int num2;
int main()
num2 is a global variable (declared
{
int num1,res; outside any functions)
printf(“Enter two integers: “);
scanf(“%d %d”, &num1, &num2);
res=func(num1);
printf(“%d\n”,num2);
return 0;
}
int func(int n)
{
int R; num2 is accessible in func()
R=n + num2 *2;
return R;
}
Recursive function

■ 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.

Enter three integer numbers:


30
80
20

Smallest value: 20

You might also like