LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-29-2008, 04:04 AM   #1
viveksnv
Member
 
Registered: Feb 2008
Posts: 38

Rep: Reputation: 15
functions in C


Hello All,

i don't know C.

is it possible to write a function with a function. Like

Example

test()
{
printf("Hello World");
test2()
{
printf("Welcome");
}
test2();
}

Please Explain in Detail..
Thanks in Advance...
 
Old 07-29-2008, 04:27 AM   #2
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
In ISO standard C you can't use inner in functions. gcc compiler has an extension that allows that.
 
Old 07-31-2008, 08:52 AM   #3
Nauntilus
Member
 
Registered: Oct 2005
Distribution: All of them
Posts: 140

Rep: Reputation: 18
Is there a specific reason you are using C instead of C++?
 
Old 08-03-2008, 11:43 PM   #4
smus
Member
 
Registered: Nov 2005
Location: Turkey
Distribution: Suse
Posts: 104

Rep: Reputation: 16
is there a reason to do that, if you have pls share with us i am wondering about this.

regards,
 
Old 08-04-2008, 12:31 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
It's to restrict the scope of the function, you see the idea in Java with inner classes
 
Old 08-04-2008, 12:48 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You can do this by placing the function in a separate source file while retaining the declaration within the function body.
Code:
one.c

test()
{
printf("Hello World");
void test2(void);
test2();
}
Code:
two.c

void test2(void)
{
printf("Welcome");
}
The linker will resolve the definition for you, something that Java won't do.
ta0kira

PS I generally prefer C over C++ whenever possible because it compiles with fewer run-time dependencies and creates linker-independent libraries.

Last edited by ta0kira; 08-04-2008 at 12:51 AM.
 
Old 08-04-2008, 03:09 AM   #7
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
You do not restrict the scope at all in our solution. With an example as:

Code:
void test()
{
  void test2(void)
  {
    printf("Welcome\n");
  }
  printf("Hello World\n");
  test2();
}
there will be no symbol named test2, so you can't call it even if you declare it. (yes, you can copy the code or something else, but test2 from test, will be accesible - will have the scope just in test.

And Java does pretty much the same. (of course, you need to specify classpath, but when you compile the linker must also know all the paths to the object files).
 
Old 08-04-2008, 06:50 AM   #8
neioo
LQ Newbie
 
Registered: Jan 2008
Location: Sant Quinti de Mediona
Distribution: Gentoo
Posts: 25

Rep: Reputation: 3
Wink

Quote:
Originally Posted by Nauntilus View Post
Is there a specific reason you are using C instead of C++?
Is there a specific reason for programming C++ instead of C?

I prefer C: no classes, no objects, no restricted access to functions, no inheritance, no interfaces and easy.
 
Old 08-04-2008, 12:43 PM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by vladmihaisima View Post
You do not restrict the scope at all in our solution.
What exactly do you mean? I don't know what you're talking about, but what I'm saying is that the way I wrote it, it's perfectly legal C. Are you saying that you can't go to yet another source file and use the same function in OP's solution? That's not scope restriction, that's symbol visibility, which can't be controlled using standard C except with static and inline. If you're worried about it showing up in a library's symbol table, just use __attribute__ ((visibility("internal"))) in the function definition.
ta0kira
 
Old 08-04-2008, 12:55 PM   #10
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
Sorry, mistyped. It was "your solution".

And the thing with inner functions is that there will be no symbol named as the inner function. You wouldn't be able to use test2 not even in the same file - except for the function where it is defined. The attribute thing is doing probably more or less the same thing (it is a C extension to restrict the visibility).

@neioo: you almost answered yourself. The restricted access is a big plus if the project is big and many people have to mantain it. Also, inheritance will help reuse parts of the code. I would say also that the type system is a bit better and allows you to catch more errors at compile time. Then again, you can mess up completly C++, but a bad programmer manages to do that even in C.

Last edited by vladmihaisima; 08-04-2008 at 01:03 PM. Reason: reply to neioo
 
Old 08-04-2008, 01:10 PM   #11
Berticus
Member
 
Registered: Jul 2005
Distribution: Arch
Posts: 159

Rep: Reputation: 31
Quote:
Originally Posted by vladmihaisima View Post
You wouldn't be able to use test2 not even in the same file - except for the function where it is defined.
I thought that's what the OP wanted... An inner function for scoping purposes.
 
Old 08-04-2008, 04:04 PM   #12
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Berticus View Post
I thought that's what the OP wanted... An inner function for scoping purposes.
No point in using invalid C when it isn't necessary. Honestly, is it really that important to protect your own code from yourself to that level? Don't tell yourself this, but you could also just copy and paste the same embedded function into another function, so by placing it in a different file, you'd have to open that file to copy and paste it.

The only real purpose I can see for this is to protect a library, which can easily be controlled by visibility or inline.
ta0kira
 
Old 08-04-2008, 04:20 PM   #13
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
I wouldn't recommend such a thing. I was just explaining why someone would want to do something like that.
 
Old 08-05-2008, 01:20 AM   #14
neioo
LQ Newbie
 
Registered: Jan 2008
Location: Sant Quinti de Mediona
Distribution: Gentoo
Posts: 25

Rep: Reputation: 3
Quote:
Originally Posted by vladmihaisima View Post
@neioo: you almost answered yourself. The restricted access is a big plus if the project is big and many people have to mantain it. Also, inheritance will help reuse parts of the code. I would say also that the type system is a bit better and allows you to catch more errors at compile time. Then again, you can mess up completly C++, but a bad programmer manages to do that even in C.
I'm sorry, it was hard to me to understand the existence of the Object oriented languages. I allways though that OO programming was simply to make programmers work harder: take care about classes, its visibility, its inheritance...

Now I see that the main of object oriented languages is to make easy to maintain a huge project with a lot of functionalities and with a lot of people working on it, although it has its cost in the performance. Because in huge projects, where hundreds of people are working together, is more important and abstraction of the problem, where a lot of little changes don't not affect the whole project than the performance.

Because I work in little projects, I have never taken this point of view. But I think that a well structured program written in C can also be maintained by a lot of people, if not, how can exists Linux kernel or GNU C library (for example)?

Ok, I stop this offtopic theme
 
Old 08-05-2008, 01:17 PM   #15
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by neioo View Post
I'm sorry, it was hard to me to understand the existence of the Object oriented languages. I allways though that OO programming was simply to make programmers work harder: take care about classes, its visibility, its inheritance...
Wrong place for this discussion, but I have to disagree.
ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: OpenOffice.org Calc functions, part 1: Understanding functions LXer Syndicated Linux News 0 03-31-2007 12:01 PM
Slackpkg: missing something in /usr/libexec/slackpkg/functions.d/dialog-functions.sh michelino Slackware 4 03-20-2007 12:22 PM
Converting php5 socket functions to php3 socket functions mrobertson Programming 0 06-23-2005 09:11 AM
g++ functions apreda07 Programming 4 07-22-2004 12:40 AM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration