C language function parameter description and return value

Source: Internet
Author: User
Tags function definition function prototype

C language is a structured language, its main structural component is function. A function is used as a component to complete a specific function in a program. Functions allow a program to be individually defined and coded for each task, so that the program is modular. This chapter introduces the design of C language function, how to use function to decompose the functions of the program, how to form a modular program, and the basic idea of code reuse.

function description and return value

The general form of the function is as follows:

Type descriptor function name (parameter table)
Parameter Description section
{
function body
}



1 Description of the type of function

The type specifier for a function can be any data type, including null-type void, that is, no return statement is used. By default, a function is automatically described as an integral type. The function must be defined before the first call, so the compiler can find the function. The predecessor function type description is called a function prototype.

#include <stdio.h>
#include <stdlib.h>

#define PI 3.141593
Float Cylinder_area (float, float); Description of the type of function, function prototype

int main ()
{
Float R, H, s; R Circle Radius, h high, s area
Puts ("Please enter the radius and height of the cylinder:");
if (scanf ("%f,%f", &r, &h) = = 2)
{
s = Cylinder_area (R, h); Call function Cylinder_area (), return value to variable s
printf ("\ n the surface area of the cylinder is:%f\n", s);
}
Else
{
Puts ("you entered the data format is not legitimate!") ");
}

return exit_success;
}

Float Cylinder_area (float R, float h)
{
float Cd_area;
Cd_area = 2 * (PI * R * r) + (2 * pi * R * H);

return cd_area;
}

When designing a function, you need to be aware of the matching relationship of the data type, the type specifier for the function must be the same as the data type returned by the return statement, and the parameter description of the function must be the same as the data type of the parameter, otherwise a compilation error will occur

2 Return Statement

The return statement of a function has 2 functions: First, end the current function, the caller gets control of the program, and second, the result is passed to the caller.

The end of the function has 2 kinds of cases, one is the function body all code completes, second is the return statement is executed.



parameters and return values of the C language function

parameters of the function

The parameters of a function are divided into formal parameters and arguments. The formal parameter appears in the function definition and can be used throughout the function, leaving the function unused. The argument appears in the function call.

The function of formal parameters and arguments is data transfer, and when a function call occurs, the value of the argument is passed to the formal parameter.

Formal parameters and arguments have the following characteristics:
1) Parametric the memory unit is allocated only when the function is called, and immediately releases the allocated memory unit at the end of the call. Therefore, formal parameters are valid only within functions and cannot be used outside of functions.

2 arguments can be constants, variables, expressions, functions, and so on, regardless of what type of data The argument is, they must have a definite value to pass the value to the formal parameter when the function is called. Therefore, the actual parameters should be given a definite value by means of assignment, input and so on.

3 arguments and formal parameters must be strictly consistent in number, type, and order, otherwise "type mismatch" errors occur.

The data transfer that occurs in a function call is one-way, and only the value of the argument can be passed to the formal parameter, and the parameter's value cannot be transferred back to the argument. Therefore, during a function call, the value of the formal parameter changes, and the value in the argument does not change.

The example calculates the value of the 1+2+3+...+ (n-1) +n.

#include <stdio.h>
int sum (int n) {
int i;
For (i=n-1 i>=1; i--) {
N+=i;
}
printf ("the Inner n =%d\n", n);
return n;
}
int main () {
int n, total;
printf ("Input a number:");
scanf ("%d", &n);
Total = SUM (n);
printf ("the outer n =%d \ n");
printf ("1+2+3+...+ (n-1) +n =%d\n", total);
return 0;
}

Run Result:
Input a number:100?
The Inner n = 5050
The outer n = 100
1+2+3+. .. + (n-l) +n = 5050

By scanf Enter the value of N, as an argument, in the call sum newsletters to the shape parameter n.

Note: In this example, the name of the parameter variable and the argument variable is n, but this is two different quantities and the respective scopes are different, as explained in the next section.

Output the N value once in the Mian function with the printf statement, and the n value is the value of the argument N. An n value is also output in the function sum with a printf statement, which is the last N value obtained by the formal parameter.

From the operating situation, the input n value is 100, that is, the value of the actual parameter n is 100, when this value is passed to the function sum, the initial value of the parameter n is 100, and in the process of executing the function, the parameter n is changed to 5050. The value of the output argument n is still 100 after the function has finished running. The value of the visible argument does not vary with the shape parameter.


The return value of the function

The value of the

function (or function return value) is the value obtained by the program segment in the function body after the function is called, which can be returned through the return statement. The general form of the

Return statement is:

return expression;

or:

return (expression);

For example:

return max;
return a+b;
return (100+200); There can be more than one return statement in the

function, but only one returns statement is executed per call, so there is only one returned value.

Once the return statement is encountered, regardless of the code behind it, the function immediately runs to the end and returns the value. For example:

    int func () {
        int a=100, b=200, C;
        return a+b;
        return a*b;
        return b/a;
   } The

Returns the value that is always a+b, that is, 300.

A function with no return value is an empty type, described in void. For example:


    void func () {
        printf ("Hello world!\ n ");
   }

Once the type of the function is defined as void, it can no longer accept its value. For example, the following statement is incorrect:

int a = func ();


to make your program readable and reduce errors, any function that does not require a return value should be defined as a null type.

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.