Home > Backend Development > C#.Net Tutorial > How to use return in c language

How to use return in c language

angryTom
Release: 2020-03-07 11:37:53
Original
6707 people have browsed it

How to use return in c language

Usage of return in C language

The return value of a function refers to the value obtained by executing the code in the function body after the function is called. As a result, this result is returned through the return statement. The general form of the

return statement is:

return 表达式;
Copy after login

or:

return (表达式);
Copy after login

It is correct with or without ( ). For the sake of simplicity, ( ) is generally not written. For example:

return max;
return a+b;
return (100+200);
Copy after login

Recommended learning: c language video tutorial

1. There can be multiple return statements, which can appear anywhere in the function body, but each call A function can only have one return statement executed, so there is only one return value (a few programming languages ​​support multiple return values, such as Go language). For example:

//返回两个整数中较大的一个
int max(int a, int b){
    if(a > b){
        return a;
    }else{
        return b;
    }
}
Copy after login

If a>b is true, return a will be executed, and return b will not be executed; if it is not true, return b will be executed, and return a will not be executed.

2. Once the function encounters the return statement, it will return immediately, and all subsequent statements will not be executed. From this perspective, the return statement also has the function of forcibly ending function execution. For example:

//返回两个整数中较大的一个
int max(int a, int b){
    return (a>b) ? a : b;
    printf("Function is performed\n");
}
Copy after login

The 4th line of code is redundant and will never have a chance to be executed.

For more c language tutorials, please pay attention to PHP Chinese website!

The above is the detailed content of How to use return in c language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template