Simple functions in C language

Source: Internet
Author: User

1 What is a function


1 Code snippets with specific functionality, that is, a piece of code that is combined to implement a function
2 function has a name, call function by name
3 functions: Avoid duplication of code, simplify programs, improve development rates, code readability
4 Write a program, divided into several blocks, each block has a number of functions. Each function can be implemented with a function that is easy to maintain

2 defining the basic structure and requirements of a function

Defining functions

return value type function name (formal argument list) {

function code;
return value://If the function has a return value, a return must be written

}

return value type: void int char float ...
Function Name: Example of Hump naming: Printhello.
Parameter: The material of the function
Function code: A code that implements a function
Return value: After submitting the material, the returned result is only one
Note: In different functions, you can use the same variable name

3 Methods for defining functions

A no return value, no parameters

void Hello () {//Execute 2 Call function method
printf ("The blue-European partners, everybody Good Morning!\n");
printf ("Good, very good, very good, yes!");
//}
/***
No return value using void
No parameters but must write ()
b no return value, parameter

void Printsum (int a,int b) {
printf ("%d +%d =%d", a,b,a + b);
}
No return value using void representation
Parameter variables can be arbitrary, define a few parameters to pass in a few parameters, print two numbers to add the results

C has return value, no parameter

int randomfor10to60 () {
Return Arc4random ()% 51 + 10;
}
(1) Declaring a return value type
(2) The function body to write the return statement, or error
(3) A function has only one return value
(4) The type of the actual return value, to be consistent with the type of the declaration return value, and, if not, cast to the declared return value type.

D has a return value, with parameters

int sumvalue (int a,int b) {
return a + B;

printf ("I'm behind the return");//The code after return will never execute

The defined function is written to the top of the main function.

3 Calling a function (in the main function)

Calling function: Using function name ()
If there are no parameters, write ()
If there are parameters, there are a few parameters, give several parameters
Parameters can be: constants, variables, expressions

Example int a = 3 int b = 4

Sumvalue (3 4); Calling functions

printf ("%d", Sumvalue (3 4));

4 Declaration of functions

There are two types of function declarations that are written on the main function and the other one builds two files. h and. m files, write the function declaration in the. h file

The function implementation (function definition) is written in the. m file and introduced in front of the main function, introduced with #import "function name", with #import< function name >

Refers to the function of the system, called in the main function, called by the name

(1) file name capitalization when creating files, name using Hump name method

Example

Where a function is declared

#import <Foundation/Foundation.h>

Declared place: parameter list can not write variable name
Declaration: The maximum value of two numbers
int maxValue (int a,int b);

Declaration: To find the minimum value of two numbers
int minValue (int a,int b);

where B functions are implemented (defined)

#import "Function.h"

Definition: The maximum value of two numbers
int maxValue (int a,int b) {
int max = a > B? A:B;
return Max;
}

Definition: Find the minimum value of two numbers
int minValue (int a,int b) {
int min = a < b? A:B;
return min;
}

Where C functions are called

int main (int argc, const char * argv[]) {

Function call

Declaration: The maximum value of two numbers
Maximum Value
printf ("Maximum value:%d\n", MaxValue (3,4));
Minimum value
printf ("Minimum Value:%d\n", MinValue (4,5));

5 arrays as arguments to functions

A. Arrays can be used as arguments to functions
B. When an array is an argument to a function, the function must have at least two parameters set, one parameter receives the array name, and the other parameter receives the array element.
C. function declaration Form return value type (int) a[],a[] do not need to fill in the number of elements
D. function call form array name as a parameter, the incoming is actually the first address of the array
F. The operation of formal parameters and arguments is the same memory space.

Example

A declaration function

#import <Foundation/Foundation.h>

Declaring: Printing elements in an array of integers
void Sortarrayforascend (int array[],int count);

B implementation functions (defining functions)

#import "FunctionAndArry.h"

Definition: Printing elements in an integer array
void PrintArray (int a[],int count) {
Traverse Print
for (int i = 0; i < count; i++) {
printf ("%d", a[i]);
}
}

C Call function

int main (int argc, const char * argv[]) {

int array[] = {1,2,3,4,5};
PrintArray (array, 5);

Simple functions in C language

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.