C Language Basics Tutorial (v) functions (5)

Source: Internet
Author: User
Tags integer numbers printf

Two, the called function returns a value to the calling function
General use the return statement, which returns a value from the called function to the calling function, which has the following purposes:
1. It can exit immediately from the function in which it was invoked, and return to the program that called it.
2. Returns a value to the function that calls it.
There are two ways to terminate a child function and return it to the function that called it: one to return to the last statement of the function, and one to return when the statement is executed. The former returns only 0 to the calling function when the child function is finished. To return a value, you must use the returns statement. Simply specify the returned value in the return statement. Example 1 returns the maximum when:
Example 3:
#include <stdio.h>
int maxmum (int x, int y, int z);  /* description A user-defined function */
int main () br>{
Int I, J, K, Max;
printf ("I, J, k=?\n");
scanf ("%4d%4d%4d", &i, &j, &k);
Max=maxmum (I, J, K);  /* call the child function and assign the return value to max*/
printf ("The Maxmum value is%d\n", Max);
Getch ();
return 0;
}

Maxmum (int x, int y, int z)
{
int max;
max=x>y?x:y; /* Max */
max=max>z?max:z;
return (max);  /* returns the maximum/
}
Return statement returns a value to the calling function, but this method can return only one argument, in many cases returning multiple arguments, which is not satisfied with the return statement. Turob C2.0 provides another method of parameter passing, that is, the calling function passes the form parameter to the called function not by passing the variable itself, but by passing the address of the variable, which, when written to the corresponding address in the child function, changes the value of the corresponding variable in the calling function. This achieves the goal of returning multiple variables.
Example 4:
#include <stdio.h>
void subfun (int *m, int *n),  /* description child function */
int main ()
{
int i, J;
printf ("I, j=?\n");
scanf ("%d,%d", &i, &j);//* from the keyboard input 2 integers/
printf ("in main before calling\n" * Output this 2 number and its product/
"I=%-4D j=%-4d I*j=%-4d\n ", I, J, I*j);
Subfun (&i, &j);  /* call a child function by sending the address/
printf ("in main after calling\n"/* Call the child function after the output value of the variable/
"i=%-4d j= %-4d i*j=%-4d\n ", I, J, I*j);
Getch ();
return 0;
}
void Subfun (int *m, int *n)
{
*m=*m+2;
*j=*i-*j;
printf ("in Subfun after calling\n"/* Child function Output Variable value * * *
"i=%-4d j=%-4d i*j=%-4d\n", *i, *j, *i**j);
}


In the example above, *i**j represents the product of the two integer numbers *i and *j referred to by pointers I and J.
In addition, the return statement can be returned with a pointer, as shown in the following example.
The following example waits for a string to be entered, waits for the character to be found, and then calls the match () function to find the character in the string. If it has the same character, it returns a pointer to this position in the string, or returns a null (NULL) pointer if it is not found.
Example 5:
#include <stdio.h>
Char *match (char C, Char *s);
int main ()
{
Char s[40], C, *str;
Str=malloc (40); /* Allocate memory space for string (= *)
printf ("Please input character string:");
Gets (s); /* Keyboard Input String * *
printf ("Please input one character:");
C=getche (); /* Keyboard input character * *
Str=match (c, s); /* Call child function * *
Putchar (' \ n ');
Puts (str); /* Output child function returned by the pointer to the string referred to by * *
Getch ();
return 0;
}
Char *match (char C, char *s)
{
int i=0;
while (c!=s[i]&&s[i]!= ' \ n ')/* Find the character specified in the string * *
i++;
Return (&s[i]); /* Return the address of the character you are looking for
}

Related Article

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.