#What is the usage of return in C language?
The usage of return in C language is as follows:
The return value of a function refers to the result obtained by executing the code in the function body after the function is called. This result is returned via the return statement. The general form of the
return statement is:
return 表达式;
or:
return (表达式);
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);
Explanation of the C language return value:
1) A function without a return value is of empty type and is represented by void. For example:
void func(){ printf("http://c.biancheng.net\n"); }
Once the return value type of a function is defined as void, its value can no longer be received. For example, the following statement is wrong:
int a = func();
In order to make the program more readable and reduce errors, all functions that do not require a return value should be defined as void type.
2) There can be multiple return statements, which can appear anywhere in the function body, but only one return statement can be executed each time a function is called, so there is only one return value (a few programming languages support multiple return value, such as Go language). For example:
//返回两个整数中较大的一个 int max(int a, int b){ if(a > b){ return a; }else{ return b; } }
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.
3) 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"); }
The 4th line of code is redundant and will never have a chance to be executed.
Below we define a function to determine prime numbers. This example is more practical:
#include <stdio.h> int prime(int n){ int is_prime = 1, i; //n一旦小于0就不符合条件,就没必要执行后面的代码了,所以提前结束函数 if(n < 0){ return -1; } for(i=2; i<n; i++){ if(n % i == 0){ is_prime = 0; break; } } return is_prime; } int main(){ int num, is_prime; scanf("%d", &num); is_prime = prime(num); if(is_prime < 0){ printf("%d is a illegal number.\n", num); }else if(is_prime > 0){ printf("%d is a prime number.\n", num); }else{ printf("%d is not a prime number.\n", num); } return 0; }
prime() is a function used to find prime numbers. A prime number is a natural number, and its value is greater than or equal to zero. Once the value passed to prime() is less than zero, it is meaningless, and it cannot be judged whether it is a prime number. Therefore, once the value of the parameter n is detected to be less than 0, use the return statement to end early. function.
The return statement is the only way to end the function early. Return can be followed by a piece of data, indicating that the data is returned outside the function; return can also be followed by no data, indicating that nothing is returned and is only used to end the function.
Change the above code so that return is not followed by any data:
#include <stdio.h> void prime(int n){ int is_prime = 1, i; if(n < 0){ printf("%d is a illegal number.\n", n); return; //return后面不带任何数据 } for(i=2; i<n; i++){ if(n % i == 0){ is_prime = 0; break; } } if(is_prime > 0){ printf("%d is a prime number.\n", n); }else{ printf("%d is not a prime number.\n", n); } } int main(){ int num; scanf("%d", &num); prime(num); return 0; }
The return value of prime() is void, and return cannot be followed by any data, just write a semicolon.
Recommended tutorial: "C Video Tutorial"
The above is the detailed content of What is the usage of return in C language?. For more information, please follow other related articles on the PHP Chinese website!

C语言return的用法有:1、对于返回值类型为void的函数,可以使用return语句来提前结束函数的执行;2、对于返回值类型不为void的函数,return语句的作用是将函数的执行结果返回给调用者;3、提前结束函数的执行,在函数内部,我们可以使用return语句来提前结束函数的执行,即使函数并没有返回值。

源码:publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#输出上述代码的输出可以简单地得出结论:return在finally之前执行,我们来看下字节码层面上发生了什么事情。下面截取case1方法的部分字节码,并且对照源码,将每个指令的含义注释在

Vue3.2setup语法糖是在单文件组件(SFC)中使用组合式API的编译时语法糖解决Vue3.0中setup需要繁琐将声明的变量、函数以及import引入的内容通过return向外暴露,才能在使用的问题1.在使用中无需return声明的变量、函数以及import引入的内容,即可在使用语法糖//import引入的内容import{getToday}from'./utils'//变量constmsg='Hello!'//函数func

JavaScript 函数提供两个接口实现与外界的交互,其中参数作为入口,接收外界信息;返回值作为出口,把运算结果反馈给外界。下面本篇文章带大家了解一下JavaScript函数返回值,浅析下return语句的用法,希望对大家有所帮助!

Python返回值return用法是当函数执行到return语句时,将立即停止执行,并将指定的值返回给调用函数的地方。详细用法:1、返回单个值;2、返回多个值;3、返回空值;4、提前结束函数的执行。

JavaScript中return的用法,需要具体代码示例在JavaScript中,return语句用于指定从函数中返回的值。它不仅可以用于结束函数的执行,还可以将一个值返回给调用函数的地方。return语句有以下几个常见的用法:返回一个值return语句可以用来返回一个值给调用函数的地方。下面是一个简单的示例:functionadd(a,b){

JavaScript中return的使用方法,需要具体代码示例在JavaScript中,return是一个非常重要的关键字,它通常用于函数中返回值或结束函数的执行。return语句用于将值返回给函数的调用者,并终止函数的执行。return语句可以在函数的任何位置使用,并且可以返回任何JavaScript数据类型,包括数字、字符串、布尔值、

是的,即使在方法中的return语句之后,finally块也会被执行。Java中无论是否发生异常,finally块都会执行。如果我们在finally块中显式调用System.exit()方法,那么只有它不会被执行。很少有情况不会执行finally,例如JVM崩溃、电源故障、软件崩溃等。除了这些情况外,finally块将始终被执行。示例publicclassFinallyBlockAfterReturnTest{ publicstaticvoidmain(St


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
