#10 함수2

김터넷 ㅣ 2023. 1. 23. 23:05

728x90
반응형

전달값이 없는 함수

#include <stdio.h>

// 선언

void function_without_params();


int main(void)
{

//파라미터(전달값)가 없는 함수
//function_without_params();


 void function_without_params()
 {
 printf("전달값이 없는 함수입니다.\n");
 }

 

전달값이 있는 함수

#include<stdio.h>

void function_with_params (int num1, int num2, int num3);

int main(void)

{

function_with_params(1,2,3)

return 0;
}

void function_with_params(int num1, int num2, int num3)

{
printf("전달받은 값은 %d, %d, %d 입니다.", num1, num2, num3);
}

 

전달값 바꿔보기

#include<stdio.h>

void function_with_params (int num1, int num2, int num3);

int main(void)

{

//function_with_params(1,2,3)  // 전달값을 바꿔주자

function_with_params(35, 21, 26)

return 0;
}

void function_with_params(int num1, int num2, int num3)

{
printf("전달받은 값은 %d, %d, %d 입니다.", num1, num2, num3);
}

 

전달값과 반환값이 있는 함수 - 1

#include <stdio.h>

int apple(int total, int ate);

int main(void)

{

int ret = apple(5,2);
printf("사과 5개중 2개를 먹으면 %d개가 남아요\n", ret);

return 0;
}

int apple(int total, int ate)
{
printf("전달값과 반환값이 있는 함수입니다\n");

return total - ate;
}

 

전달값과 반환값이 있는 함수 - 2 (printf문에서 한번에 해결하기)

#include <stdio.h>

int apple(int total, int ate);

int main(void)

{

int ret = apple(5,2);
printf("사과 %d개 중에 %d개를 먹으면 %d개가 남아요\n", 10, 4, apple(10, 4));  //printf내의 함수 활용

return 0;
}

int apple(int total, int ate)
{
printf("전달값과 반환값이 있는 함수 입니다.");

return total - ate;
}

반응형

'C 언어' 카테고리의 다른 글

#12 문자vs문자열  (0) 2023.01.26
#11 배열(array)  (0) 2023.01.24
#9 함수  (0) 2023.01.21
#8 UP & DOWN  (0) 2023.01.20
#7 and, or, rand, switch  (0) 2023.01.19