Hướng dẫn non value-returning function in python - hàm không trả về giá trị trong python

Trong một bài học trước (2.1 - Giới thiệu về các chức năng), chúng tôi đã chỉ ra rằng cú pháp cho một định nghĩa hàm trông như thế này:

return-type identifier() // identifier replaced with the name of your function
{
// Your code here
}

Mặc dù chúng tôi đã chỉ ra các ví dụ về các chức năng có kiểu trả lại

#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
2, chúng tôi đã không thảo luận về điều này có nghĩa là gì. Trong bài học này, chúng tôi sẽ khám phá các chức năng với loại trả về
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
2.

Void return giá trị

Các chức năng không bắt buộc phải trả lại giá trị trở lại người gọi. Để nói với trình biên dịch rằng một hàm không trả về một giá trị, một loại void trả về được sử dụng. Ví dụ:void is used. For example:

#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}

Trong ví dụ trên, chức năng

#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
4 có một hành vi hữu ích (nó in ra Hi Hi) nhưng nó không cần phải trả lại bất cứ điều gì trở lại cho người gọi. Do đó,
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
4 được cung cấp một loại trả lại
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
2.

Khi

#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
7 gọi
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
4, mã trong
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
4 được thực thi và in Hi Hi được in. Vào cuối
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
4, kiểm soát trở lại
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
7 và chương trình tiến hành.

Một hàm không trả về giá trị được gọi là hàm trả về không giá trị (hoặc hàm void).non-value returning function (or a void function).

Hàm vô hiệu don don cần một tuyên bố trả lại

Một hàm void sẽ tự động quay lại người gọi ở cuối hàm. Không có tuyên bố hoàn trả là bắt buộc.

Một câu lệnh trả về (không có giá trị trả về) có thể được sử dụng trong hàm void - một câu lệnh như vậy sẽ khiến hàm quay lại người gọi tại điểm thực hiện câu lệnh trả về. Đây là điều tương tự xảy ra ở cuối chức năng. Do đó, việc đặt một câu lệnh hoàn trả trống ở cuối hàm void là dự phòng:

#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    return; // tell compiler to return to the caller -- this is redundant since the return will happen at the end of the function anyway!
} // function will return to caller here

int main()
{
    printHi();

    return 0;
}

Thực hành tốt nhất

Không đặt câu lệnh trả lại ở cuối hàm trả lại không giá trị.

Các chức năng void có thể được sử dụng trong biểu thức yêu cầu một giá trị

Một số loại biểu thức yêu cầu giá trị. Ví dụ:

#include 

int main()
{
    std::cout << 5; // ok: 5 is a literal value that we're sending to the console to be printed
    std::cout << ;  // compile error: no value provided

    return 0;
}

Trong chương trình trên, giá trị được in cần được cung cấp ở phía bên phải của

#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    return; // tell compiler to return to the caller -- this is redundant since the return will happen at the end of the function anyway!
} // function will return to caller here

int main()
{
    printHi();

    return 0;
}
2. Nếu không có giá trị nào được cung cấp, trình biên dịch sẽ tạo ra lỗi cú pháp. Vì cuộc gọi thứ hai đến
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    return; // tell compiler to return to the caller -- this is redundant since the return will happen at the end of the function anyway!
} // function will return to caller here

int main()
{
    printHi();

    return 0;
}
3 không cung cấp giá trị được in, điều này gây ra lỗi.

Bây giờ hãy xem xét chương trình sau:

#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    std::cout << printHi(); // compile error

    return 0;
}

Cuộc gọi đầu tiên đến

#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    return; // tell compiler to return to the caller -- this is redundant since the return will happen at the end of the function anyway!
} // function will return to caller here

int main()
{
    printHi();

    return 0;
}
4 được gọi trong bối cảnh không yêu cầu giá trị. Vì hàm không trả về một giá trị, điều này là tốt.

Lời kêu gọi chức năng thứ hai để chức năng

#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    return; // tell compiler to return to the caller -- this is redundant since the return will happen at the end of the function anyway!
} // function will return to caller here

int main()
{
    printHi();

    return 0;
}
4 won thậm chí còn biên dịch. Hàm
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
4 có loại trả về
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
2, có nghĩa là nó không trả về một giá trị. Tuy nhiên, tuyên bố này đang cố gắng gửi giá trị trả lại của
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
4 đến
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    return; // tell compiler to return to the caller -- this is redundant since the return will happen at the end of the function anyway!
} // function will return to caller here

int main()
{
    printHi();

    return 0;
}
3 để được in.
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    return; // tell compiler to return to the caller -- this is redundant since the return will happen at the end of the function anyway!
} // function will return to caller here

int main()
{
    printHi();

    return 0;
}
3 không biết cách xử lý việc này (giá trị nào sẽ xuất hiện?). Do đó, trình biên dịch sẽ gắn cờ này là một lỗi. Bạn cần phải nhận xét dòng mã này để tạo mã mã của bạn.

Mẹo

Một số tuyên bố yêu cầu các giá trị phải được cung cấp, và một số khác.

Khi chúng ta gọi một hàm của chính nó (ví dụ:

#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    return; // tell compiler to return to the caller -- this is redundant since the return will happen at the end of the function anyway!
} // function will return to caller here

int main()
{
    printHi();

    return 0;
}
4 đầu tiên trong ví dụ trên), chúng ta sẽ gọi một hàm cho hành vi của nó, chứ không phải giá trị trả về của nó. Trong trường hợp này, chúng ta có thể gọi hàm trả về không giá trị hoặc chúng ta có thể gọi hàm trả lại giá trị và chỉ bỏ qua giá trị trả về.

Khi chúng ta gọi một hàm trong bối cảnh yêu cầu một giá trị (ví dụ:

#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    return; // tell compiler to return to the caller -- this is redundant since the return will happen at the end of the function anyway!
} // function will return to caller here

int main()
{
    printHi();

    return 0;
}
3), phải cung cấp giá trị. Trong bối cảnh như vậy, chúng ta chỉ có thể gọi các hàm trả lại giá trị.

#include 

// Function that does not return a value
void returnNothing()
{
}

// Function that returns a value
int returnFive()
{
    return 5;
}

int main()
{
    // When calling a function by itself, no value is required
    returnNothing(); // ok: we can call a function that does not return a value
    returnFive();    // ok: we can call a function that returns a value, and ignore that return value

    // When calling a function in a context that requires a value (like std::cout)
    std::cout << returnFive();    // ok: we can call a function that returns a value, and the value will be used
    std::cout << returnNothing(); // compile error: we can't call a function that returns void in this context

    return 0;
}

Trả về giá trị từ hàm void là lỗi biên dịch

Cố gắng trả về giá trị từ hàm trả về không giá trị sẽ dẫn đến lỗi biên dịch:

void printHi() // This function is non-value returning
{
    std::cout << "In printHi()" << '\n';

    return 5; // compile error: we're trying to return a value
}

Trở lại sớm

Một câu lệnh trả về không phải là câu lệnh cuối cùng trong một hàm được gọi là lợi nhuận sớm. Một câu lệnh như vậy sẽ khiến hàm quay lại người gọi khi câu lệnh trả về được thực thi (trước khi hàm sẽ quay lại người gọi, do đó, sớm sớm).early return. Such a statement will cause the function to return to the caller when the return statement is executed (before the function would otherwise return to the caller, hence, “early”).

#include 

void print() // note: void return type
{
    std::cout << "A";

    return; // the function will return to the caller here (note: no return value)

    std::cout << "B"; // this will never be printed
}

int main()
{
    print();

    return 0;
}

Trong ví dụ trên, khi

#include 

int main()
{
    std::cout << 5; // ok: 5 is a literal value that we're sending to the console to be printed
    std::cout << ;  // compile error: no value provided

    return 0;
}
3 thực hiện, trước tiên nó sẽ in ra một A. Sau đó, câu lệnh trả về thực thi và điều khiển quay lại cho người gọi (
#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
7). Một số người không bao giờ được in vì tuyên bố không bao giờ thực thi.

Như một bên…

Khi biên dịch ví dụ trên, trình biên dịch của bạn có thể đưa ra cảnh báo về dòng 9 không thể truy cập được. Nếu bạn đang coi cảnh báo là lỗi, bạn có thể cần phải vô hiệu hóa cài đặt đó để biên dịch ví dụ.

Lợi nhuận sớm cũng có thể được sử dụng trong các hàm trả lại giá trị quá:

#include 

int print() // note: return type of int
{
    std::cout << "A";

    return 5; // the function will return to the caller here

    std::cout << "B"; // this will never be printed
}

int main()
{
    std::cout << print(); // print() returns value 5, which will be print to the console

    return 0;
}

Chương trình trên in như sau:

A5

Đầu tiên,

#include 

int main()
{
    std::cout << 5; // ok: 5 is a literal value that we're sending to the console to be printed
    std::cout << ;  // compile error: no value provided

    return 0;
}
3 được gọi. Tuyên bố đầu tiên trong
#include 

int main()
{
    std::cout << 5; // ok: 5 is a literal value that we're sending to the console to be printed
    std::cout << ;  // compile error: no value provided

    return 0;
}
3 in trên A A. Sau đó, câu lệnh trả về được thực thi, trả lại giá trị của
#include 

int main()
{
    std::cout << 5; // ok: 5 is a literal value that we're sending to the console to be printed
    std::cout << ;  // compile error: no value provided

    return 0;
}
7 trở lại cho người gọi. Giá trị trả lại này cũng được in. Tuyên bố
#include 

int main()
{
    std::cout << 5; // ok: 5 is a literal value that we're sending to the console to be printed
    std::cout << ;  // compile error: no value provided

    return 0;
}
8 không bao giờ được thực thi vì hàm đã trả lại cho người gọi trước thời điểm đó.

Trong lịch sử, sự trở lại sớm đã được cau mày. Tuy nhiên, trong lập trình hiện đại, chúng được chấp nhận nhiều hơn, đặc biệt khi chúng có thể được sử dụng để làm cho một hàm đơn giản hơn hoặc được sử dụng để hủy bỏ một chức năng sớm do một số điều kiện lỗi.

Thời gian đố

Câu hỏi 1

Kiểm tra các chương trình sau và nêu rõ những gì chúng xuất ra, hoặc liệu chúng sẽ không biên dịch.

1a)

#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
0

Hiển thị giải pháp

1b)

#include 

// void means the function does not return a value to the caller
void printHi()
{
    std::cout << "Hi" << '\n';

    // This function does not return a value so no return statement is needed
}

int main()
{
    printHi(); // okay: function printHi() is called, no value is returned

    return 0;
}
1

Hiển thị giải pháp

Câu hỏi 2

Sự trở lại sớm là gì, và hành vi của nó là gì?

Hiển thị giải pháp

Hàm không trả lại là gì?

Hàm "không trả lại" là một hàm không thể trả về bình thường.Nó chỉ có thể chuyển kiểm soát thông qua longjmp (), ném (trong C ++) hoặc các cơ chế tương tự.Chức năng nổi bật nhất của lớp này là chức năng hủy bỏ.Các chức năng không trả lại được khai báo với loại trả về khoảng trống.a function which cannot return normally. It can transfer control only through longjmp() , throw (in C++), or similar mechanisms. The most prominent function of this class is the abort function. Non-returning functions are declared with a void return type.

Hàm không có giá trị trả về là gì?

Một hàm không có câu lệnh trả về sẽ trả về một giá trị mặc định.Trong trường hợp hàm tạo được gọi với từ khóa mới, giá trị mặc định là giá trị của tham số này.Đối với tất cả các chức năng khác, giá trị trả về mặc định không được xác định.return a default value. In the case of a constructor called with the new keyword, the default value is the value of its this parameter. For all other functions, the default return value is undefined .

Hàm trả về giá trị trong Python là gì?

Một câu lệnh trả về được sử dụng để kết thúc việc thực hiện cuộc gọi chức năng và trả về kết quả (giá trị của biểu thức theo từ khóa trả về) cho người gọi.Các tuyên bố sau các tuyên bố trả lại không được thực thi.Nếu câu lệnh trả về không có bất kỳ biểu thức nào, thì giá trị đặc biệt không được trả về.