summaryrefslogtreecommitdiff
path: root/reference/C/SYNTAX/return.html
blob: 68a9c2cf2e63ec3c436bb581f36624ec761a4ec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<title>The return statement</title>
<head>
<script language="JavaScript">
</script>
</head>
<body bgcolor="#ffffcc">
<hr>
<center>
<h1>The return statement </h1>
</center>
<hr>
<p>
<b>return</b> will return a value from a function to its caller. 
The value returned is 
the result of an <a href="../CONCEPT/expressions.html">expression</a>.
<p>
<hr>
As an Example this will print <b>7</b>

<p>
<table border=2 width="100%" bgcolor=ivory>
<tr>
<td>
<pre>

        int func(void);

	main()
 	{
	   printf("%d \n", func());
        }

        int func(void)
        {
           return 7;
	}
</pre>
</td>
</tr>
</table>
<p>
	   
What ever follows the <b>return</b> statement will be evaluated as 
an expression. So, to be consistant you could place brackets around 
the return value.

<p>
<table border=2 width="50%" bgcolor=ivory>
<tr>
<td>
<pre>

        return(7);
</pre>
</td>
</tr>
</table>
<p>
Or you could evaluate a formula on the statement:

<p>
<table border=2 width="50%" bgcolor=ivory>
<tr>
<td>
<pre>

	return (Count-1);
</pre>
</td>
</tr>
</table>
<p>
Finally, if the function returns a <a href=void.html>void</a>
the <b>return</b> statement is not required, but maybe needed to 
leave a function before the end of the function block. Here
is an example.

<p>
<table border=2 width="100%" bgcolor=ivory>
<tr>
<td>
<pre>

	void CheckDate(int)

	main()
	{
	  CheckDate(40)
        }

	void CheckDate(int Month)
	{
	  if (Month > 31)
	  {
	    return;
	  }

	  puts("Month is valid");
        }
</pre>
</td>
</tr>
</table>
<p>
<hr>
<h2>See also:</h2>
The <a href=../FUNCTIONS/exit.html>exit </a>function.


<p>

<hr>
<p>
<center>
<table border=2 width="80%" bgcolor="ivory">
<tr align=center>
<td width="25%">
<a href="../cref.html">                  Top</a>
</td><td width="25%">
<a href="../master_index.html">          Master Index</a>
</td><td width="25%">
<a href="keywords.html">                 Keywords</a>
</td><td width="25%">
<a href="../FUNCTIONS/funcref.htm">      Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie 
</address><p>
</body>
</html>