값에 의한 전달 또는 값에 의한 호출은 인수로 전송됩니다.
알고리즘
Call by Value 알고리즘을 참고하세요.
Step 1: Enter any 2 numbers at runtime Step 2: Read those two numbers from console Step 3: Call the function swap with arguments is a call by value Step 4: Go to called function swap(int a,int b) Step 5: Print the numbers after swap
예시
다음은 값에 의한 호출을 위한 C 프로그램입니다 -
#include<stdio.h>
void main(){
void swap(int,int);
int a,b;
clrscr();
printf("enter 2 numbers");
scanf("%d%d",&a,&b);
printf("Before swapping a=%d b=%d",a,b);
swap(a,b);
printf("after swapping a=%d, b=%d",a,b);
getch();
}
void swap(int a,int b){
int t;
t=a;
a=b;
b=t;
} 출력
위의 프로그램이 실행되면 다음과 같은 결과가 생성됩니다 -
enter 2 numbers 10 20 Before swapping a=10 b=20 After swapping a=10 b=20