Thread: Type of function

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    55

    Type of function

    I understand four type of function in c language as following
    Code:
    void foo (void) // Functin does't take any value and doesn't return any value 
    void foo (int X) // Functin take value store in X and doesn't return any value 
    int foo (void) // Functin does't take any value and return integer value
    int foo (int X) // Functin take value store in X and return integer value
    I don't understand following type of function

    Does it valid function?

    Code:
    int *foo ( int X)
    What type of function it is ? What does it return ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,361
    It's the same as your last example, except that the return type is pointer to int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    55
    Quote Originally Posted by laserlight View Post
    It's the same as your last example, except that the return type is pointer to int.
    Thank you

    Code:
    struct S *F (struct S *T){
    	return T;
    }
    S is structure Name, T is structure variable

    Is this function pass pointer to structure type struct ?
    Is this function return pointer to structure type struct ?

    What is F ? Is the function name F?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,361
    Where did you find this example? By now you should know enough to figure out from context.

    Besides figuring out from context, there's another thing you can do: write a program to test your hypotheses. So you put this function definition in your program, declare struct S however it suits you, then you try and write code to use this function according to what you think is the answer. If it fails, then unless you made some typo, probably your hypothesis is wrong. If it compiles, then your hypothesis might be right (you can always come back here with your test program to ask someone to confirm).

    I can of course tell you the answer right now, but this kind of exploration will help you get a stronger grounding in the concepts and syntax than just a quick reply from a community member.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2021
    Posts
    55
    Quote Originally Posted by laserlight View Post
    I can of course tell you the answer right now, but this kind of exploration will help you get a stronger grounding in the concepts and syntax than just a quick reply from a community member.
    I have written following code Just for doing experiment I have explain all with comments

    Code:
    #include<stdio.h>
    
    // function F get content of Y pointer and return pointer P to function
    int *F (int *P)
    {
      printf("P = %p \n", P); // Print content of pointer Y
      ++P;  // increment pointer value 
      return P; // return pointer P
    }
    int main ()
    {
    	int *Y;  // Decleare pointer to integer 
    	printf("Y = %p\n", Y); // Print content of pointer Y
        Y = F (Y);	// call function F and pass content of Y
    	printf("Y = %p\n", Y); //  print content of Y after calling function 
    	return 0;
    }
    Y = 00400080
    P = 00400080
    Y = 00400084

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Type/value mismatch while passing type as parameter to function
    By Omar Sharaki in forum C++ Programming
    Replies: 3
    Last Post: 05-12-2018, 11:18 PM
  2. Replies: 8
    Last Post: 03-23-2016, 09:46 AM
  3. Replies: 6
    Last Post: 04-30-2008, 11:14 AM
  4. void function(type **, type *) // more info. PLEASE HELP
    By Seek n Destroy in forum C Programming
    Replies: 5
    Last Post: 09-14-2002, 07:44 PM
  5. void function(type **, type *)
    By John Wallace in forum C Programming
    Replies: 3
    Last Post: 09-14-2002, 02:37 AM

Tags for this Thread