Getting Started with C programming-functions (bottom)

Source: Internet
Author: User


Meaning of void:

# include <stdio.h>int f (void)//void means no data is received, int means return int type value, that is, return 10 below; {return 10;//return 10 to the main function, that is, return 10 to the statement that is called in the function. A void before the}void g (void)//function name indicates that the function has no return value. {//return 10;//error, inconsistent with void above, cannot have return value. }int Main (void) {int i = 88;int J = 5201314;i = f ();//Because the F function does not receive any data, the parentheses are empty, and after the function is called, the F function returns the value of 10,i to 10. j = g (); Because the G function does not return a value, it cannot be assigned to J. Note: No return value is returned to 0, but it is returned as empty! printf ("%d\n", i);p rintf ("%d\n", j); return 0;}


The difference between break and return:

# include <stdio.h>void f (void) {int i;for (i=0; i<5; i++) {printf ("Hehe! \ n "); Break;//break is used to terminate the For loop. The}return;//return is used to terminate the entire function. printf ("Haha! \ n ");} int main (void) {f (); return 0;}


Function exercises--Multiple primes:

# include <stdio.h>//This function is to determine whether a number is a prime, if so, returns True, or False if not. BOOL IsPrime (int m) {int i;for (i=2; i<m; ++i) {if (0 = = m%i) break;} if (i = = m) return True;elsereturn false;} This function is to enter 1 to N of all prime numbers. void Traverseval (int n) {int i;for (i=2; i<=n; ++i) if (IsPrime (i)) printf ("%d\n", I);} int main (void) {int i;int val;int j;printf ("Please enter the number of times required:"), scanf ("%d", &i), for (j=1; j<=i; ++j) {printf ("Please enter the upper%d limit: ", j); scanf ("%d ", &val); Traverseval (val);} return 0;}

Global variables and local variables:

# include <stdio.h>int k = 1000;//k is a global variable that can be used by all functions. void f (int i) {int j;//parameter I and definition variable J are local variables, i.e. only for internal use of the function. printf ("k =%d\n", k);} int main (void) {int m = 10;f (m); return 0;}


Naming conflict issues for global variables and local variables:

# include <stdio.h>int i = 99;void f (int i) {printf ("i =%d\n", i);} int main (void) {F (8); return 0;} /* The output is: i = 8 that is, when the global variable name is the same as the local variable name, local variables, local variables (Private) Priority > Global Variables (public) are used by default. */



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Getting Started with C programming-functions (bottom)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.