2015.1.26
Function: A function called a method, a function, or code reuse code body; a function is called by a function name
// *
// ***
// *****
//*******
void print (void)
//{
printf ("*\n");
printf ("***\n");
printf ("*****\n");
printf ("*******\n");
//
return;
//}
//
int add (int a, int b)
//{
return a+b;
//}
INT: return value data type
Add: function name, which is actually the entry address of the function
int a, int b:a, B is the interface of the function, is also the form parameter;
Return: function return value;
The function result is the return value of the function;
Function passing Value: passed by value, equivalent to the value to be passed to assign the parameter form;
int main (int argc, const char * argv[]) {
//
//
Print ();
Print ();
printf ("sum =%d\n", add (3,5));
//
return 0;
//}
Local variables and global variables
A local variable is a variable defined within {};
Scope of the local variable: start at the defined position and end with the corresponding};
The scope of the formal parameter: within the current function;
Actual parameters:
Global variables are variables that are defined outside the function body;
Scope of global variables: starting from the location of the definition or declaration, to the end of the whole source program;
The global variable does not belong to a function, it belongs to the whole source program;
The value of the global variable is easily modified
#include <stdio.h>
#if 0
extern int n;
int func (void)
{
int a=200;
printf ("A=%d\n", a);
for (int i; i<5; i++) {
int m=90;
printf ("welcome\n");
}
return 0;
}
int func1 (void)
{
int n=100;
printf ("n=%d\n", N);
return 0;
}
int changeval (int val)
{
n = val;
return 0;
}
int main (int argc, const char *argv[])
{
int a=20;//local variable
Func ();
a=300;
printf ("A=%d\n", a);
Func1 ();
n=3000;
Func1 ();
Changeval (a);
Func1 ();
int val=300;
Changeval (val);//val argument
Func1 ();
return 0;
}
int n=100;//; global variable
#endif
void Print_time (int hour, int min)
//{
printf ("%02d:%02d\n", Hour, Min);
printf ("%p%p\n", &hour, &min);
return;
//}
//
void print_time1 (int hour, int min)
//{
Print_time (Hour, Min);
//
Return
//}
//
//
int main (int argc,const char *argv[])
//{
//
Print_time (12, 5);
int a=1000;
int b=4000;
//
Print_time (13, 6);
//
Print_time1 (12,7);
//
printf ("A =%d, b=%d\n", A, b);
return 0;
//}
#include "Calculator.h"//contains the header file corresponding to the function of the header file with "" "contains, system-defined header file <> contains
int main (int argc,const char *argv[])
//{
printf ("Add =%d\n", add (3, 5));
printf ("Sub =%d\n", Sub (80, 34));
//
Setnum (200);
int ret = Getnum ();
printf ("ret =%d\n", ret);
//
num= 300;
Welcome;
printf ("min =%d\n", min (3, 5));
printf ("num =%d\n", num);
return 0;
//}
#include "Practice.h"
int main (int argc,const char *argv[])
{
int n;
scanf ("%d", &n);
int ret = IsPrime (n);
if (ret) {
printf ("%d is prime number \ n" and "Nth");
}
else{
printf ("%d is not prime number \ n";
}
printf ("zero =%d\n", Countofzero (n));
return 0;
}
ios-c_day6___ function