SlideShare una empresa de Scribd logo
1 de 25
Descargar para leer sin conexión
Functions
Function Overview
 Read 3 numbers and
print followings
– max(first, second )
– max(first, third)
– max(second, third)
2
#include <stdio.h>
int main()
{
int a, b, c, m ;
scanf( “%d%d%d”, &a, &b, &c ) ;
if( a > b ) m = a ;
else m = b ;
printf( “%dn”, m ) ;
if( a > c ) m = a ;
else m = c ;
printf( “%dn”, m ) ;
if( b > c ) m = b ;
else m = c ;
printf( “%dn”, m ) ;
return 0;
}
Function Overview
 Yes, you can do!
3
#include <stdio.h>
int main(void)
{
int a, b, c, m ;
scanf( “%d%d%d”, &a, &b, &c ) ;
m = max( a, b ) ;
printf( “%dn”, m ) ;
m = max( a, c ) ;
printf( “%dn”, m ) ;
m = max( b, c ) ;
printf( “%dn”, m ) ;
return 0;
}
int max( int m, int n )
{
if( m > n )
return m ;
else
return n ;
}
function
Function Definition
 Yes, you can do!
4
#include <stdio.h>
int max( int m, int n )
{
if( m > n )
return m ;
else
return n ;
}
int main(void)
{
int a, b, c, m ;
scanf( “%d%d%d”, &a, &b, &c ) ;
m = max( a, b ) ;
printf( “%dn”, m ) ;
...
}
Function Invocation :
call ‘max()’ Function
Function Definition :
definition of Function
return value type
function name
parameter
5
Function Definition
 Return value type
– any type possible: char, int, float
– if there is no return value: void
 Function name
– any name under naming convention
 Parameters
– number of parameters: any number
– if there is no parameters: empty or void
Function Definition
 프로그램 수행 순서
1. 프로그램일 시작되면,
main() 함수가 실행된다.
2. 함수호출을 만나면,
main()함수의 실행이 멈
춘다.
3. Parameter가 함수쪽으로
복사된다.
4. 함수가 실행된다.
5. 함수에서 return을 만나
면, main()으로 돌아온다.
6. main()이 계속 수행된다.
6
#include <stdio.h>
int max( int m, int n )
{
if( m > n )
return m ;
else
return n ;
}
int main()
{
int m ;
int a = 1 ;
m = max( a, 10 ) ;
printf( “%dn”, m ) ;
}
Function Definition
 참고사항
– 함수가 또 다른 함수를 호
출할 수 있다.
– 함수가 함수를 호출했을
때 수행 순서는, main()이
함수를 호출했을 때와 유
사하다.
7
#include <stdio.h>
int max( int m, int n )
{
if( m > n )
return m ;
else
return n ;
}
int max3( int a, int b, int c )
{
int k = max( a, b ) ;
k = max( k, c ) ;
return k ;
}
int main()
{
int m ;
m = max3( 3, 4, 10 ) ;
printf( “%dn”, m ) ;
}
8
Function Definition
 The Return Statement
– Stop function execution and return value
– If there is no return value, you may use omit “return”
#include <stdio.h>
float abs(float x)
{
if(x>=0.0)
return x;
else
return –x;
}
int main(void)
{
float y = -2.4 ;
printf( “%fn”, abs(y) ) ;
return 0;
}
#include <stdio.h>
void PrintHello(void)
{
printf( “Hello, Worldn” ) ;
}
int main(void)
{
PrintHello();
return 0;
}
#include <stdio.h>
void PrintHello(void)
{
printf( “Hello, Worldn” );
return ;
printf( “Hellooooooon” );
}
int main(void)
{
PrintHello();
return 0;
}
Example
 What happens?
9
#include <stdio.h>
int Sum( int k )
{
int i, sum = 0 ;
for( i = 1 ; i <= k ; i++ )
sum += i ;
return sum ;
}
int main()
{
int i ;
for( i = 0 ; i <= 10 ; i++ )
printf( “%dn”, Sum(i) ) ;
return 0;
}
0
1
3
6
10
15
21
28
36
45
55
10
Function Prototypes
 Function Prototypes
#include <stdio.h>
double square(double x);
int main(void)
{
int y=4;
double result = square(y);
printf(“square(%d) = %fn”, y, result);
return 0;
}
double square(double x)
{
return x*x ;
}
#include <stdio.h>
double square(double x)
{
return x*x ;
}
int main(void)
{
int y=4;
double result = square(y);
printf(“square(%d) = %fn”, y, result);
return 0;
}
11
Function Prototypes
 Function Prototypes
– Declaration saying “there is a function such as …”
– Every function should be declared before it is used!!!
– Format of declaration
• return-type function_name ( parameter type list );
[Ex] double square(double);
double square(double x) ;
Recursive Problem Solving
 Sum : Iterative version
– What is the return value?
12
int sum ( int n )
{
int sum = 0, k;
for( k = 1 ; k <=n ; k++ )
sum += k;
return sum;
}
Recursive Problem Solving
 Sum : Recursive version
– What is the return value?
13
int sum ( int n )
{
if ( n == 1 )
return 1;
else
return n + sum( n – 1 );
}






 1if
1if1
1 nna
n
a
n
n
14
Recursive Problem Solving
 Sum : Recursive version
– 호출이 몇 번 일어날까?
int sum( int n ) ;
int main()
{
printf( “%dn”, sum(3) ) ;
return 0;
}
int sum ( int n )
{
if ( n == 1 )
return 1;
else
return n + sum( n – 1 ) ;
}
main
sum(3)
sum(2)
sum(1)
return 1
return 2 +1
return 3 + 3call sum(3)
call sum(2)
call sum(1)
15
Recursive Problem Solving
 Sum : Recursive version
 sum의 변수 n은 몇 개가 생성될까?
int sum( int n ) ;
int main()
{
printf( “%dn”, sum(3) ) ;
return 0;
}
int sum ( int n )
{
if ( n == 1 )
return 1;
else
return n + sum( n – 1 ) ;
}
nsum(3)
n
n
sum(3)
sum(2)
n
n
n
sum(3)
sum(2)
sum(1)
main()
16
Recursive Problem Solving
 factorial을 구하는 예제
[Ex] /* Recursive version */
int fact( int n)
{
if ( n == 1 )
return 1;
else
return n * fact ( n - 1 );
}
[Ex] /* Iterative version */
int fact( int n )
{
int result = 1;
for ( ; n > 1; --n )
result *= n;
return result;
}






 1if
1if1
1 nna
n
a
n
n
17
Recursive Problem Solving
 xn을 구하는 예제
[Ex] /* Recursive version */
int power ( int x, int n )
{
if ( n == 0 )
return 1;
else
return x * power( x, n – 1 );
}
[Ex] /* Iterative version */
int power ( int x, int n )
{
int pow = 1;
for ( ; n > 0; n--)
pow *= x;
return pow;
}
 
 





1if1,
1if
,
nxnxa
nx
nxa
18
Local variables and Global variables
 Local variables
– All variables declared in a function including parameters
– meaningful and useful only in the function body
#include <stdio.h>
void function() ;
int main(void)
{
int i = 1, j = 0;
function() ;
printf(“main : i=%d, j=%d n”, i, j);
return 0;
}
void function()
{
int j=0, k = 1;
printf(“in function : j=%d, k=%d n”, j, k);
}
Same variables
or
Different variables with the same name?
19
Local variables and Global variables
 Local variables
#include <stdio.h>
void function() ;
int main(void)
{
int i = 1, j = 0;
function() ;
printf(“main : i=%d, j=%d n”, i, j);
return 0;
}
void function()
{
int j=0, k = 1;
printf(“in function : j=%d, k=%d n”, j, k);
i = 0 ;
}
i j
main
j k
function
20
Local variables and Global variables
 Local variables
– 함수의 파라미터도 해당 함수의 지역변수이다.
#include <stdio.h>
void function(int) ;
int main(void)
{
int i = 0, j = 0;
function(i, j) ;
printf(“i=%d, j=%d n”, i, j);
return 0;
}
void function(int i, int j)
{
int k = 0 ;
i++ ; j++ ;
printf(“i=%d, j=%d, k=%d n”, i, j, k);
}
i j
main
i j
function
k
21
Local variables and Global variables
 Global variables
– All variables declared outside functions
– meaningful and useful in all program
#include <stdio.h>
int g = 0;
int main(void)
{
int i =0, j=0 ;
...
}
void function(int i)
{
int j = 3 ;
g++ ;
}
i j
main
i j
function
g
your program
22
Local variables and Global variables
 Global variables
#include <stdio.h>
void function(int i) ;
int g = 0;
int main(void)
{
int i =0, j=0 ;
function(i) ;
printf(“main : %d %d %dn”, i, j, g);
return 0;
}
void function(int i)
{
int j = 3 ;
i++ ;
printf(“in function : %d %d %dn”, i, j, g);
g++ ;
}
i j
main
i j
function
g
your program
23
Invocation and Call-by-Value
 Call-by-Value
– When you call a function, only VALUES are passed to the function
#include <stdio.h>
void function(int k)
{
k ++ ;
printf(“in function : k=%d n”, k);
}
int main(void)
{
int i = 1, j = 0;
function(i);
printf(“main : i=%d, j=%d n”, i, j);
return 0;
}
i j
main
k
function
Invocation and Call-by-Value
 Call-by-value
24
int main()
{
int i = 0, j = 1, k = 2 ;
int temp ;
printf( “%d %d %dn”, i, j, k ) ;
temp = i ;
i = j ;
j = temp ;
temp = j ;
j = k ;
k = temp ;
printf( “%d %d %dn”, i, j, k ) ;
return 0;
}
Can we replace this part with a function:
Swap(x,y)
Invocation and Call-by-Value
 Call-by-value
25
#include <stdio.h>
void Swap( int i, int j );
int main()
{
int i = 0, j = 1, k = 2 ;
printf( “%d %d %dn”, i, j, k ) ;
Swap( i, j ) ;
Swap( j, k ) ;
printf( “%d %d %dn”, i, j, k ) ;
return 0;
}
void Swap( int i, int j )
{
int temp ;
temp = i ;
i = j ;
j = temp ;
}
OK ? Why not ?
How to solve this?

Más contenido relacionado

La actualidad más candente

Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nmmohamed sikander
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloadingmohamed sikander
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]vikram mahendra
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9Rumman Ansari
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6Rumman Ansari
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2Amit Kapoor
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4Rumman Ansari
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by DivyaDivya Kumari
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]vikram mahendra
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04hassaanciit
 

La actualidad más candente (20)

lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
Functions
FunctionsFunctions
Functions
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6
 
Functions
FunctionsFunctions
Functions
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
 
Functions
FunctionsFunctions
Functions
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by Divya
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04
 

Destacado

7 mid term summary
7 mid term summary7 mid term summary
7 mid term summary웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 

Destacado (9)

7 mid term summary
7 mid term summary7 mid term summary
7 mid term summary
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
7. arrays
7. arrays7. arrays
7. arrays
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 

Similar a 6. function (20)

functions
functionsfunctions
functions
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Functions
FunctionsFunctions
Functions
 
Array Cont
Array ContArray Cont
Array Cont
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
Function in c
Function in cFunction in c
Function in c
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Function in c
Function in cFunction in c
Function in c
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Functions
FunctionsFunctions
Functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 
7 functions
7  functions7  functions
7 functions
 

Más de 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)웅식 전
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료웅식 전
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)웅식 전
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide웅식 전
 
Week12 chapter11
Week12 chapter11 Week12 chapter11
Week12 chapter11 웅식 전
 

Más de 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)
 
W14 chap13
W14 chap13W14 chap13
W14 chap13
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide
 
Week12 chapter11
Week12 chapter11 Week12 chapter11
Week12 chapter11
 

Último

Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDELiveplex
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioChristian Posta
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Brian Pichman
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 

Último (20)

Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
20230104 - machine vision
20230104 - machine vision20230104 - machine vision
20230104 - machine vision
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and Istio
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )
 
20150722 - AGV
20150722 - AGV20150722 - AGV
20150722 - AGV
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 

6. function

  • 2. Function Overview  Read 3 numbers and print followings – max(first, second ) – max(first, third) – max(second, third) 2 #include <stdio.h> int main() { int a, b, c, m ; scanf( “%d%d%d”, &a, &b, &c ) ; if( a > b ) m = a ; else m = b ; printf( “%dn”, m ) ; if( a > c ) m = a ; else m = c ; printf( “%dn”, m ) ; if( b > c ) m = b ; else m = c ; printf( “%dn”, m ) ; return 0; }
  • 3. Function Overview  Yes, you can do! 3 #include <stdio.h> int main(void) { int a, b, c, m ; scanf( “%d%d%d”, &a, &b, &c ) ; m = max( a, b ) ; printf( “%dn”, m ) ; m = max( a, c ) ; printf( “%dn”, m ) ; m = max( b, c ) ; printf( “%dn”, m ) ; return 0; } int max( int m, int n ) { if( m > n ) return m ; else return n ; } function
  • 4. Function Definition  Yes, you can do! 4 #include <stdio.h> int max( int m, int n ) { if( m > n ) return m ; else return n ; } int main(void) { int a, b, c, m ; scanf( “%d%d%d”, &a, &b, &c ) ; m = max( a, b ) ; printf( “%dn”, m ) ; ... } Function Invocation : call ‘max()’ Function Function Definition : definition of Function return value type function name parameter
  • 5. 5 Function Definition  Return value type – any type possible: char, int, float – if there is no return value: void  Function name – any name under naming convention  Parameters – number of parameters: any number – if there is no parameters: empty or void
  • 6. Function Definition  프로그램 수행 순서 1. 프로그램일 시작되면, main() 함수가 실행된다. 2. 함수호출을 만나면, main()함수의 실행이 멈 춘다. 3. Parameter가 함수쪽으로 복사된다. 4. 함수가 실행된다. 5. 함수에서 return을 만나 면, main()으로 돌아온다. 6. main()이 계속 수행된다. 6 #include <stdio.h> int max( int m, int n ) { if( m > n ) return m ; else return n ; } int main() { int m ; int a = 1 ; m = max( a, 10 ) ; printf( “%dn”, m ) ; }
  • 7. Function Definition  참고사항 – 함수가 또 다른 함수를 호 출할 수 있다. – 함수가 함수를 호출했을 때 수행 순서는, main()이 함수를 호출했을 때와 유 사하다. 7 #include <stdio.h> int max( int m, int n ) { if( m > n ) return m ; else return n ; } int max3( int a, int b, int c ) { int k = max( a, b ) ; k = max( k, c ) ; return k ; } int main() { int m ; m = max3( 3, 4, 10 ) ; printf( “%dn”, m ) ; }
  • 8. 8 Function Definition  The Return Statement – Stop function execution and return value – If there is no return value, you may use omit “return” #include <stdio.h> float abs(float x) { if(x>=0.0) return x; else return –x; } int main(void) { float y = -2.4 ; printf( “%fn”, abs(y) ) ; return 0; } #include <stdio.h> void PrintHello(void) { printf( “Hello, Worldn” ) ; } int main(void) { PrintHello(); return 0; } #include <stdio.h> void PrintHello(void) { printf( “Hello, Worldn” ); return ; printf( “Hellooooooon” ); } int main(void) { PrintHello(); return 0; }
  • 9. Example  What happens? 9 #include <stdio.h> int Sum( int k ) { int i, sum = 0 ; for( i = 1 ; i <= k ; i++ ) sum += i ; return sum ; } int main() { int i ; for( i = 0 ; i <= 10 ; i++ ) printf( “%dn”, Sum(i) ) ; return 0; } 0 1 3 6 10 15 21 28 36 45 55
  • 10. 10 Function Prototypes  Function Prototypes #include <stdio.h> double square(double x); int main(void) { int y=4; double result = square(y); printf(“square(%d) = %fn”, y, result); return 0; } double square(double x) { return x*x ; } #include <stdio.h> double square(double x) { return x*x ; } int main(void) { int y=4; double result = square(y); printf(“square(%d) = %fn”, y, result); return 0; }
  • 11. 11 Function Prototypes  Function Prototypes – Declaration saying “there is a function such as …” – Every function should be declared before it is used!!! – Format of declaration • return-type function_name ( parameter type list ); [Ex] double square(double); double square(double x) ;
  • 12. Recursive Problem Solving  Sum : Iterative version – What is the return value? 12 int sum ( int n ) { int sum = 0, k; for( k = 1 ; k <=n ; k++ ) sum += k; return sum; }
  • 13. Recursive Problem Solving  Sum : Recursive version – What is the return value? 13 int sum ( int n ) { if ( n == 1 ) return 1; else return n + sum( n – 1 ); }        1if 1if1 1 nna n a n n
  • 14. 14 Recursive Problem Solving  Sum : Recursive version – 호출이 몇 번 일어날까? int sum( int n ) ; int main() { printf( “%dn”, sum(3) ) ; return 0; } int sum ( int n ) { if ( n == 1 ) return 1; else return n + sum( n – 1 ) ; } main sum(3) sum(2) sum(1) return 1 return 2 +1 return 3 + 3call sum(3) call sum(2) call sum(1)
  • 15. 15 Recursive Problem Solving  Sum : Recursive version  sum의 변수 n은 몇 개가 생성될까? int sum( int n ) ; int main() { printf( “%dn”, sum(3) ) ; return 0; } int sum ( int n ) { if ( n == 1 ) return 1; else return n + sum( n – 1 ) ; } nsum(3) n n sum(3) sum(2) n n n sum(3) sum(2) sum(1) main()
  • 16. 16 Recursive Problem Solving  factorial을 구하는 예제 [Ex] /* Recursive version */ int fact( int n) { if ( n == 1 ) return 1; else return n * fact ( n - 1 ); } [Ex] /* Iterative version */ int fact( int n ) { int result = 1; for ( ; n > 1; --n ) result *= n; return result; }        1if 1if1 1 nna n a n n
  • 17. 17 Recursive Problem Solving  xn을 구하는 예제 [Ex] /* Recursive version */ int power ( int x, int n ) { if ( n == 0 ) return 1; else return x * power( x, n – 1 ); } [Ex] /* Iterative version */ int power ( int x, int n ) { int pow = 1; for ( ; n > 0; n--) pow *= x; return pow; }          1if1, 1if , nxnxa nx nxa
  • 18. 18 Local variables and Global variables  Local variables – All variables declared in a function including parameters – meaningful and useful only in the function body #include <stdio.h> void function() ; int main(void) { int i = 1, j = 0; function() ; printf(“main : i=%d, j=%d n”, i, j); return 0; } void function() { int j=0, k = 1; printf(“in function : j=%d, k=%d n”, j, k); } Same variables or Different variables with the same name?
  • 19. 19 Local variables and Global variables  Local variables #include <stdio.h> void function() ; int main(void) { int i = 1, j = 0; function() ; printf(“main : i=%d, j=%d n”, i, j); return 0; } void function() { int j=0, k = 1; printf(“in function : j=%d, k=%d n”, j, k); i = 0 ; } i j main j k function
  • 20. 20 Local variables and Global variables  Local variables – 함수의 파라미터도 해당 함수의 지역변수이다. #include <stdio.h> void function(int) ; int main(void) { int i = 0, j = 0; function(i, j) ; printf(“i=%d, j=%d n”, i, j); return 0; } void function(int i, int j) { int k = 0 ; i++ ; j++ ; printf(“i=%d, j=%d, k=%d n”, i, j, k); } i j main i j function k
  • 21. 21 Local variables and Global variables  Global variables – All variables declared outside functions – meaningful and useful in all program #include <stdio.h> int g = 0; int main(void) { int i =0, j=0 ; ... } void function(int i) { int j = 3 ; g++ ; } i j main i j function g your program
  • 22. 22 Local variables and Global variables  Global variables #include <stdio.h> void function(int i) ; int g = 0; int main(void) { int i =0, j=0 ; function(i) ; printf(“main : %d %d %dn”, i, j, g); return 0; } void function(int i) { int j = 3 ; i++ ; printf(“in function : %d %d %dn”, i, j, g); g++ ; } i j main i j function g your program
  • 23. 23 Invocation and Call-by-Value  Call-by-Value – When you call a function, only VALUES are passed to the function #include <stdio.h> void function(int k) { k ++ ; printf(“in function : k=%d n”, k); } int main(void) { int i = 1, j = 0; function(i); printf(“main : i=%d, j=%d n”, i, j); return 0; } i j main k function
  • 24. Invocation and Call-by-Value  Call-by-value 24 int main() { int i = 0, j = 1, k = 2 ; int temp ; printf( “%d %d %dn”, i, j, k ) ; temp = i ; i = j ; j = temp ; temp = j ; j = k ; k = temp ; printf( “%d %d %dn”, i, j, k ) ; return 0; } Can we replace this part with a function: Swap(x,y)
  • 25. Invocation and Call-by-Value  Call-by-value 25 #include <stdio.h> void Swap( int i, int j ); int main() { int i = 0, j = 1, k = 2 ; printf( “%d %d %dn”, i, j, k ) ; Swap( i, j ) ; Swap( j, k ) ; printf( “%d %d %dn”, i, j, k ) ; return 0; } void Swap( int i, int j ) { int temp ; temp = i ; i = j ; j = temp ; } OK ? Why not ? How to solve this?