Same name for an argument and a local variable for the initialization function in GrainGrowth application.

12 views
Skip to first unread message

Xuesong Gao

unread,
Oct 3, 2024, 9:16:43 AM10/3/24
to PRISMS-PF Users
Hi, I am learning the grainGrowth application. In the file "ICs_and_BCs.cc", the initialization function "setInitialCondition(const dealii::Point<dim> &p, ...other arguments)" has an argument named p. Inside the function, there are also local variables named p.
// The big grains
{
dealii::Point<dim> p(0.2, 0.15);
center.push_back(p);
}I am confused why the argument p is used when referring to the coordinates.
dist += (p[dir] - center[index][dir] * userInputs.domain_size[dir]) *
(p[dir] - center[index][dir] * userInputs.domain_size[dir]);
Thanks.

David Montiel

unread,
Oct 3, 2024, 4:43:27 PM10/3/24
to Xuesong Gao, PRISMS-PF Users
Hello, Xuesong

In the lines within the setInitialCondition such as 

{dealii::Point<dim> p(0.2, 0.15); center.push_back(p);}

"p" is a new variable that is local to the scope defined by the curly braces {}. Even though it has the same name as the function argument (const dealii::Point<dim> &p), it is a separate variable that exists only within its block. In C++, variables declared in inner scopes (such as inside loops or blocks) can have the same name as those declared in outer scopes without causing conflicts. 

That being said, I think the name of the local variable should be changed to avoid confusion. I have opened issue #255 about this.

I hope this is helpful, 

David

--
You received this message because you are subscribed to the Google Groups "PRISMS-PF Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prisms-pf-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prisms-pf-users/c38370df-1f62-4c9a-ba28-0894d6e41d9fn%40googlegroups.com.

Xuesong Gao

unread,
Oct 3, 2024, 7:54:41 PM10/3/24
to PRISMS-PF Users
David, much thanks! 
Reply all
Reply to author
Forward
0 new messages