C and pointer learning notes-4. Functions

Source: Internet
Author: User

Parameter transfer:

PASS Parameters by value


ADT & black box

ADT: abstract data type, abstract data type

C can be used to design and implement abstract data types because it can restrict the scope of functions and data definitions. This technique is also called Black Box Design.

User. h

# Define MAXLEN 3 struct UserClz {char * name; char * phone; char * address;}; typedef struct UserClz User; /** */char const * lookupAddress (char const * name ); /** call the function * To find the phone number by location */char const * lookupPhone (char const * name );

User. c

# Include "user. h" # include
  
   
# Include
   
    
/** Restricted access scope */static User users [MAXLEN] = {"user1", "111", "aaa" },{ "user2", "222 ", "bbb" },{ "user3", "333", "ccc" }}; void init () {int j = 0; for (j = 0; j
    
     
Name, name) = 0) {return pu + I;} return NULL;} char const * lookupPhone (char const * name) {User const * pu; pu = getUserByName (name); if (pu! = NULL) {return pu-> phone;} else {return NULL;} char const * lookupAddress (char const * name) {User const * pu; pu = getUserByName (name); if (pu! = NULL) {return pu-> address;} else {return NULL ;}}
    
   
  

Main. c

#include 
  
   #include "user.c"void main(){init();printf("phone=%s\n",lookupPhone("user1"));printf("address=%s\n",lookupAddress("user2"));}
  


The above is a black box example. The function of the ink cartridge accesses the module through the specified interface, and the interface functions include lookupPhone and lookupAddress,

Users cannot directly access data related to the module implementation, such as users and getUserByName, which are declared as static


Recursion

Calculate the number of fipona

#include 
  
   int fn(int n){if(n<=2) return 1;return fn(n-1)+fn(n-2);}void main(){printf("result:%d\n",fn(5));// result:5}
  

Process Diagram













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.