<< Chapter < Page Chapter >> Page >

- Khi gặp lệnh return hoặc dấu } cuối cùng trong thân hàm, chương trình sẽ thoát khỏi hàm để trở về chương trình gọi nó và thực hiện tiếp tục những câu lệnh của chương trình này.

Truyền tham số cho hàm

Mặc nhiên, việc truyền tham số cho hàm trong C là truyền theo giá trị; nghĩa là các giá trị thực (tham số thực) không bị thay đổi giá trị khi truyền cho các tham số hình thức

Ví dụ 1: Giả sử ta muốn in ra nhiều dòng, mỗi dòng 50 ký tự nào đó. Để đơn giản ta viết một hàm, nhiệm vụ của hàm này là in ra trên một dòng 50 ký tự nào đó. Hàm này có tên là InKT.

#include<stdio.h>

#include<conio.h>

void InKT(char ch)

{

int i;

for(i=1;i<=50;i++) printf(“%c”,ch);

printf(“\n”);

}

int main()

{

char c = ‘A’;

InKT(‘*’); /* In ra 50 dau * */

InKT(‘+’);

InKT(c);

return 0;

}

Lưu ý:

- Trong hàm InKT ở trên, biến ch gọi là tham số hình thức được truyền bằng giá trị (gọi là tham trị của hàm). Các tham trị của hàm coi như là một biến cục bộ trong hàm và chúng được sử dụng như là dữ liệu đầu vào của hàm.

- Khi chương trình con được gọi để thi hành, tham trị được cấp ô nhớ và nhận giá trị là bản sao giá trị của tham số thực. Do đó, mặc dù tham trị cũng là biến, nhưng việc thay đổi giá trị của chúng không có ý nghĩa gì đối với bên ngoài hàm, không ảnh hưởng đến chương trình chính, nghĩa là không làm ảnh hưởng đến tham số thực tương ứng.

Ví dụ 2: Ta xét chương trình sau đây:

#include<stdio.h>

#include<conio.h>

int hoanvi(int a, int b)

{

int t;

t=a;/*Đoạn này hoán vị giá trị của 2 biến a, b*/

a=b;

b=t;

printf("\Ben trong ham a=%d , b=%d",a,b);

return 0;

}

int main()

{

int a, b;

clrscr();

printf("\n Nhap vao 2 so nguyen a, b:");

scanf("%d%d",&a,&b);

printf("\n Truoc khi goi ham hoan vi a=%d ,b=%d",a,b);

hoanvi(a,b);

printf("\n Sau khi goi ham hoan vi a=%d ,b=%d",a,b);

getch();

return 0;

}

Kết quả thực hiện chương trình:

***SORRY, THIS MEDIA TYPE IS NOT SUPPORTED.***

Giải thích:

- Nhập vào 2 số 6 và 5 (a=6, b=5)

- Trước khi gọi hàm hoán vị thì a=6, b=5

- Bên trong hàm hoán vị a=5, b=6

- Khi ra khỏi hàm hoán vị thì a=6, b=5

* lưu ý

Trong đoạn chương trình trên, nếu ta muốn sau khi kết thúc chương trình con giá trị của a, b thay đổi thì ta phải đặt tham số hình thức là các con trỏ, còn tham số thực tế là địa chỉ của các biến.

Lúc này mọi sự thay đổi trên vùng nhớ được quản lý bởi con trỏ là các tham số hình thức của hàm thì sẽ ảnh hưởng đến vùng nhớ đang được quản lý bởi tham số thực tế tương ứng (cần để ý rằng vùng nhớ này chính là các biến ta cần thay đổi giá trị).

Người ta thường áp dụng cách này đối với các dữ liệu đầu ra của hàm.

Ví dụ: Xét chương trình sau đây:

#include<stdio.h>

#include<conio.h>

long hoanvi(long *a, long *b)

/* Khai báo tham số hình thức *a, *b là các con trỏ kiểu long */

{

long t;

t=*a;/*gán nội dung của x cho t*/

*a=*b;/*Gán nội dung của b cho a*/

*b=t;/*Gán nội dung của t cho b*/

printf("\n Ben trong ham a=%ld , b=%ld",*a,*b);

/*In ra nội dung của a, b*/

return 0;

}

int main()

{

long a, b;

clrscr();

printf("\n Nhap vao 2 so nguyen a, b:");

scanf("%ld%ld",&a,&b);

printf("\n Truoc khi goi ham hoan vi a=%ld ,b=%ld",a,b);

hoanvi(&a,&b); /* Phải là địa chỉ của a và b */

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Ngôn ngữ lập trình. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10783/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Ngôn ngữ lập trình' conversation and receive update notifications?

Ask