Getting Started with C programming-functions (top)

Source: Internet
Author: User

Functions are the most important part of a structured programming language, the basic unit of module independence.


Declaration of the function:

# include <stdio.h>void f (void);//Declaration of function, that is, declaration f is a function. void g (void); void f (void) {printf ("Haha! \ n ");p rintf (" Haha! \ n ");p rintf (" Haha! \ n ");} int main (void) {f (); F (); G (); return 0;} void g (void) {printf ("Hey! \ n ");}


The return value type of the function:

# include <stdio.h>int f (void) {return 10.5;//Because the function return value type is int, the integer 10 is returned instead of 10.5. That is, if the type of the return value of the function differs from the definition type, the type of the definition before the letter name will prevail. }int Main (void) {Double x = 1.314;x = f ();p rintf ("%lf\n", x); return 0;}

Examples of functions:

# include <stdio.h>void max1 (int i, int j)//max is the function name, int is the formal parameter type, I and J are formal parameters (parameter) to receive the argument value void to indicate that the function has no return value. {if (i > J) printf ("%d\n", I); elseprintf ("%d\n", j);} In other notation: int max2 (int i, int j) {if (i > J) return I;elsereturn J;} the int main (void)//int represents the type of the function return value, main is the function name, and void means that the function does not receive any values. {int A, B, C, D, E, f;int i;a = 3;b = 5;c = 12;d = 20;e = 128;f = 1314;max1 (A, B),//max is a reference max1 function, A/b in parentheses is an argument, the value of a A, a, a, a, a, and a. The parameter of the number, the argument parameter one by one corresponds. Max1 (c, D); Max1 (E, f);//Call to MAX2: i = Max2 (A, B);//Assignment printf ("%d", i);p rintf ("%d", Max2 (c, D));//Direct Output Method printf ("%d", max2 (e, F)); /Add: printf ("Time: January 4, 2013 If you do not leave, I will dependency ~\n"); return 0;}


The value of the function:

# include <stdio.h>int f (void) {return 10;//returns a value of 10 to the main function, and assigns the value to I. }int Main (void) {int i;i = f ();p rintf ("%d\n", I); return 0;}


Determine whether a number is prime:

# include <stdio.h>bool isprime (int val) {int i;for (i=2; i<val; i++) {if (val%i = = 0) break;} if (i = = val) return True;elsereturn false;} int main (void) {int val;printf ("Enter the number that needs to be judged:"), scanf ("%d", &val), and if (IsPrime (val))//isprime function The value is Boolean, which is true or false. printf ("This number is prime!") \ n "); elseprintf (" This number is not prime! ") \ n "); return 0;}


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

Getting Started with C programming-functions (top)

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.