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)