Learning function is mainly to learn the function of the declaration, definition and invocation, see the following two examples to help us learn functions:
Topic One:
Write a function Iswithin () that accepts two arguments, one is a character, and the other is a string pointer. Its function is if the character is in a string. Returns 1 (true), or 0 (false) if the character is not in the string. Test in a complete program that uses circular statements to provide comfort for this function.
The code is as follows:
#include <stdio.h>
int iswithin (char p,char *q)
{while
(*q)
{
if (p = = *q) return
1;
else
q++;
}
return 0;
}
int main (int argc, char *argv[])
{
int m;
char p,*q;
p = *argv[1];
Q = argv[2];
m = Iswithin (p,q);
if (m = = 1)
printf ("\ '%c\ ' is in the string!\n", p);
else
printf ("\ '%c\ ' isn't in" string!\n ", p);
return 0;
}
The results of the implementation are as follows:
fs@ubuntu:~/qiang/hanshu$./hanshu2 h Hello
' h ' is in the string!
fs@ubuntu:~/qiang/hanshu$/hanshu2 H World
' H ' isn't in the string!
fs@ubuntu:~/qiang/hanshu$
Notice how the function passes the argument.
Topic Two,
the function of the following function is to compute the value of the N-Order de-phasor of X by a recursive method. An existing Call statement P (n,x): Write function implementation functionality.
The code is as follows:
#include <stdio.h>
int p (int n,int x)
{
int m;
if (n = = 0) return
0;
else
if (n = = 1) return
x;
else
{
m = ((2*n-1) *x*p (n-1,x)-(n-1) *p (n-2,x))/n;
return m;
}
}
int main (int argc, const char *argv[])
{
int x, n;
int q;
printf ("Please input x and n:\n");
scanf ("%d%d", &x,&n);
Q = P (n,x);
printf ("p =%d\n", q);
return 0;
}
The results of the implementation are as follows:
fs@ubuntu:~/qiang/hanshu$./HANSHU1 please
input x and N:
2
1
p = 2
Fs@ubuntu:~/qiang/hanshu $./HANSHU1 please
input x and N:
2
5
p = 194
fs@ubuntu:~/qiang/hanshu$